ASP.NET页面加载 - 无法在编辑项模板中找到控件

时间:2016-05-13 18:37:15

标签: asp.net gridview

为什么我在gridview中的编辑项模板中找不到文本框控件? 我在编辑按钮上设置了Session [“index”],然后单击

if (Session["index"] != null)  
{  TextBox txt =
     GridView1.Rows[Convert.ToInt32(Session["index"])].FindControl("txtEmail") as TextBox; 

  txt.Text = "AAAAA";
}

GridView:http://pastebin.com/CwAqs2J3

1 个答案:

答案 0 :(得分:1)

我认为你应该先把它放在对象中,然后确定控件是否是这样的文本框:

Textbox txt = new Textbox();
if(Session["index"] != null) {
    int sessionIndex = Convert.ToInt32(Session["index"]);
    object thiscontrol = GridView1.rows[sessionIndex].FindControl("txtPhone");
    if(thiscontrol is typeof(Textbox))
    {
        txt = thiscontrol;
        txt.Text = "AAAAA";
    }
}

我希望这会有所帮助。

相关问题