2013-12-18 2 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. 
      } 
     } 
관련 문제