Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# asp.net repeater FindControl不工作?_C#_Asp.net - Fatal编程技术网

C# asp.net repeater FindControl不工作?

C# asp.net repeater FindControl不工作?,c#,asp.net,C#,Asp.net,我有一个带有项目模板的转发器: <asp:Repeater ID="Repeater4" runat="server" DataSourceID="SqlDataSource1"> <HeaderTemplate> <table> </HeaderTemplate> <ItemTemplate> <

我有一个带有项目模板的转发器:

<asp:Repeater ID="Repeater4" runat="server" DataSourceID="SqlDataSource1">
            <HeaderTemplate>
                <table>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td style="text-align:center;width:15%;vertical-align:top">
                        <strong id="author" runat="server" style="color:brown;font-size:20px"><%# Eval("Username") %></strong>
                        <img src="<%# Eval("Avatar") %>" alt="Avatar" />
                        <p>Create At: <%# Eval("createAt") %></p>
                    </td>
                    <td style="margin-left:5%;width:70%;vertical-align:top">
                        <div id="contentTopic" runat="server">
                        <p><%# Eval("TopicContent") %></p>
                        </div>
                    </td>
                    <td style="width:5%;vertical-align:bottom;margin-left:5%">
                        <asp:Button id="btnSua" runat="server" Text="Update" CssClass="button" Height="30px" Visible="false"/>
                        <br />
                        <br />
                        <asp:Button id="btnXoa" runat="server" Text="Delete" CssClass="button" Height="30px" Visible="false"/>
                    </td>
                </tr>
            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:Repeater>

有什么解决方案可以解决这个问题吗?

同意@Aristos,它将在不检查PageLoad中的中继器的情况下运行,因此我使用ItemDataBound:

protected void Repeater4_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            HtmlGenericControl user =(HtmlGenericControl) e.Item.FindControl("author");
            string Username = user.InnerText.ToString();
            Button btnsua = e.Item.FindControl("btnSua") as Button;
            Button btnxoa = e.Item.FindControl("btnXoa") as Button;
            if (Session["account"] != null)
            {
                string account = Session["account"].ToString();
                if (Username == account)
                {
                    btnsua.Visible = true;
                    btnxoa.Visible = true;
                }
                else
                {
                    btnsua.Visible = false;
                    btnxoa.Visible = false;
                }
            }
        }
    }

你试过调试它吗?您是否在author变量中获取了值?它正常运行,author变量中的值也显示了。但是,它没有显示author变量=Session[“account”]项中的按钮。如果此代码不起作用,并且没有找到按钮,那么您将得到一个空异常(查看该代码)-因此,其他地方是您的问题!中继器是否在
Repeater4.items
页面加载时有任何项目一步一步地调试代码@亚里士多德:是的,确实是这样,只是按钮不起作用,但它没有null异常,所以我认为它仍然找到了控件的作者。
protected void Repeater4_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            HtmlGenericControl user =(HtmlGenericControl) e.Item.FindControl("author");
            string Username = user.InnerText.ToString();
            Button btnsua = e.Item.FindControl("btnSua") as Button;
            Button btnxoa = e.Item.FindControl("btnXoa") as Button;
            if (Session["account"] != null)
            {
                string account = Session["account"].ToString();
                if (Username == account)
                {
                    btnsua.Visible = true;
                    btnxoa.Visible = true;
                }
                else
                {
                    btnsua.Visible = false;
                    btnxoa.Visible = false;
                }
            }
        }
    }