2

I have a radiobox <asp:RadioButtonList CssClass="list" Style="width: 150px" ID="rdo_RSD_ExcerciseRoT" runat="server" Font-Bold="false" RepeatDirection="Horizontal" RepeatLayout="Table" TextAlign="Left" > <asp:ListItem Text="Yes" onclick="en();" Value="Y"></asp:ListItem> <asp:ListItem Text="No" onclick="dis();" Value="N" Selected="True"></asp:ListItem> </asp:RadioButtonList>

AS you may see second listitem is selected by default. But issue is, when my page is getting load dis() is not getting called. I want to run dis() on page load too.

I tried google, some blogs suggest the use of Page.RegisterStartupScript Method. But I dont exactly know what is the problem and why we should use this above mentioned method. I would appreciate if someone please tell me why this function is not getting called and how to call it.

Edit: I am giving Javascript code also, if it helps.

    <script type="text/javascript">
    function dis()
    {
        ValidatorEnable(document.getElementById('<%=RequiredFieldValidator32.ClientID%>'), false);
    }

    function en()
    {
        ValidatorEnable(document.getElementById('<%=RequiredFieldValidator32.ClientID%>'), true);
    }


</script>
4
  • is your "dis()" a piece of C# code or JavaScript code? Commented May 26, 2010 at 5:41
  • @Sefler it is Javascript Code. You may see the Edit Commented May 26, 2010 at 5:45
  • Is this a javascript question, or an ASP question? Commented May 26, 2010 at 5:45
  • @misterMatt I exactly dont know. I guess its both. Commented May 26, 2010 at 5:50

5 Answers 5

1

Default list items are SET by default. They don't get CLICKED on page load to select them. So this is the reason the function defined for OnClick is not called.

You coud either include a script - Tag in that you call dis() or you could use RegisterStartupScript.

The script tag would look like:

<script type="text/javascript">
  dis();
</script>

And would have to be included in the page's html source.

RegisterScriptControl does this internally. It also has the option to gather several startup script calls and renders them in a single script-tag.

Sign up to request clarification or add additional context in comments.

1 Comment

What do you mean by "You coud include a script - Tag in that you call dis()" And what exactly is the function of RegisterStartupScript.
1

dis() is assigned to the onclick - meaning it'll be called only when the list item is clicked on by the user. You can call dis() from the onload of the <body> if you want.

1 Comment

@Sebastian I assume you meant to put this comment on your post
0

If you want to execute C# code on Server side on page load, overide

 protected void Page_Load(object sender, EventArgs e)

If you want to execute JavaScript on page load, use "onLoad" in "<body>" tag. like

<body onload="dis();">

Hope it helps.

2 Comments

I am using Master Pages. So I think the Body Tag would be in the Master Page. So how would I be able to write "onLoad" for this particular .aspx page
@vaibhav I think you may be able to use the <script> tag instead.
0
<body onload="dis()">

</body>

done.

Comments

0

You need to run the script when the page loads because no one has clicked your list item yet. You should use Page.RegisterStartupScript vs <body onload="..."> to do this for three reasons:

  1. Asp.net may want to use the normal <body onload="..."> script for something else.
  2. You can control whether or not you want the script to also run on postbacks
  3. RegisterStartupScript can protect you from naming conflicts (say, a custom control you use wants to run a script with the same name at startup as well) and from accidentally setting the script to run more than once.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.