Problem with Javascript in a UserControl

T

tshad

I get an error when running my Javascript inside my UserControl. It works
fine if I put the UserControl Data directly in my Web Page.

The stripped down code is:

<script language=javascript>
function CheckQuestion()
{
var checkBox = document.getElementById('SecurityStandard');
alert("checkBox.checked = " + checkBox.checked);
}
</script>

Mozilla doesn't seem to have a problem, but IE gives me this error:

Error: Object required.

The object in the UserControl is:

<asp:RadioButton ID="SecurityStandard" Checked="true"
GroupName="SecurityQuestion" runat="server" Text=" Secret Question:"/>

From View Source:

<input id="_ctl0_SecurityStandard" type="radio"
name="_ctl0:SecurityQuestion" value="SecurityStandard" checked="checked"
onclick="return CheckQuestion();" />

On the page that does work:

<input id="SecurityStandard" type="radio" name="SecurityQuestion"
value="SecurityStandard" checked="checked" onclick="return CheckQuestion();"
/>

For some reason the radio buttons Name is appended with "_ctl0:" in the
UserControl and nothing is appended in the regular page.

I am used to seeing this from DataGrids, but not as a regular control on the
page.

How would I hande the Javascript in this case?

Thanks,

Tom
 
J

John Prado

Make a look at the HTML generated by your code and find your checkbox ID.

Did it looks some diferent from the original??? Sure it is.

The framework add the name of the control to input ids in generated HTML.

That's why your javascript don't works.
 
V

Vadivel Kumar

The reason this script doesn't work is because of you are using an user
control. Generally, .NET appends this _ct* prefix with the controls that
placed in a UserControl and UserControl placed in a page, which is to
make uniqueness among the controls present in the UserControl and in the
Page.

The solution to this problem is you have to place this script in your
code behind file's Page_Load event. Check the below code.


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

String _Script = "function CheckQuestion()";
_Script += "{";
_Script += "var checkBox = document.getElementById({0});";
_Script += "alert(checkBox.checked)";
_Script += "}";

// Assume you have the RadioButton already defined
_Script = String.Format(_Script, SecurityStandard.ClientId);

Page.ClientScript.RegisterStartupScript(this.GetType(),
"CheckBox_Script", _Script, true);

}

}

For your understanding, the trick part is

_Script = String.Format(_Script, SecurityStandard.ClientId);

I am just updating the ASP.NET generated control id to the script,
instead of the one it has in the design time. By using
RegisterStartupScript I am registering the script to the client.

Let me know if you have further issues with the code.

-
Vadivel Kumar
http://vadivelk.net

Note: This solution will only work in .NET 2.0, Let me know if you are
looking for .NET 1.1
 
T

tshad

Why do you have to Register the script? I use Javascript all the time and I
don't register it.

Also, how does document.getElementById({0}) get you the Radio button?

I actually have about 15 objects (texboxes and other radio buttons). How
does it know what control you are talking about?

What actually prepends the "_ctl0:" to the ID?

Thanks,

Tom
 
L

Laurent Bugnion

Hi,
Why do you have to Register the script? I use Javascript all the time and I
don't register it.

When you "register" a script in a Page instance, you make sure that this
script will appear only once on the page, even if there are more
instances of the same control. If you don't register the script, and if
you have more than one instance of the same control, then each instance
will generate the same script, which is not needed, might cause unwanted
effects, and makes the response bigger for no reason.
Also, how does document.getElementById({0}) get you the Radio button?

document.getElementById takes a string (ID) as parameter. However, the
ID must be carefully used, because in a control, it might not be equal
to the ID set in the code. Once again, if you have more than one
instance of the same control, the ID must be unique. This is why ASP.NET
generates a unique ID, which is accessible with the ClientID property.

The syntax String.Format( "document.getElementById({0})",
SecurityStandard.ClientID ) will simply replace the {0} element with
the first parameter after the string, in this case the ClientID of the
SecurityStandard control.
I actually have about 15 objects (texboxes and other radio buttons). How
does it know what control you are talking about?

I don't understand your question.
What actually prepends the "_ctl0:" to the ID?

See my explanation above: It's ASP.NET because of the need for the ID to
be unique.

Thanks,

Tom

HTH,
Laurent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top