2011-12-26 4 views
0

일부 열이있는 gridview 및 버튼이 포함 된 템플릿 필드 열이 있는데 버튼 클릭시 프로 시저를 호출하려고하지만 열의 값을 전달하려고합니다. 프로 시저가 있지만 오류가 발생했습니다 여기에 단추의 동작 수신기가 있습니다 : (gridview의 열 이름은 team_ID입니다) 오류 : Eval(), XPath() 및 Bind() 같은 데이터 바인딩 방법 만 사용할 수 있습니다 데이터 바인딩 된 컨트롤의 맥락에서. 오류 라인 : int team_ID = Convert.ToInt32 (Eval ("team_ID"));버튼 클릭시 GridView의 데이터에 액세스

protected void Button1_Click(object sender, EventArgs e) 
    { 
     string connStr = ConfigurationManager.ConnectionStrings["MyDbConn"].ToString(); 
     SqlConnection conn = new SqlConnection(connStr); 

     SqlCommand cmd = new SqlCommand("join_team", conn); 
     cmd.CommandType = CommandType.StoredProcedure; 
     int team_ID = Convert.ToInt32(Eval("team_ID")); 
     string email = Session["email"].ToString(); 
     cmd.Parameters.Add(new SqlParameter("@team_ID", team_ID)); 
     cmd.Parameters.Add(new SqlParameter("@myemail", email)); 
     conn.Open(); 
     cmd.ExecuteNonQuery(); 
     conn.Close(); 

    } 

답변

2

첫째, TemplateField에서 클릭 무슨 버튼을 처리하기 위해, 당신은 RowCommand 방법에 가입하고자 할 수 있습니다 : 당신은 당신의 그리드에 여러 개의 버튼을 가지고 발생하는 추측 할 수

<asp:GridView runat="server" ID="gv" OnRowCommand="yourMethod"> 

CommandName 속성으로 클릭하십시오. 아래의 코드는 클릭 한 버튼의 행을 가져 오는 방법과 해당 행에서 다른 컨트롤을 검색하여 값을 얻을 수있는 방법을 보여줍니다.

protected void yourMethod(object sender, GridViewCommandEventArgs e) { 
    if (e.CommandName == "yourButtonName") { 

     GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer); 

     TextBox someTextBox = row.FindControl("tb") as TextBox; 
     string textValue = someTextBox.Text; 
    } 
} 
뒤에

<asp:TemplateField> 
     <ItemTemplate> 
      <asp:Button CommandName="yourButtonName" runat="server" /> 

코드