0

我在 ASP.Net 页面上有一个中继器控件。它有一个复选框、标签、文件上传控件和3个隐藏字段。我想根据隐藏字段之一的值隐藏/显示文件上传控件。

这是中继器的 ItemDataBound 事件的代码片段。

protected void rptChecks_ItemDataBound (object sender, RepeaterItemEventArgs e)
{ 
    HiddenField hdID = (HiddenField)e.Item.FindControl("hdnCheckID");
    HiddenField hdDocOpt = (HiddenField)e.Item.FindControl("hdnDocOption");
    FileUpload fileCheck = (FileUpload)e.Item.FindControl("fileDocument");

    if ( Convert.ToInt32(hdDocOpt.Value) == 0 || Convert.ToInt32(hdDocOpt.Value) == 1)
            fileCheck.Visible = true;
        else
            fileCheck.Visible = false;
}

这是一个标记代码

<asp:Repeater ID="rptChecks" runat="server" 
                onitemdatabound="rptChecks_ItemDataBound">
                <HeaderTemplate>
                    <table>
                        <tr>
                            <th>
                                &nbsp;
                            </th>
                            <th>
                                &nbsp;
                            </th>
                            <th>
                                &nbsp;
                            </th>
                        </tr>
                </HeaderTemplate>
                <ItemTemplate>
                    <tr>
                        <td>
                            <asp:CheckBox ID="chk" runat="server" />
                        </td>
                        <td style="padding-left: 10px">
                            <asp:Label ID="lblCheck" runat="server" Text="<%#Bind('Name') %>"></asp:Label>
                        </td>
                        <td style="padding-left: 10px">
                            <asp:HiddenField ID="hdnDocOption" runat="server" Value="<%#Bind('DocumentOption') %>" />
                            <asp:HiddenField ID="hdnCheckID" runat="server" Value="<%#Bind('ID') %>" />
                            <asp:FileUpload ID="fileDocument" runat="server" />
                        </td>
                    </tr>
                </ItemTemplate>
                <FooterTemplate>
                    </table>
                </FooterTemplate>
            </asp:Repeater>

我怎样才能做到这一点?

错误:发生错误,因为对象引用未设置为 if-condition 的对象实例

4

1 回答 1

0

错误是当您尝试在 HeaderTemplate 中查找控件并使用该值时,在这种情况下,值始终为空。我们应该在 ItemTemplate 中找到控件,如下所示:

protected void rptChecks_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
    {
        HiddenField hdID = (HiddenField)e.Item.FindControl("hdnCheckID");

        HiddenField hdDocOpt = (HiddenField)e.Item.FindControl("hdnDocOption");

        FileUpload fileCheck = (FileUpload)e.Item.FindControl("fileDocument");

        if (Convert.ToInt32(hdDocOpt.Value) == 0 || Convert.ToInt32(hdDocOpt.Value) == 1)
            fileCheck.Visible = true;
        else
            fileCheck.Visible = false;
    }
}
于 2013-08-08T13:32:06.977 回答