How do cause it to be clicked in the Page_Load event? Tried Button1.click()
I want to do the equivalent of the javascript "Form1.submit()" by either causing the buttons OnClick event to fire or by doing something similar with the form.
Hi, you have to cause the button click event. Think of the button click method, where you would put your code when the button really was clicked just as a normal method and you can call this as you want providing you pass the correct parameters! The code
below is just what you need, as you want it to happen on the page load event you simply pass the variables passed to the page load event. In the code I have a label (lbCount) and a button (btClick), when the page loads it prompts and on Yes prints hello on
the screen.
Protected Sub Page_Load(ByVal sender
As Object,
ByVal e As System.EventArgs)
Handles Me.Load
If MsgBox("do you want to say hello?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes
Then
Call btClick_Click(sender, e)
Else
'.....
End If
End Sub
Protected Sub btClick_Click(ByVal sender
As Object,
ByVal e As System.EventArgs)
Handles btClick.Click
Me.lbCount.Text =
"hello"
End Sub
I am using the above code to programmatically invoke a button's click event on the page load event. It work fine in IE but not Firefox. Any ideas on why that would be? Here's my code:
Porter
Member
7 Points
15 Posts
Programmatically causing a button Click event?
Mar 30, 2007 05:08 PM|LINK
I have a button
<asp:Button ID="Button1" PostBackUrl="some.asp" runat="server" text="Submit" />
How do cause it to be clicked in the Page_Load event? Tried Button1.click()
I want to do the equivalent of the javascript "Form1.submit()" by either causing the buttons OnClick event to fire or by doing something similar with the form.
Tom Ankers
Member
708 Points
184 Posts
Re: Programmatically causing a button Click event?
Mar 30, 2007 09:29 PM|LINK
Hi, you have to cause the button click event. Think of the button click method, where you would put your code when the button really was clicked just as a normal method and you can call this as you want providing you pass the correct parameters! The code below is just what you need, as you want it to happen on the page load event you simply pass the variables passed to the page load event. In the code I have a label (lbCount) and a button (btClick), when the page loads it prompts and on Yes prints hello on the screen.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If MsgBox("do you want to say hello?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then Call btClick_Click(sender, e) Else '..... End If End Sub Protected Sub btClick_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btClick.Click Me.lbCount.Text = "hello" End SubHaissam
All-Star
37421 Points
5632 Posts
Re: Programmatically causing a button Click event?
Mar 30, 2007 09:40 PM|LINK
What you can do is to inject a javascript function on page_load which will triger the button click event. example below
ASP.NET 1.X
if(!Page.IsPostBack)
Page.RegisterStartupScript("click","<script language=javascript>document.getElementById('"+Button1.ClientID+"').click();</script>");
ASP.NET 2.0
if(!Page.IsPostBack)
Page.ClientScript.RegisterStartupScript(this.GetType(),"click","<script language=javascript>document.getElementById('"+Button1.ClientID+"').click();</script>");
Where Button1 is the id of the button you wish to fire it's event handler
HC
MCAD.NET
| Blog |
bcollery
Member
2 Points
1 Post
Re: Programmatically causing a button Click event?
Jul 14, 2008 09:50 AM|LINK
Hi,
I am using the above code to programmatically invoke a button's click event on the page load event. It work fine in IE but not Firefox. Any ideas on why that would be? Here's my code:
Page.ClientScript.RegisterStartupScript(this.GetType(), "click", "<script type='text/javascript'>document.getElementById('" + btnMonth.ClientID + "').click(); </script>"); Thanks in advanceBarbara
ocertain
Member
2 Points
4 Posts
Re: Programmatically causing a button Click event?
Mar 04, 2009 06:41 PM|LINK
The example shown by the first responder works great in all browsers. ie: btClick_Click(sender, e);