Re: Problem with Javascript in a UserControl



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.




tshad wrote:
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


.


Loading