2012-04-02 66 views
-1

我需要获取gridview中所选行单选按钮的值。选定的值被分配给一个文本框。我的代码是如何在gridview中使用按钮单击中的单选按钮获取所选行的值

protected void lnBTNDone_Click(object sender, EventArgs e) 
      { 
       GridViewRow gvRow = grdEventDetails.SelectedRow; 
       txtEventId.Text = gvRow.Cells[0].Text; 

      } 

问题是gvRow值被分配为空。

linkbutton不在gridview中。 gridview n链接按钮和文本框在用户控件中。

protected void lnBTNDone_Click(object sender, EventArgs e) 
{ 
    LinkButton lnBTNDone = (LinkButton)sender; 
    GridViewRow row = (GridViewRow)lnBTNDone.NamingContainer; 
    txtEventId.Text = row.Cells[0].Text; 
} 

假设LinkButton是选择行:

回答

2
protected void lnBTNDone_Click(object sender, EventArgs e) 
{ 
    for (int i = 0; i < grdEventDetails.Rows.Count; i++) 
    { 
     RadioButton rb = (grdEventDetails.Rows[i].FindControl("grdRdo")) as RadioButton; 
     if (rb.Checked == true) 
     { 
      txtEventId.Text = grdEventDetails.Rows[i].Cells[1].Text; 
     } 
    }   
} 
+0

这不适用于在网格视图中进行的可见性错误设置.. – 2017-10-09 03:26:05

2

你总是可以通过发件人的NamingContainer得到GridViewRow

顺便说一下,我还不确定为什么SelectedRow为空,可能是因为GridView的SelectedIndexChanged事件是在LinkBut​​ton的单击事件后触发的。

+0

获取此错误无法将类型为'ASP.usercontrols_usc_eventidparameter_ascx'的对象转换为键入'System.Web.UI.WebControls.GridViewRow'。 – kk1076 2012-04-02 11:48:59

+0

@ kk1076:那么我的假设是什么不正确。我以为LinkBut​​ton在GridView中。然后你应该告诉我们linkbutton的位置以及你如何选择gridview中的行。 – 2012-04-02 11:52:39

+0

链接按钮不在gridview中。但是gridview,textbox和链接按钮放在用户控制页面中 – kk1076 2012-04-02 12:03:05

相关问题