Link to home
Create AccountLog in
Avatar of Relegence
RelegenceFlag for Israel

asked on

using "ClientScript.RegisterClientScriptBlock'


I am writing a c# application. I have a page with a "asp:LinkButton".
When pressing the linkButton I want to open another window by calling a javascript function using:

ClientScript.RegisterClientScriptBlock(this.GetType(), "openWin", "<script>openChannelsWin('newSection')</script>");

On 'openChannelsWin' function on client side, i am trying to fetch some values from controls on the screen, before opening the window, but then i get errors.

For example, I am trying to fetch the following value:
var siteName = document.getElementById("txtName").value;

The problem is that "document.getElementById("txtName")" is null and i get an exception.

The strange thing is that if I press "View Source" on my page, i do see all of the controls, but if i press the linkButton, i get an exception as if non of my controls are on the page.

What am I doing wrong here?

Thank you

My linkButton:
<asp:LinkButton ID="lnkSecChannels" runat="server" Text="Click to select channels..." OnClick="lnkSecChannels_Click"></asp:LinkButton>
My code behind function:
 protected void lnkSecChannels_Click(object sender, EventArgs e)
        ClientScript.RegisterClientScriptBlock(this.GetType(), "openWin", "<script>openChannelsWin('newSection')</script>");
My javascript function (openWin is a function i wrote):
function openChannelsWin(src)
var siteName = document.getElementById("txtName").value;            
openWin("../channels.aspx?siteName=" + siteName,'Channels','height=300')

Open in new window

Avatar of Irzana
Flag of Sri Lanka image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Relegence


Thanks, that works now.
I'd love to know why...
The RegisterClientScriptBlock method inserts the client-side script immediately below the opening tag of the Page object's <form runat="server"> element. The code cannot access any of the form's elements because, at that time, the elements haven't been instantiated yet. This explains why Text box "txtName" had a null value. The RegisterStartupScript method inserts the specified client-side script just before the closing tag of the Page object's <form runat="server"> element. The code can access any of the form's elements because, at that time, the elements have been instantiated. The choice of which method to use really depends on the "order" in which you want your script to be run by the browser when rendering the page.
Thank you very much!!