i used in this below code in usercontrol

but error happen

because the checkbox value didn't pass

please help how to pass the checkbox value

i try <!--onclick="checkedChanged(this,'checkbox')--> this line

but the checkbox is runat=server

so different value is generated in html view source code...

so doesn't work this code...........


function checkedChanged(PanelId,ChkID)
{
if(ChkID.checked == true)
{
PanelId.style.backgroundColor="Yellow";

return true;

}
if(ChkID.checked == false)
{
PanelId.style.backgroundColor="YellowGreen";
}
return false;

}


<div id="Panel1" style="WIDTH: 64px; HEIGHT: 48px; BACKGROUND-COLOR: yellowgreen" onclick="checkedChanged(this,checkbox) runat="server">
<img id="imgcolor" style="WIDTH: 32px; HEIGHT: 31px" height="31" src="file:///\\system107\HIS\Images\seat2.jpg"
width="32" align="middle" runat="server">
<input type="checkbox" id="Checkbox1" name="Checkbox1" runat="server">
<label id="Label1" runat="server"></label>
</div>

Dani AI

Generated

Note for (and following ):

The null-reference happens because ASP.NET WebForms does not always render a server control with the same client-side id/name as the server ID. Controls inside user controls or other naming containers get prefixed ids, so document.myform.Checkbox1 will often be null. is correct that a DOM object must be referenced by its rendered name, but in WebForms that name is the control's ClientID, not necessarily the server ID (Control.ClientID).

Two straightforward fixes:

  • Pass the checkbox element itself from the checkbox onclick (put the onclick on the checkbox, not the outer div), for example pass this so the handler always gets the right element.
  • Resolve the checkbox by its rendered id using the server-side ClientID when emitting inline script or attributes. Example pattern: use document.getElementById('<%= Checkbox1.ClientID %>') from markup or from an inline script block. For ASP.NET 4+, ClientIDMode="Static" is an option to keep the id stable, but beware of duplicate ids if the control is used multiple times (ClientIDMode).

Additional notes: server-side <%= ... %> only works in inline server-rendered markup (not in an external .js file), so for external scripts emit the client id into a small inline script variable. Alternatively, find the checkbox inside the panel at runtime with standard DOM queries such as panel.querySelector('input[type=checkbox]') (see getElementById usage on MDN). These approaches avoid adding a second runat="server" form and work correctly inside user controls.

Recommended Answers

All 2 Replies

Try this :

onclick="checkedChanged(this, document.YourFormName.Checkbox1)

You need to define passed object not by type, but by name.

Hai thanks for your reply...........

i already try this code...

dcument.myform.checkbox1 is not a null object error display..

because i am not used form tag with " runat=server"

because i am used this code in one usercontrol page in asp.net..

i call this user controls page another aspx page..

so the form tag runat=server should only one form...

please help how to pass checkbox(this) value..

reply please.........

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.