By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,740 Members | 844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,740 IT Pros & Developers. It's quick & easy.

Fire Code behind code AND Javascript code associated to a Button Click Event

P: n/a
I have a button that opens a new Window (some kind of search-window), which is fired using JavaScript (btnSearch.Attributes["onclick"]=".....";)
Now I need to run some code behind code BEFORE this JavaScript runs. I tried to define another Button with an EventHandler associated, and from this (after doing my code) fire 'btnSearch.click()'. But I can't make it work.
I tried
private void Button1_Click(object sender, System.EventArgs e

RestartAll()
Response.Write("<script>window.document.forms[0].elements['btnSearch'].click();</script>")

but unfortunately the browser says that this element is not valid
Am I on the right way or are there better options to do what I try to accomplish
Thanks for any help
Nov 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Carlo
In order to do what you want you can call a Javascript function to load a new window after you handled the post back event of your search click button

Don't attach any javascript function to the search click event. Instead handle the event on the postback like you usually do. In the postback handler add a call to a javascript function that opens a window.

Add Page.RegisterStartupScript("<script language=\"JavaScript\">OpenMyWindow();</script>"); in your btnClick handle
method

When your aspx page re-renders on the browser after postback it will execute your Javascript function

HTH
Suresh

----- Carlo Marchesoni wrote: ----

I have a button that opens a new Window (some kind of search-window), which is fired using JavaScript (btnSearch.Attributes["onclick"]=".....";)
Now I need to run some code behind code BEFORE this JavaScript runs. I tried to define another Button with an EventHandler associated, and from this (after doing my code) fire 'btnSearch.click()'. But I can't make it work.
I tried
private void Button1_Click(object sender, System.EventArgs e

RestartAll()
Response.Write("<script>window.document.forms[0].elements['btnSearch'].click();</script>")

but unfortunately the browser says that this element is not valid
Am I on the right way or are there better options to do what I try to accomplish
Thanks for any help
Nov 18 '05 #2

P: n/a
Carlo,

I have a little bit of sample code on my website, www.aboutfortunate.com,
that might be just what you need. It shows how to make the "Body" tag of the
page into a server side control. Once this is done you can add a javascript
to the Body tag so that when a page loads that script fires. This technique
would do exactly what you need.

To find the sample code click the "Code Library" link in top right corner of
the site and then use the "Search" box there to search for something like:
"javascript on page load"

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
"Suresh" <an*******@discussions.microsoft.com> wrote in message
news:EA**********************************@microsof t.com...
Carlo,
In order to do what you want you can call a Javascript function to load a new window after you handled the post back event of your search click
button.
Don't attach any javascript function to the search click event. Instead handle the event on the postback like you usually do. In the postback
handler add a call to a javascript function that opens a window.
Add Page.RegisterStartupScript("<script language=\"JavaScript\">OpenMyWindow();</script>"); in your btnClick handler method.

When your aspx page re-renders on the browser after postback it will execute your Javascript function.
HTH,
Suresh.

----- Carlo Marchesoni wrote: -----

I have a button that opens a new Window (some kind of search-window), which is fired using JavaScript (btnSearch.Attributes["onclick"]=".....";). Now I need to run some code behind code BEFORE this JavaScript runs. I tried to define another Button with an EventHandler associated, and from
this (after doing my code) fire 'btnSearch.click()'. But I can't make it
work. I tried:
private void Button1_Click(object sender, System.EventArgs e)
{
RestartAll();
Response.Write("<script>window.document.forms[0].elements['btnSearch'].click
();</script>"); }
but unfortunately the browser says that this element is not valid.
Am I on the right way or are there better options to do what I try to accomplish ? Thanks for any help

Nov 18 '05 #3

P: n/a
Hi Carlo,
Thanks for posting in the community!
From your description, currently you add a clientside "onclick" event for a
server button which will open a ne w browser window. However, now you'd
like to do some serverside operations before execute the clientside
script(open a browser window) yes?
If there is anything I misunderstood, please feel free to let me know.

As for this question, I quite agree to Suresh's suggestion that we can
register the clientside script(open a new browser window) in the certain
button's serverside click even handler after you've finished some certain
operations. Then, after the page turns back to client, the new browser
window will be opened. For example, the button's server click event is as
below:
private void btnSearch_Click(object sender, System.EventArgs e)
{
//do some operations here

string script = "<script
language=\"JavaScript\">window.open('http://www.google.com');</script>";
Page.RegisterStartupScript("tempscript",script);
}
To make it clearly, I've made a sample page to show this means, you may
have a look if you feel anything unclear above:
-------------------aspx page------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm2</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
<script language="javascript">
function checkField()
{
if(document.all("txtMain").value.length>4)
{
document.all("btnPostBack").click();
}
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="500" align="center">
<tr>
<td><FONT face=""></FONT></td>
</tr>
<tr>
<td><FONT face="">
<asp:Button id="btnSearch" runat="server"
Text="Search"></asp:Button></FONT></td>
</tr>
</table>
</form>
</body>
</HTML>
-----------------code behind class --------
public class WebForm2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnSearch;

private void Page_Load(object sender, System.EventArgs e)
{
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion

private void btnSearch_Click(object sender, System.EventArgs e)
{
//do some operations here
Response.Write("<br>Page is posted back at: "+
DateTime.Now.ToLongTimeString());
string script = "<script
language=\"JavaScript\">window.open('http://www.google.com');</script>";
Page.RegisterStartupScript("tempscript",script);
}
}
---------------------------------------------

Please check out the above things. If you have any further quesitons or
need any help, please feel free to post here.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #4

P: n/a
A lot of thanks to all of you - it works great.
Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.