在转发器页脚中查找控件对文本框返回 null

Dan*_*nia 5 c# asp.net repeater findcontrol nested-repeater

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

这是代表内部转发器页脚的 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>
Run Code Online (Sandbox Code Playgroud)

这是我尝试访问控件的 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");
    }
}
Run Code Online (Sandbox Code Playgroud)

运行页面时出现此错误,

你调用的对象是空的

这是由这一行引起的:

String postText = commentTextBox.Text.ToString(); 
Run Code Online (Sandbox Code Playgroud)

我尝试删除文本框代码并仅检索上传文件,效果非常好。问题在于访问文本框。

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

全局变量声明:

TextBox commentTextBox;
FileUpload upFile;
Repeater childRepeater;
String postText; 
Run Code Online (Sandbox Code Playgroud)

嵌套转发器事件内的代码:

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

onclick里面的代码:

protected void submitComment(object sender, EventArgs e)
{
    Boolean insert = true;
    if (upFile.HasFile || !String.IsNullOrEmpty(postText))
    {
        //some code. 
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的 if 语句仅当 upFile 有文件时才执行,postText 始终被视为 null。

谁能帮我,是什么导致了这个错误?

Adr*_*ode 2

在这种情况下,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);
            }
}
Run Code Online (Sandbox Code Playgroud)