0

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:

  • Get the bool result from code behind
  • After postback keep tick (if username is available)

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

Community
  • 1
  • 1
Anicho
  • 2,647
  • 11
  • 48
  • 76
  • 3
    Please don't put stuff like "C# Asp.Net Javascript " in your titles. That's what tags are for. You only succeed in making it hard to understand your title if I have to ignore the first three words before getting to the point. – John Saunders Sep 27 '11 at 15:47
  • So I have added updatepanel around the code, this stops an actual postback and create a partial postback, so now I can just make the switch in code behind? – Anicho Sep 27 '11 at 15:49
  • @JohnSaunders +1 your comment thanks for the contructive critism. :) I will avoid in future. – Anicho Sep 27 '11 at 15:50

2 Answers2

1

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)

Community
  • 1
  • 1
deostroll
  • 11,661
  • 21
  • 90
  • 161
  • I do not need to do anything more then use UpdatePanel, which I was not doing before. thank you for your answer. – Anicho Sep 28 '11 at 08:15
0

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

}

}

Shazhad Ilyas
  • 1,183
  • 8
  • 14