Avatar of Relegence
Relegence
Flag for Israel asked on

using "ClientScript.RegisterClientScriptBlock'

Hello,

I am writing a c# asp.net 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

ASP.NETJavaScriptWeb Applications

Avatar of undefined
Last Comment
Relegence

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Irzana

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Relegence

ASKER
Thanks, that works now.
I'd love to know why...
Irzana

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

ASKER
Thank you very much!!
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23