首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

gridview 里面放一个按纽,如何点击它

2012-01-01 
gridview 里面放一个按纽,怎么点击它asp:GridViewID GV_userinfo CellPadding 0 CellSpacing 0

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)row.FindControl( "user_name ");


TextBox txtPwd=(TextBox)row.FindControl( "password ");

//现在要更新的主键ID值已经取出,要更新的内容也取出,你现在就执行更新就行了
string sql= "update 表 set username= ' "+txtName.Text+ " ',pwd= ' "+txtPwd.Text+ " ' where id= ' "+id+ " ' ";
....
con.open();
com......//执行操作
con.clolse();
}
}
[解决办法]

探讨
首先 你要在 <asp:GridView...DataKeyNames= "au_id "> 把你库中的主键值写上
你的更新按纽最好把CommandName属性赋值,比如 cmd_button.CommandName= "edit "
然后在模板列事件中写:
C#:
---------------------------
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(cmd_button.CommandName= "edit ")//判断…

热点排行