2013-12-18 13 views
-1

我有一个Gridview有两个按钮,我希望能够在用户点击它们时运行代码。我已经尝试使用Row_command并在按钮上设置了CommandName,但我正在圈圈!帮帮我!从gridview中的2个按钮运行c#代码

我似乎无法以搜索在rowcommand用户从第一小区获取用户名名称:

protected void gridview_search_RowCommand(object sender, 
    GridViewCommandEventArgs e) 
{ 
    if (e.CommandName == "unlock_account") 
    { 
     string user = gridview_search.SelectedRow.Cells[0].ToString(); 

     //run code when user is obtained 
    } 
} 
+2

请发布您当前使用的代码 – ganders

回答

0

我的语法是错误的命令名称的一部分。我终于弄明白了,然后能够使用DataKeys从gridview中获得选定的值,然后可以通过发送该值来解锁用户帐户。

protected void gridview_search_RowCommand(object sender, 
    GridViewCommandEventArgs e) 
{ 
    if (e.CommandName.CompareTo("unlock_account") == 0) 
    { 
     int user = (int)gridview_search.DataKeys[Convert.ToInt32(e.CommandArgument)].Value; 

     //unlock account method 
     userObj.unlockAccount(user); 

     } 
} 
0

应能设置事件处理程序的按钮,无论工作,你正在尝试运行。

dgvbutton1.clickevent = dosomething(); 
dgvbutton2.clickevent = dosomething(); 

东西沿着这些线路应该工作

0
<asp:TemplateField> 
    <ItemTemplate> 
    <asp:Button ID="AddButton" runat="server" 
     CommandName="AddToCart" 
     CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" 
     Text="Add to Cart" /> 
    </ItemTemplate> 
</asp:TemplateField>  

     protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
     { 
      if (e.CommandName == "AddToCart") 
      { 
       // Retrieve the row index stored in the 
       // CommandArgument property. 
       int index = Convert.ToInt32(e.CommandArgument); 

       // Retrieve the row that contains the button 
       // from the Rows collection. 
       GridViewRow row = GridView1.Rows[index]; 

       // Add code here to add the item to the shopping cart. 
      } 
     }