日期:2014-05-20  浏览次数:20863 次

datagrid里的模板列textbox输入数据后怎么添加到数据库中
前台
<TABLE id="Table1" cellSpacing="1" cellPadding="1">
<TR>
<TD><asp:datagrid id="DataGrid1" runat="server" HorizontalAlign="Center" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="id" HeaderText="部门"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="技术部">
<ItemTemplate>
<asp:TextBox id="tx1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"技术部")%>'>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="客服部">
<ItemTemplate>
<asp:TextBox id="tx2" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"客服部")%>'>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid></TD>
</TR>
<tr>
<td><asp:Button id="Button1" runat="server" Text="保存"></asp:Button></td>
</tr>
</TABLE>

------解决方案--------------------
C# code

//前台模板列中加入:
    CommandName="text"
//后台代码
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "text")
        {
            //你的操作
        }
    }

------解决方案--------------------
if(!IsPostback)
{
DataGrid1.DataSource=dt;
DataGrid1.DataKeyField="id";
DataGrid1.DataBind();
}

<asp:Button id="Button1" runat="server" Text="保存" onclick="Button1_Click"> </asp:Button> 

void Button1_Click(.......)
{
for (int i = 0; i < DataGrid1.Items.Count; i++)
{
string txt1 = (DataGrid1.Items[i].FindControl("txt1") as TextBox).Text;//获取textbox的值
string txt2 = (DataGrid1.Items[i].FindControl("txt2") as TextBox).Text;

string id = DataGrid1.DataKeys[i].ToString();//获取id
//执行你的sql
}}