一个DataList控件中,绑定的一个表中有几十个字段,如果对任意字段进行修改,点击保存的时候,是不是只能用类似下面的语句,把所有的文本框中的值取出来,保存到一个变量中,然后再组合成一个sql语句来保存?
例句如下:
string id=DataList1.DataKeys[e.Item.ItemIndex].ToString();
string fname = ((TextBox)e.Item.FindControl("TextBox2")).Text;
string city = ((TextBox)e.Item.FindControl("TextBox3")).Text;
db sdb = new db();
string query="update authors set au_fname='"+fname+"',city='"+city+"' where au_id='"+id+"'";
if (sdb.sql(query) > 0)
{
DataList1.EditItemIndex = -1;
bind();
}
因为表中的字段都与TextBox都是绑定了的,有没有像PB中,dw_1.update(),就搞定的呢?
例句如下:
string id=DataList1.DataKeys[e.Item.ItemIndex].ToString();
string fname = ((TextBox)e.Item.FindControl("TextBox2")).Text;
string city = ((TextBox)e.Item.FindControl("TextBox3")).Text;
db sdb = new db();
string query="update authors set au_fname='"+fname+"',city='"+city+"' where au_id='"+id+"'";
if (sdb.sql(query) > 0)
{
DataList1.EditItemIndex = -1;
bind();
}
因为表中的字段都与TextBox都是绑定了的,有没有像PB中,dw_1.update(),就搞定的呢?
<ItemTemplate>
ProductID:
<asp:Label ID="ProductIDLabel" runat="server" Text='<%# Eval("ProductID") %>'></asp:Label><br />
ProductName:
<asp:Label ID="ProductNameLabel" runat="server" Text='<%# Eval("ProductName") %>'>
</asp:Label><br />
Discontinued:
<asp:CheckBox ID="DiscontinuedCheckBox" runat="server" Checked='<%# Eval("Discontinued") %>' Enabled="false">
</asp:CheckBox><asp:Button ID="btnEdit" runat="server" Text="编辑" CommandName="Edit"/><br />
<br />
</ItemTemplate>
<EditItemTemplate>
ProductID:
<asp:Label ID="ProductIDEditLabel" runat="server" Text='<%# Eval("ProductID") %>'></asp:Label><br />
ProductName:
<asp:TextBox ID="ProductNameTextBox" runat="server" Text='<%# Bind("ProductName") %>'>
</asp:TextBox><br />
Discontinued:
<asp:CheckBox ID="DiscontinuedCheckBox" runat="server" Checked='<%# Bind("Discontinued") %>'>
</asp:CheckBox><asp:Button ID="btnUpdate" runat="server" Text="更新" CommandName="Update"/><asp:Button ID="btnCancel" runat="server" Text="取消" CommandName="Cancel"/><br />
<br />
</EditItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True" ProviderName="System.Data.SqlClient" SelectCommand="SELECT [ProductID], [ProductName], [Discontinued] FROM [Products]" UpdateCommand="UPDATE [Products] SET [ProductName] = @ProductName, [Discontinued] = @Discontinued WHERE [ProductID] = @ProductID">
<UpdateParameters>
<asp:Parameter Name="ProductName" Type="String" />
<asp:Parameter Name="Discontinued" Type="Boolean" />
<asp:Parameter Name="ProductID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
SqlDataSource1.UpdateParameters["ProductID"].DefaultValue = (e.Item.FindControl("ProductIDEditLabel") as Label).Text;
SqlDataSource1.UpdateParameters["ProductName"].DefaultValue = (e.Item.FindControl("ProductNameTextBox") as TextBox).Text;
SqlDataSource1.UpdateParameters["Discontinued"].DefaultValue = (e.Item.FindControl("DiscontinuedCheckBox") as CheckBox).Checked.ToString();
SqlDataSource1.Update();
DataList1.EditItemIndex = -1;
DataList1.DataBind();
}
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = e.Item.ItemIndex;
DataList1.DataBind();
}
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = -1;
DataList1.DataBind();
}