Generating Javascript from a button in Code-behind

N

Nevyn Twyll

I have a web form, with c# code-behind.
I have a listbox on the form, bound to a dataset.
I want to have 2 buttons/hyperlinks/etc. beside the listbox.
When they are clicked, I want to launch a popup form, passing it some
information from the listbox (record id) and the linked information.

If I could show the data in place of the page showing, I know how to get the
data and use Response.Redirect().
Since it needs to be a popup, I know how I could use a javascript, such as:
window.open("mynextpage.aspx","_blank", "height=300, width=450,
left=100, top=100, " + "location=no, menubar=no, resizable=no, " +
"scrollbars=no, titlebar=no, toolbar=no", true);

I know that in my PageLoad() method, I could add an Onclick() method to a
hyperlink, using the .

But I don't want to do a roundtrip every time the user clicks an entry in
the listbox.

So is there some way a hyperlink button could, in the code-behind put a
javascript command into the html re-emited to the user?

Thanks!
 
B

Brian K. Williams

Add an Attribute in the code-behind.
txtFirstName.Attributes.Add("onclick","fnJavaScriptCall()");

You could also write the client script in the code-behind like this:
public void writeScript()
{
string script = "<script language=\"javascript\">";
script += "function fnJavaScriptCall(){";
script += " Code...";
script += " }";
script += "</script>";
this.Page.RegisterStartupScript(script);
}

Regards,
-Brian K. Williams
 
N

Nevyn Twyll

Okay, but how would I get the ID Value of the currently selected row in the
listbox in order to use it in the redirect of the javascript function?
That's what I don't quite get how to do.
 
B

Brian K. Williams

This should do the job..

DropDownList1.Attributes.Add("onchange","fnGetSelectedValue(this)");

function fnGetSelectedValue(oElement)
{
var strSelectedValue = oElement.options[oElement.selectedIndex].value;
alert(strSelectedValue);
}

Or

DropDownList1.Attributes.Add("onchange","fnGetSelectedText(this)");

function fnGetSelectedText(oElement)
{
var strSelectedValue = oElement.options[oElement.selectedIndex].text;
alert(strSelectedValue);
}


-Brian
 
N

Nevyn Twyll

That looks great.
However, how do I generate the code I want dynamically, in the code-behind?

Basically, I want the user to have free-reign with the listbox on the client
side, and when they press a button (hyperlink button, etc.), I want to pop
up a window with a bunch of information from the line they selected in the
listbox, which is easy to get to from the code-behind?

Should I just write a javascript method to do it anyway, and if so, how do I
access an asp control from javascript?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top