0
votes

On my ASPX page, I need a server control checkbox to pass a backend variable as a parameter in its onclick JS function. But it doesn't seem to work as expected.

Please refer the two checkboxes below:

<input type="checkbox" id="Checkbox1" onclick="ToggleMyOnly('<%=gsListID %>');" />
<input type="checkbox" id="Checkbox2" runat="server" onclick="ToggleMyOnly('<%=gsListID %>');" />

Here the Checkbox1 evaluates the value of gsListID as expected. However, Checkbox2 just passes it as is.

The only difference between these two controls is that Checkbox2 is a server control.

I have searched for a solution for this issue across many sites, but did not get an answer.

I also tried converting the Checkbox1 to an ASP checkbox as follows:

<asp:CheckBox ID="CheckboxASP" runat="server" Text="test" onclick="ToggleMyOnly('<%=gsListID %>');" />

But this also did not evaluate the server tag.

Can anyone help me know how to use the server tag in "onclick" for a server control input element? (Avoiding an ASP:checkbox preferred).

1
You cannot embed server side code within server controls. So add attribute from server side code instead of aspx page.INDIA IT TECH

1 Answers

1
votes

You should write like below:

 <asp:CheckBox runat="server" ID="chk" Text="Test" onchange="return ToggleMyOnly('<%=gsListID %>');" />

if you dont want to postback then return false from ToggleMyOnly function.

It's working from myside.