中继器页脚中的Find控件为文本框返回null



我有两个嵌套的中继器。在嵌套的页脚中,我有文本框和文件上传控件。我可以毫无问题地上传文件的实例,但文本框的实例为空,尽管两者都放在页脚中。

以下是表示内部中继器页脚的aspx部分:

<FooterTemplate>
            <tr class="add_comment">
                <td>Add comment </td>
            </tr>
            <tr>
                <td>
                    <asp:TextBox runat="server" Columns="20" Rows="3" ID="comment_txt" TextMode="MultiLine" Width="60%" CssClass="new_comment" ViewStateMode="Inherit"></asp:TextBox>
                </td>
             </tr>
           <tr class="add_comment">
                <td>
                            <asp:FileUpload ID="uploadImageBtn" runat="server" Text="Add image" OnClick="uploadImage" CssClass="comment_buttons" />
                            <asp:Button ID="comment_btn" runat="server" OnClick="submitComment" Text="Comment" CssClass="comment_buttons" />
                </td>
       </tr>
         </table>
     </FooterTemplate>

这是我试图访问控件的C#代码:

protected void commentsRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Footer ))
            {
                Repeater childRepeater = (Repeater)sender;
                TextBox commentTextBox = (TextBox)e.Item.FindControl("comment_txt");
                String postText = commentTextBox.Text.ToString(); 
                FileUpload upFile = (FileUpload)e.Item.FindControl("uploadImageBtn");
            }
        }

当运行页面时,我得到这个错误,

Object reference not set to an instance of an object 

这是由以下线路引起的:

String postText = commentTextBox.Text.ToString(); 

我尝试删除文本框代码,只检索上传文件,效果非常好。访问文本框时出现问题。

编辑:文本框和上传按钮实例的访问文本应在同一页面中某个按钮的onclick事件处理程序中访问。因此,我已经全局定义了这两个,并在执行中继器的一些嵌套中继器事件(如ItemDataBound)或Adrian Iftode建议的事件(即ItemCreated)时为它们分配了值。然后,在按钮的onclick中,我使用了它们,假设它们有值,因为嵌套的中继器事件应该在按钮onclick之前触发。上载文件实例已成功检索,但文本框始终为null。

全局变量声明:

  TextBox commentTextBox;
    FileUpload upFile;
    Repeater childRepeater;
    String postText; 

嵌套中继器事件内的代码:

 protected void commentsRepeater_ItemCreated(object sender, RepeaterItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Footer))
            {
                 childRepeater = (Repeater)sender;
                 commentTextBox = (TextBox)(e.Item.FindControl("comment_txt"));
                 postText = commentTextBox.Text.ToString();
                upFile = (FileUpload)e.Item.FindControl("uploadImageBtn");
            }
        }

onclick:内的代码

protected void submitComment(object sender, EventArgs e)
        {

            Boolean insert = true;
            if (upFile.HasFile || !String.IsNullOrEmpty(postText))
            {
                   //some code. 
              }

上面的if语句仅在upFile有文件的情况下执行,postText始终被视为null。

有人能帮我吗?是什么原因导致了这个错误?

谢谢。

ItemDataBound不是在这种情况下处理的正确事件,因为转发器项的页眉和页脚模板没有在中实例化。

适当的事件是ItemCreated

protected void rp_ItemCreated(Object sender, RepeaterItemEventArgs e)
{
            if (e.Item.ItemType == ListItemType.Footer)
            {    
              e.Item.FindControl(ctrl);
            }
            if (e.Item.ItemType == ListItemType.Header)
            {
              e.Item.FindControl(ctrl);
            }
}

最新更新