Muhammad Ibrar
Muhammad Ibrar

Reputation: 79

Accessing Properties of checkbox from link button of another page in ASP.NET

I am creating a web application using asp.net I'd like to know how to alter properties of a checkbox of one page from another page: Eg: my Page2.aspx page has a checkbox with id checkbox1 and by default its visibility is false. Now i need to set the visibility of checkbox1 to true from another page Page1.aspx with click event of a link button with id linkbutton1. any help in this regards please?

Upvotes: 1

Views: 563

Answers (2)

Syed Mhamudul Hasan
Syed Mhamudul Hasan

Reputation: 1349

You can use this approach also

page1.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>

<body>
    <form id="form1" runat="server">
    <div>
    <asp:linkbutton ID="Linkbutton1" runat="server" onclick="Linkbutton1_Click">LinkButton</asp:linkbutton>
    </div>
    </form>
</body>
</html> 

page1.aspx.cs

protected void Linkbutton1_Click(object sender, EventArgs e)
    {
        Response.Redirect("page2.aspx?visible=1");//this is how to control the visibility 
    }

and page2.aspx.cs

protected void Page_Load(object sender, EventArgs e)
    {
        //Request.QueryString["visible"].ToString() will be same

        if (Request.QueryString[0].ToString() != "1")
        {
            CheckBox1.Visible = false;
        }
        else
        {
            CheckBox1.Visible = true;
        }
    }

Upvotes: 1

Josh Greenberg
Josh Greenberg

Reputation: 323

Since the web is stateless, each page within a .NET website or web application loads independently of one another. Therefore, you're not able to directly control elements on one .aspx from another .aspx.

However, you would be able to store the desired settings for a control when Page1.aspx posts back and then use the settings saved from Page1.aspx to load the desired settings when Page2.aspx is loaded.

I'm not a huge fan of using Session management, but something like this would work:

The following event could exist on Page One.

protected void btnPageOne_Click(object sender, EventArgs e)
{
    Session["PageTwoIsChecked"] = true;
}

Then when Page Two is loaded, you could check the session information set in Page One.

    protected void Page_Load(object sender, EventArgs e)
    {
       if (Session["PageTwoIsChecked"] != null && Convert.ToBoolean(Session["PageTwoIsChecked"]) == true)
       {
           chkPageTwo.Visible = true;
       }
    }

I hope this helps!

Upvotes: 2

Related Questions