3

Here is my problem :

I want to manage the state of a checkbox in a webform page using visual studio 2010 to which i have reduced the code to the strict minimum for you to better understand my problem.

In order to get the state of my checkbox dynamically created i have tried severals things :

  • At the beginning my wish was simply : get the state of my checkbox in code-behing which is for my case the c# but i quickly realised that i needed to put the attribute runat="server" to the spans that contains the state of my checkbox
  • So i have changed the dynamic declaration of the spans and i have added the attributes "id" and "runat" to each of the two spans. After, these attributes were visible in the code source when the page is generated but i still couldn't manage them in the code-behind
  • I finally tried to use the DOM function : getElementById but when i check with the debugger, the variable is always null

In fact i think i just need to act when the page is fully loaded but i don't know how.

I just need to know if my checkbox is on or off.

Here is the body content :

<body>
    <form id="form1" runat="server">
          <div class="checkbox1" id="CB1">
            <input type="checkbox" value="1"/>
          </div>     
    </form>   

    <script type="text/javascript">
        $(function () {     

            $('input[type=checkbox]').switchButton({
                button_width: 10,
                labels_placement: "left",
                height: 22,
                width: 50
            });

        })        
    </script>
</body>

The jquery script is there : Jquery

The css for the particular checkbox is there : Css

Let me know how to make this work.

3
  • Hi, could you provide your try with GetElementById? btw, don't you need a ; after width: 50? Commented Dec 11, 2013 at 11:06
  • Hi thanks for answering, Here is the Screenshot and no, i don't need a ; after width because these are only parameters of switchButton managed by jQuery Commented Dec 11, 2013 at 12:27
  • Ok, next time try this for inline link in comment because your link lead to an error, got to remove the last ] in the url Commented Dec 11, 2013 at 12:29

3 Answers 3

1

switchButton (which I've developed) doesn't delete or modify the initial checkbox input you apply the plugin to...

So I suggest you create your checkbox just like you would in any other project and get its state through C# code...

I guess that's what other people have suggested here already.

Sign up to request clarification or add additional context in comments.

1 Comment

Finally it's working, i didn't understood that your switchButton doesn't modify the initial checkbox input. I have tried too many things for make this example out but i didn't analyse correctly the switch button behavior So the solution is simply to put an <asp:CheckBox ID="CheckBox1" runat="server" /> instead of a simple <input type="checkbox" value="1"/>
1

Possible you need to do an Async Postback to your code behind Updating the Control Tree of your Page with the newly added Element. Otherwise C# has no way of knowing you add an element with jQuery.

The code should look like that

    TextBox TitleTxtBox = new TextBox();
    TitleTxtBox.ID = "TitleTxtBox";

    this.Controls.Add(TitleTxtBox);

There is an article here that will help:

http://weblogs.asp.net/infinitiesloop/archive/2006/08/30/TRULY-Understanding-Dynamic-Controls-_2800_Part-3_2900_.aspx

Another possible solution might be to request Request.Form to get the newly added element but i'm not sure if it works. Can't check it right now.

  if (Request.Form["checkboxName"] != null && Request.Form["checkboxName"] == "on")
        {
            Response.Write("This checkbox is selected");
        }
        else
        {
            Response.Write("This checkbox is not selected");
        }

http://forums.asp.net/t/1440174.aspx

Hope the above will help you.

Comments

0

You could create the checkbox as asp:checkbox then on client side using clientID call switchButton function on it.

If you don't know how many checkboxes you will need, then you can create the checkboxes with name attribute and on server side access it like this Request["checkBoxName"].

You can do something like this to create many checkboxes and get their values easily:

Dictionary<string, bool> data = new Dictionary<string, bool>();
foreach (var item in Request.Params)
{
    //code_chkId_001, true
    if (item != null && item.ToString().StartsWith("code_"))
    {
        data.Add(item.ToString().Replace("code_", "")/*chkId_001*/ ,
                Convert.ToBoolean(Request[item.ToString()]) /*true*/); 
//you have to check here, if it's "on" instead of "true" ToBoolean will break.
    }
}

I know this is like a hack, but I hate Asp.Net's postback and mainly use AJAX. Hope it helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.