简体   繁体   中英

Make User Control hide/show

I have a main page: Main.aspx and 2 user controls User1.ascx and User2.ascx. First, i want User2.ascx to be invisible.I have a hidden value in the main page control. And if value of hidden value is not null then show user2.ascx. I have typed the code in the prerender function on user2.ascx.

Currently, what I try

In Main.aspx

<usercontrol:User1 runat="server" ID="user1control" Visible = "false"  />

By this, In User2, it comes only in pageload event but not in OnPreRender.

I have my all code in OnPreRender

Try something like this

protected void btnToggle_Click(object sender, EventArgs e)
    {
        string s = btnToggle.Text;

        switch (s)
        {
            case "Hide":
                btnToggle.Text = "Show";
                break;
            case "Show":
                btnToggle.Text = "Hide";
                break;
        }
        ucDetails myControl = (ucDetails)Page.LoadControl("~/ucDetails.ascx");
        UserControlHolder.Controls.Add(myControl);
        myControl.Visible = !myControl.Visible;
    }

The other option you can create this on the javascript side. Where you can wrap your usercontrol in a panel and hide and show through javascript function. Hiding and showing through css.

<script type="text/javascript">
    function hide() {
        document.getElementById("ResultPanel2").style.display = 'none';
    }
</script>

I Solved it. I kept a hidden field in main page, and in the load event of main page, I kept a contion that if the value is not null or empty then, user2.visible = true . This worked for me.

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