1

I had created a checkbox at the server during runtime, and populated it in a table on the webpage, and I would like to make use of it to trigger some JavaScript to handle some functions in the client.

However, I'm not sure of how to trigger the function using a checkbox created in this manner.

I understand that I can trigger the function when I declare a checkbox in this syntax.

asp:CheckBox ID="Checkbox1" runat="server" Onclick="javascript:myfunction()"

However, this checkbox may or may not be required to be created on the webpage, and hence I'm unable to insert this hardcoded on the webpage

Is there any way which I can use to detect the status of this checkbox and trigger my JavaScript in the client? or if I can handle what I need to do at code_behind?

1
  • can u please share how you are creating checkbox during runtime ? I had created a checkbox at the server during runtime Commented Aug 16, 2012 at 6:32

2 Answers 2

2

suppose that we are adding CheckBox to td of an table

<table>
        <tr>
            <td id="td1" runat="server">
            </td>
        </tr>
</table>

and in the code behind,

CheckBox chkbox = new CheckBox();
chkbox.ID = "CheckBox1";
chkbox.Attributes.Add("onclick", "click_Func(this);");
td1.Controls.Add(chkbox);

and javascript will be look like this ,

<script type="text/javascript">
function click_Func(chkbox) {
 alert(chkbox.id);
}
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

you can add a script tag on the page like this

    <script type="text/javascript">
    $('#<%=Checkbox1.ClientID%>').live('changed',function(){
            // Do your work here.
    });
    </script>

7 Comments

it might be getting a compile time error because u are trying to access a checkbox which is created at runtime only.
Or you can add this javascript on the page by the server when you create the textbox by c# code ClientScript.RegisterClientScriptBlock(this.GetType(), "myScript", script, false);
Thats why I posted the second solution above. In the parameter script, you will have to put the whole script in the solution above in a string before passing here.
Its better to write in client side itself, But you must provide the class. Try $('.myRuntimeChkBoxClass') instead of $('#<%=Checkbox1.ClientID%>') . While creating checkbox you must set cssclass property with myRuntimeChkBoxClass. Hope you got the point
Writing script on the page through code is no different, the solution you provided is just another workaround.
|

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.