gridview 里面放一个按纽,怎么点击它
<asp:GridView ID= "GV_userinfo " CellPadding= "0 " CellSpacing= "0 " runat= "server " Width= "100% " AllowPaging= "false " AutoGenerateColumns= "false ">
<Columns>
<asp:TemplateField> <ItemTemplate>
<table cellpadding= "0 " cellspacing= "0 " border= "0 " width= "98% ">
<tr> <td> 用户名: </td> <td>
<asp:TextBox ID= "user_name " runat= "server "> </asp:TextBox> </td> </tr>
<tr> <td> 密码: </td> <td> <asp:TextBox ID= "password " runat= "server "> </asp:TextBox> </td> </tr>
<tr> <td>
<asp:Button ID= "cmd_button " runat= "server " Text= "保存 " /> </td> <td> <asp:Button ID= "cmd_reset " runat= "server " Text= "取消 " /> </td> </tr>
</table>
</ItemTemplate> </asp:TemplateField>
</Columns>
</asp:GridView>
现在我想点击cmd_button,想把用户名和密码的信息,填进数据库中。
------解决方案-------------------- <asp:Button ID= "cmd_button " runat= "server " Text= "保存 " CommandName= "Save " />
protected void DataList1_ItemCommand(object source,
DataListCommandEventArgs e)
{
if (e.CommandName == "Save ")
{
// Add code here to add the item to the shopping cart.
// Use the value of e.Item.ItemIndex to find the data row
// in the data source.
}
}
------解决方案--------------------首先 你要在 <asp:GridView...DataKeyNames= "au_id "> 把你库中的主键值写上
你的更新按纽最好把CommandName属性赋值,比如 cmd_button.CommandName= "edit "
然后在模板列事件中写:
C#:
---------------------------
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(cmd_button.CommandName= "edit ")//判断你更新的是关于什么操作的按钮
{
//取出你要更新行的主键值
Control cmdSource = (Control)e.CommandSource;
GridViewRow row = (GridViewRow)cmdSource.NamingContainer;
int rowIndex = row.RowIndex;
int id = Convert.ToInt32(GridView1.DataKeys[rowIndex].Value);
//接着取出你两个文本框中的值
TextBox txtName=(TextBox)ro