user1594987
user1594987

Reputation: 107

Create a checkbox on server, handling function with javascript

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?

Upvotes: 1

Views: 444

Answers (2)

Ram Mourya
Ram Mourya

Reputation: 2548

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>

Upvotes: 2

Taha Rehman Siddiqui
Taha Rehman Siddiqui

Reputation: 2533

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>

Upvotes: 0

Related Questions