wsun31
wsun31

Reputation: 91

How to display div on postback

#layer1 {
    width: 575px;
    height: 400px;
    background-color: #E0E0EB;
    position: absolute;
    top: 36px;
    left: 222px;
}

#layer2 {
    width: 575px;
    height: 400px;
    background-color: #E0E0EB;
    position: absolute;
    top: 36px;
    left: 222px;
    visibility:hidden;
}

And i have some controls on both of the divs....

<asp:HyperLink ID="HyperLink1" runat="server" onclick="f1();" NavigateUrl="#">Add Personal Details</asp:HyperLink>
<asp:HyperLink ID="HyperLink1" runat="server" onclick="f2();" NavigateUrl="#">Add Personal Details</asp:HyperLink>

On Click Of HyperLink i have following Code...

function f1() {
    document.getElementById("layer1").style.visibility = "visible";
    document.getElementById("layer2").style.visibility = "hidden";
}

function f2() {
    document.getElementById("layer1").style.visibility = "hidden";
    document.getElementById("layer2").style.visibility = "visible";
}

And i have a button..

<asp:Button ID="Button5" runat="server" Text="Button" />

Everything works fine when i click on HyperLink but when i CLICK on BUTTON which i hv in div2 due to postback Page Reset occurs and div1 is showing.which is true as per PostBack.But I want div2 only to displayed aftr button click.Can anyone provide me code for that...Please Help...

Upvotes: 0

Views: 1870

Answers (3)

10bit
10bit

Reputation: 83

Its been a while since I used asp but here goes.

You can store varables in a Viewstate object and check the varable upon loading the program. For example

function f1() {
    document.getElementById("layer1").style.visibility = "visible";
    document.getElementById("layer2").style.visibility = "hidden";
    ViewState("layer") = "1"

}

function f2() {
    document.getElementById("layer1").style.visibility = "hidden";
    document.getElementById("layer2").style.visibility = "visible";
    ViewState("layer") = "2"

}

When you load the page you do something similar to this

String strLayer = ViewState("layer").ToString();

if(strLayer.equals("2"))
    f2();

You may find more information here that could help http://www.dotnetuncle.com/aspnet/75_viewstate.aspx

Hope this helps.

Upvotes: 0

ovaltein
ovaltein

Reputation: 1215

If you're trying to prevent Button5 from causing postback then add onclientclick="return false;" to the control. Otherwise you can just handle the visibility of your divs in the click event handler. Something like this:

protected void Button5_Click(object sender, EventArgs e)
{
    div1.Style["display"] = "block";
    div2.Style["display"] = "none";
}

In order to access your divs in codebehind you may need to make them server controls by adding runat="server"

Upvotes: 1

Guanxi
Guanxi

Reputation: 3131

I think you know what happens when there is a postback, Page is reloaded. So, you have to handle the case that when it is postback retain the old value (IsPostback)

Upvotes: 0

Related Questions