locked
ToggleButton doesn't toggle when set in code RRS feed

  • Question

  • User-298487420 posted

    I have a toggle button that works fine, applied to a CheckBox (ToggleButtonExtender).  When I click it, the images change and everything works fine.  I have a situation where I need to set the Checked property to true in code.  When I do this, the ToggleButton doesn't reflect this change visually even though the CheckBox is "checked".  How do you force this change visually to make it programmatically change its images in accordance with the state of the CheckBox?

    Its as if the ToggleButton(Extender) doesn't respond to programmatic changes, only click events.  How do you circumvent this?

    Thanks,

    -D

    Wednesday, February 5, 2014 11:57 AM

Answers

  • User336673788 posted

    for c#

    you would do this in your page load function. the code below will register the javascript and set your checkbox to checked.

    string strScript = "document.getElementById('" & this.chkSendInvitation_1_4.ClientID & "').checked = true;";
    System.Web.UI.ScriptManager.RegisterStartupScript(this.chkSendInvitation_1_4, this.chkSendInvitation_1_4.GetType(), "ToggleButtonExtender_JS", strScript, True);
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 7, 2014 5:56 PM

All replies

  • User336673788 posted

    put the code below in your page load.

    		strScript += "document.getElementById('" & Me.opnToggle.ClientID & "').checked = true;"
    
    		System.Web.UI.ScriptManager.RegisterStartupScript(Me.opnToggle, Me.opnToggle.GetType(), "ToggleButtonExtender_JS", strScript, True)

    Wednesday, February 5, 2014 7:46 PM
  • Wednesday, February 5, 2014 11:04 PM
  • User-298487420 posted

    If my CheckBox (with the ToggleButtonExtender) is called chkSendInvitation_1_4 and the ToggleButtonExtender is called tbxSendInvitationExtender how does it relate to the above code?  Also, I'm using C# (sorry for not clarifying this) although any example I can probably convert once I know how the above names in my code relate to the RegisterStartupScript code you posted...

    Thursday, February 6, 2014 11:14 AM
  • User336673788 posted

    for c#

    you would do this in your page load function. the code below will register the javascript and set your checkbox to checked.

    string strScript = "document.getElementById('" & this.chkSendInvitation_1_4.ClientID & "').checked = true;";
    System.Web.UI.ScriptManager.RegisterStartupScript(this.chkSendInvitation_1_4, this.chkSendInvitation_1_4.GetType(), "ToggleButtonExtender_JS", strScript, True);
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 7, 2014 5:56 PM
  • User-298487420 posted

    Thanks for your response, this seems to get me closer.

    This code definitely changes the state of the button, however doing it int he Page Load is going to be hard because I haven't run the code that actually does anything with the databsa yet.  Its almost like I need to manually set this to true and false through the client code you've provided.  This code defintiely changes the state but I might need a more manual solution?  Do you have an examples of how to set these toggle button images exactly manually?

    Its possible I am doing something wrong as well... the above code does what I need it just doesn't do it based on the database.  My DB code iterates through some stuff while building the page dynamically...

    Thanks,

    D

    Wednesday, February 12, 2014 1:14 PM
  • User336673788 posted

    I don't have a full view of your code or how you build your page. Are there many togglebuttons? If you create the togglebutton at runtime, instead of setting the checkbox.checked = true; you can also use css.

    at run time you can set it: this.checkeboxID.attributes.add("checked",true);

    Thursday, February 13, 2014 12:40 PM