473,480 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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
4 5519
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
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
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
A lot of thanks to all of you - it works great.
Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1315
by: Big_Stu | last post by:
Hi all, I have an <asp:button> inside an <asp:table> and I am finding that the button click event doesn't fire at all! I have added the button to the initialize component event, and the table. ...
4
4007
by: Mark Lingen | last post by:
I've found a problem with postback event handling and webcontrol buttons. Try out the following code in an ASP.Net project and you will see. Create a web project in VB.Net and drop this code...
3
52205
by: Steve Kershaw | last post by:
Hi, I need to fire a server side button click event from my client side javascript. The client side javascript code follows: <script language="javascript" type="text/javascript"> function...
0
1156
by: TCook | last post by:
Hey All, I'm trapping a 'submit' button's 'Click' event in a VB.Net code behind class in order to loop through a 'Select' controls 'option' list as follows: For Each ThisItem In...
17
9615
by: Eric | last post by:
I'm new to JavaScript and I wrote this code to play with. Oddly, if I enter text in a box and then press the button, I only get the onChange event for the text box and not the button's onclick...
1
1845
by: spwanxander | last post by:
The big problem that I am having is that I have a asp.net support manager website that I am building. One of my pages has a couple of buttons on it. The first button is and "add" or can be changed...
1
3912
by: aawhan | last post by:
I have a master page and a content page in my web page(.net 2008).In the master page i am having some menus.Now after doing some operation in the page if i will click to any other menu then it should...
0
2858
by: ADN | last post by:
Hi, I am currently extending the GridView control and would like to add a button to the GridView so that it will automatically render one button at the top of the grid. I have a click event for...
19
4532
Frinavale
by: Frinavale | last post by:
I'm in the middle of implementing a custom Ajax enabled Server Control. At this point I need help finding the answer to an Ajax Framework question...here it goes: I have a Server Control that...
4
6987
yarbrough40
by: yarbrough40 | last post by:
can any one tell me if there is a way to pass a string variable to a button click event? ultimately I am looking to fire client side Javascript "__doPostBack('Button1','click','')" and have the...
0
7037
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7034
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
6886
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4768
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4472
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2990
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1294
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.