Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Problem with Javascript in a UserControl

Reply
Thread Tools

Problem with Javascript in a UserControl

 
 
tshad
Guest
Posts: n/a
 
      06-28-2006
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


 
Reply With Quote
 
 
 
 
John Prado
Guest
Posts: n/a
 
      06-28-2006
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
>
>

 
Reply With Quote
 
 
 
 
Vadivel Kumar
Guest
Posts: n/a
 
      06-28-2006
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.GetTy pe(),
"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




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
>
>

 
Reply With Quote
 
tshad
Guest
Posts: n/a
 
      06-28-2006
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

"Vadivel Kumar" <> wrote in message
news:...
> 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.GetTy pe(),
> "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
>
>
>
>
> 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



 
Reply With Quote
 
Laurent Bugnion
Guest
Posts: n/a
 
      06-28-2006
Hi,

tshad wrote:
> 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
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch
 
Reply With Quote
 
Vadivel Kumar
Guest
Posts: n/a
 
      06-29-2006
Thanks Laurent, for the explanation behalf of me.

--
Vadivel Kumar
http://www.vadivelk.net
(remove "online.")

Laurent Bugnion wrote:
> Hi,
>
> tshad wrote:
>> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Postback problem with usercontrol in usercontrol Mark van Bree ASP .Net Web Controls 1 05-30-2006 08:27 PM
accessing usercontrol from another usercontrol Phl ASP .Net 2 11-18-2004 07:33 PM
Access a control inside an usercontrol from another control inside another usercontrol nail ASP .Net 0 09-15-2004 03:55 PM
Can we use a usercontrol inside a usercontrol Rajesh Tiwari ASP .Net 0 08-12-2003 03:56 PM
Use LoadControl to load a usercontrol but the webcontrol in the usercontrol can not AutoPostBack huobazi ASP .Net 1 07-03-2003 03:14 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57