程序是需要不斷改進,上午做了,下午再去看時,會覺它不夠完美。
比如這個:http://www.jb51.net/article/33563.htm現在Insus.NET把它改為下面這個樣子:
這篇與前一篇改進部分,也許大家會留意到動畫演示,主要是GridVeiw的更新與刪除會在每row都有。因此Insus.NET把它抽取出來,放在GridView外。致於一次性更新多筆記錄,Insus.NET在很早之前已經錄制過視頻下載地址。還有一個就是刪除,在每row第一列放了一個CheckBox,讓用戶可以選擇記錄進行刪除。
復制代碼 代碼如下:
<asp:TemplateField>
<ItemStyle BorderWidth="1" BorderColor="#c0c0c0" Width="1%" />
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" onclick="Javascript:changeRowBgColor(this)" />
</ItemTemplate>
</asp:TemplateField>
刪除前提示用戶確認之後,再刪除,但是在這裡遇上一個問題,就是首先判斷是否有選擇記錄,再confirm用戶確認刪除
還有一點要注意的地方,就是選擇CheckBox之後,行可以Highlight,實現方可以參考:http://www.jb51.net/article/33558.htm
以下較完整代碼,僅供參考:
復制代碼 代碼如下:
<asp:Table ID="Table1" runat="server" CssClass="table" CellPadding="2" CellSpacing="0">
<asp:TableHeaderRow Height="20" BackColor="#efebde" BorderWidth="1" BorderColor="#c0c0c0">
<asp:TableHeaderCell BackColor="#efebde" BorderWidth="1" BorderColor="#c0c0c0">
Chinese Name
</asp:TableHeaderCell>
<asp:TableHeaderCell BackColor="#efebde" BorderWidth="1" BorderColor="#c0c0c0" Width="50%">
English Name
</asp:TableHeaderCell>
</asp:TableHeaderRow>
<asp:TableRow Height="20">
<asp:TableCell BorderWidth="1" BorderColor="#c0c0c0">
<asp:TextBox ID="txt_CName" runat="Server" CssClass="textbox" />
</asp:TableCell>
<asp:TableCell BorderWidth="1" BorderColor="#c0c0c0">
<asp:TextBox ID="txt_EName" runat="Server" CssClass="textbox" />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
<div style="margin-top: 3px; margin-bottom: 3px; padding: 3px;">
<asp:Button ID="ButtonInsert" Text="Insert" runat="Server" OnClick="ButtonInsert_Click" />
<asp:Button ID="ButtonUpdate" Text="Update" runat="Server" OnClick="ButtonUpdate_Click" />
<asp:Button ID="ButtonDelete" Text="Delete" runat="Server" OnClick="ButtonDelete_Click"
CausesValidation="false" />
</div>
<asp:GridView ID="GvCutterType" runat="server" DataKeyNames="CutterTypeId" AutoGenerateColumns="false"
CellPadding="2" CellSpacing="0" Width="100%" BorderWidth="0" BorderColor="#c0c0c0"
RowStyle-Height="20" ShowHeader="false">
<Columns>
<asp:TemplateField>
<ItemStyle BorderWidth="1" BorderColor="#c0c0c0" Width="1%" />
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" onclick="Javascript:changeRowBgColor(this)" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemStyle BorderWidth="1" BorderColor="#c0c0c0" />
<ItemTemplate>
<asp:TextBox ID="txtCName" runat="server" Text='<%# Eval("CName") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemStyle BorderWidth="1" BorderColor="#c0c0c0" Width="50%" />
<ItemTemplate>
<asp:TextBox ID="txtEName" runat="server" Text='<%# Eval("EName") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
xxx.aspx.cs:
復制代碼 代碼如下:
protected void ButtonInsert_Click(object sender, EventArgs e)
{
//do Insert something
//obj.Insert(......);
}
protected void ButtonUpdate_Click(object sender, EventArgs e)
{
//reference this url: http://www.cnblogs.com/insus/articles/1400150.html
}
protected void ButtonDelete_Click(object sender, EventArgs e)
{
// string dataKeyNames = xxx;
// string[] arr = dataKeyNames.Substring(1).Split(',');
try
{
foreach (string s in arr)
{
//obj.Delter(s);
}
}
catch (Exception ex)
{
//alert exception message
}
}