简体   繁体   中英

How to call CodeBehind sub from an EmptyDataTemplate ( C# )

What I want to do is hide the button (btnQuery) that initiated the query with no results, and make visible another button that reloads or redirects the page. What I have here seems to run, breakpoint travels through the sub but the button state does not change... Any hints/suggestions?

aspx frontend

<EmptyDataTemplate>
    <h1> No records found !!! </h1>
    <br />
    <%SwitchButtons();%>
    <asp:Button id="btnReturn" runat="server" Text="Return" onclick="btnReturn_Click" />
</EmptyDataTemplate>

aspx.cs codebehind btnReturn set Visible = false in PageLoad

protected void SwitchButtons()
{
    btnQuery.Visible = false;
    btnReturn.Visible = true;
}

You can't access template item directly via ID . Read MSDN article : Locating a Control Inside a Hierarchy of Naming Containers .

protected void SwitchButtons()
{
    btnQuery.Visible = false; // Where is btnQuery?
    Button btn1=(Button)FindControlRecursive(GridView1,"btnReturn");
    btn1.true;
}

private Control FindControlRecursive(Control rootControl, string controlID)
{
 if (rootControl.ID == controlID) return rootControl;
    foreach (Control controlToSearch in rootControl.Controls)
    {
    Control controlToReturn =
         FindControlRecursive(controlToSearch, controlID);
    if (controlToReturn != null) return controlToReturn;
    }
   return null;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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