简体   繁体   English

只有在以下情况下,EditItemTemplate内部的控件才可以在Page_Load()中使用:

[英]Control inside EditItemTemplate will be available in Page_Load() only if


On my page I've defined controls TextBox1, Label1 and GridView1. 在我的页面上,我定义了控件TextBox1,Label1和GridView1。 Inside GridView1 I've defined the following template: 在GridView1内部,我定义了以下模板:

           <asp:TemplateField>

              <ItemTemplate>
                <asp:LinkButton runat="server" Text="Edit" CommandName="Edit" ID="cmdEdit" />         
              </ItemTemplate>

              <EditItemTemplate>
                <asp:TextBox Text='<%# Bind("Notes") %>' runat="server" id="textBoxNotes" />
                <br /><br />
                <asp:LinkButton runat="server" Text="Update" 
                 CommandName="Update" ID="cmdUpdate" /> 
                <asp:LinkButton runat="server" Text="Cancel" 
                 CommandName="Cancel" ID="cmdCancel" />
              </EditItemTemplate>

            </asp:TemplateField>

If user enters a text into textBoxNotes and clicks cmdUpdate button, then on postback this text will already be available when Page_Load() is called. 如果用户在textBoxNotes中输入文本并单击cmdUpdate按钮,则在回发时,当调用Page_Load()时,此文本将已可用。


Thus, if user, before clicking the update button cmdUpdate, also entered into TextBox1 a string “something”, then the following code will extract the text user entered into textBoxNotes 因此,如果用户在单击更新按钮cmdUpdate之前还向TextBox1输入了字符串“ something”,则以下代码将提取用户输入到textBoxNotes中的文本

    protected void Page_Load(object sender, EventArgs e)
    {
        if(TextBox1.Text=="text from Notes")
        Label1.Text =((TextBox)gridEmployees.Rows[0].Cells[0].FindControl("textBoxNotes")).Text;
    }


A) The following code should also extract the text user entered into textBoxNotes, but the moment I click cmdEdit button, I get “Object reference not set to an instance of an object.” exception A)以下代码也应该提取输入到textBoxNotes中的文本用户,但是当我单击cmdEdit按钮时,我得到“对象引用未设置为对象的实例。”异常

    protected void Page_Load(object sender, EventArgs e)
    {
        if(IsPostBack)
        Label1.Text =((TextBox)gridEmployees.Rows[0].Cells[0].FindControl("textBoxNotes")).Text;
    }

Why do I get this exception? 为什么会出现此异常? It appears as if textBoxNotes doesn't exist. 似乎textBoxNotes不存在。 But why wouldn't it exist? 但是为什么不存在呢?


thanx 谢谢

The manner you're referring to in the Page_Load occurs before the gridview's rows actually exist. 您在Page_Load中引用的方式是在gridview的行实际存在之前发生的。 Because it's occuring on the PostBack, the GridView (and firing events) must be recreated (re: its rows populated) from the ViewState. 因为它发生在PostBack上,所以必须从ViewState重新创建GridView(和触发事件)(re:填充其行)。 While the objects on the page have been initted and their values have been populated, the GridView has not been recreated yet and its events have not fired. 初始化页面上的对象并填充其值后,尚未重新创建GridView且未触发其事件。

As dr mentioned, this should be done in the RowUpdating event. 如博士所说,这应该在RowUpdating事件中完成。

When the Update event occurs, the row is no longer in edit mode. 发生Update事件时,该行不再处于编辑模式。 Therefore textBoxNotes does not exist on the page. 因此,textBoxNotes在页面上不存在。 Use the RowUpdating event handler of the gridview to access the edit template controls. 使用gridview的RowUpdating事件处理程序访问编辑模板控件。

public void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
     TextBox1.Text = ((TextBox)GridView1.Rows[e.RowIndex]
                              .FindControl("textBoxNotes")).Text;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM