Anicho
Anicho

Reputation: 2667

Get variable & keep changes after postback

This question is related to: Hide div on clientside click

The issue I am having is that after postback event from asp.net happens onClick any clientside changes made reset how can I keep the client side changes I am making.

Second question how can I get a variable from code behind and pass it into my javascript to perform a comparison.

Html:

    <div runat="server" id="someDiv1" enableviewstate="true" >
        <asp:LinkButton OnClientClick="Show_Hide_Display()" 
                        ID="lbtnDiv1" 
                        runat="server" 
                        CausesValidation="true" 
                        OnClick="lbtn_onClickServer">
        </asp:LinkButton>
    </div>

    <div runat="server" class="tick" id="div2" style="display:none;" enableviewstate="true">
    </div>

Javascript:

<script type="text/javascript">

function Show_Hide_Display() {

    var div1 = document.getElementById("<%=someDiv1.ClientID%>");
    var div2 = document.getElementById("<%=div2.ClientID %>");

        if (div1.style.display == "" || div1.style.display == "block") {
            div1.style.display = "none";
            div2.style.display = "block";
        }
        else {
            div1.style.display = "block";
            div2.style.display = "none";
        }
}    

</script>

The OnClick event causes a postback like it should, on this occassion it checks if users, chosen username is available.

If it is available show a tick, if it isn't error.

I got the error working and am trying to program the tick on client side.

So OnClientClick I am able to toggle between some text and a tick. So I need to:

I am almost there but can't quite figure the last two points out.

Upvotes: 0

Views: 2719

Answers (2)

deostroll
deostroll

Reputation: 11995

If you are using an UpdatePanel in your page, and assuming that div which you are trying to toggle is outside the control, you can always inject javascript on a partial postback:

Like for e.g. on your button's click event which executes on a partial postback make a call to ScriptManager.RegisterClientScriptBlock() --> How to retain script block on a partial postback?

Alternatively, you can append an end request handler. This is some javascript which should run after the partial postback. --> ASP.NET Register Script After Partial Page Postback (UpdatePanel)

Upvotes: 1

Shazhad Ilyas
Shazhad Ilyas

Reputation: 1193

The answer for the both questions lies of checking the boolean value send from the code behind.

1-----.in code-behind c#

protected void Page_Load(object sender, System.EventArgs e) {

var linkbtn = (Button)Page.FindControl("lbtnDiv1");

linkbtn .Attributes.Add("onClick", "Show_Hide_Display('" + parameter+ "')");

}

2------- change your javascript

function Show_Hide_Display(parameter)

{

if( paramater=='true') {

----your logic---

}

else

{

----your logic

}

}

Upvotes: 0

Related Questions