将gridview中的templatefield的值赋给变量

时间:2013-03-13 20:08:24

标签: asp.net gridview templatefield

我知道如何在templatefield事件中获得RowDataBound的价值:

Control ctrl = e.Row.FindControl("Drop_Responsaveis");  
DropDownList ddl = ctrl as DropDownList;
ddl.items.add(something);

但我需要在button_Click事件中获得它的价值......我该怎么做?

@Siz S回答解决方案

foreach (GridViewRow gvr in GridView1.Rows)
   {
     string str = ""
     Control ctrl = gvr.FindControl("Drop_Responsaveis");
     if (ctrl != null)
        {
           DropDownList ddl = ctrl as DropDownList;
           str= ddl.SelectedItem.ToString();
        }
    }

1 个答案:

答案 0 :(得分:1)

你可以将gridview TemplateField控件作为

foreach (GridViewRow row in yourGrid.Rows)
{
     Control ctrl = row.FindControl("Drop_Responsaveis");
     DropDownList ddl = ctrl as DropDownList;
     ddl.items.add(something);
 }