How to call click event of button apart from clicking on the button.

I want to call click event event of my button and run codes within the event, not clicking on the button itself. For instance in Page_Load event of my page i want to call it. How can i handle this?

Thanks in advance.

0
kem06
3/24/2008 8:36:26 AM
asp.net.web-forms 93655 articles. 6 followers. Follow

11 Replies
1631 Views

Similar Articles

[PageSpeed] 41
Get it on Google Play
Get it on Apple App Store

Hi,

Try this

 

Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load
     If Not IsPostBack Then
          Me.Button1_Click(sender, e)
     End If
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
     Response.Write("Hello World!!!")
End Sub

 


Faraz Shah Khan
MCP, MCAD.Net, MCSD.Net, MCTS-Win/Web, MCPD-Web
Blog
0
farazsk11
3/24/2008 9:11:23 AM

Hi,

Use this code. Add it to your Page_Load()

C#

Page.ClientScript.RegisterStartupScript(this.GetType(), "onclick", "<script language=javascript>document.getElementById('" + Button1.ClientID + "').click();</script>");

VB.NET

 Page.ClientScript.RegisterStartupScript(Me.GetType(),"onclick","<script language=javascript>document.getElementById('" & Button1.ClientID & "').click();</script>")

Javascript

var b = document.getElementById(button1);
b.click();

 


HTH,
Suprotim Agarwal


ASP.NET Grid With Excel Capabilities | 27 GridView Tips & Tricks | Hosting your site? | SQL Server Tips
0
Suprotim
3/24/2008 9:21:26 AM

Hi,

The above one is perfect and the following one is for C# .net if you are from C# programmers:

protected void form1_Load(object sender, System.EventArgs e)
{
    if (! IsPostBack)
    {
         this.Button1_Click(sender, e);
    }
}

protected void Button1_Click(object sender, System.EventArgs e)
{
       Response.Write("Hello World!!!");
}


Hope this will help!

Nepster from India Yes
Note: Please mark this post as answer if it help you.
0
nepster_from_india
3/24/2008 9:22:47 AM

Well, i don't know any visual basic. I code using C#

0
kem06
3/24/2008 9:26:58 AM

Hi,

I don't understand your question, Why u want to call an event in page_load.  Instead of that you can put all the functionality in a method and call that in the page_load or in any method.

 

 


radha
0
Vellaiachi
3/24/2008 9:28:04 AM
Man, I have given the code for c# in the previous post, just check it (Before your deman Embarrassed)
Hope this will help!

Nepster from India Yes
Note: Please mark this post as answer if it help you.
0
nepster_from_india
3/24/2008 9:39:48 AM
Hi, 
Here is the C# Code for the same

protected void Page_Load(object sender, EventArgs e)
{
    if (! IsPostBack)
    {
         Button1_Click(sender, e);
    }
}

protected void Button1_Click(object sender, EventArgs e)
{
      //Your code goes here...
}


Virendra (MCTS)

My SiteBlog
0
virendra1983
3/24/2008 9:42:06 AM

nepster_from_india:
Man, I have given the code for c# in the previous post, just check it (Before your deman Embarrassed)

 While sending my post, there were no posts yet on my browser ;) After my post approved, i saw the other codes(in C#) posted. It is ok.

0
kem06
3/24/2008 9:48:03 AM

I  portrayed my problem a litle bir wrong. Actually i have no a click event for my button. Yet i added a javascript function as attribute to my button's onclick event and i wanted to run this on PAge_Load event of my page.

Suprotim Agarwal 's post already helped. Thanks, all people posted here.

0
kem06
3/24/2008 9:51:17 AM

Suprotim Agarwal:

Hi,

Use this code. Add it to your Page_Load()

C#

Page.ClientScript.RegisterStartupScript(this.GetType(), "onclick", "<script language=javascript>document.getElementById('" + Button1.ClientID + "').click();</script>");


HTH,
Suprotim Agarwal

Thanks for answring, but i have one more question. The code above worked fine, yet when i wantto use it in another methods, i get the following error message:

Sys.InvalidOperationException: A control is already associated with the element. How could i avoid this?

0
kem06
3/25/2008 1:22:32 PM

Hi,

Post some code to shown how are you using the code in a different method. Also, try changing the key name "onclick" to something else if you are using RegisterStartUpScript for a second time for the same control.

HTH,
Suprotim Agarwal


ASP.NET Grid With Excel Capabilities | 27 GridView Tips & Tricks | Hosting your site? | SQL Server Tips
0
Suprotim
3/26/2008 2:32:32 AM
Reply:

Similar Artilces:

button click event from another button click not firing
 Hi,  I want to trigger a button click event from another button click .It is not firing.  Please help.  protected void btnReview_Click(object sender, EventArgs e) {       if (MandatoryValidation())        {         this.Button1.Click += new System.EventHandler(this.Button1_Click);       } }protected void Button1_Click(object sender, EventArgs e) { } protected void btnReview_Click(object sender, EventArgs e){   &...

Dynamically loaded Web User Control .ascx button click event handler not called on second time click
I have an ascx control (that performs actions for an entity) being added dynamically to accordion panes, and then the panes are added to an ajax accordion control, so that the accordion can contain multiple panes with each pane containing one ascx control. The ascx control contains a delete button which removes the entity from the database. Once the entity is removed, I need to reflect this deletion in the parent page by removing the accordion pane from the accordion. To do this I created a delegate function and which is called from the parent page to rebind the accordion and hence not d...

Call a buttons click event from another button??
Hi, in asp.net vb.net how do you call a buttons click event from another procedure?In standard vb it is easy not sure what to do in .net though because of the parameters.Thanks for the help. Graham.<code> Private Sub CloseBtn_Click(ByVal sender As System.Object, ByVal e As   System.Web.UI.ImageClickEventArgs) Handles CloseBtn.Click ' close the browser window.HttpContext.Current.Response.Write("<script>window.close();</script>") End Sub Private Sub ProcessBtn_Click(ByVal sender As System.Object, ByVal e As  System.Web.UI.ImageClickEventArgs) Handles Pro...

update form: page_load event is called before button click event
Hi all,Very simple question I think.I've got an useraccount_detail.aspx page that can both serve to insert a new record into a table and update an existing one.So when I detect an ID paramater in the request I know it's an update and the save button click event performs an update operation.But when I click the save buton the page_load event gets called first. I initialize my objects there so basically it gets the existing object, not the one with the changed attributes and save it. Nothing has been changed as a result. Of course when I use the "if not page.ispostback" condition in the page_l...

Calling a button click event from a menu item click
I'm trying to transition from MFC to this and its going none too smooth. I have this where the button calls a java script function. I want the menu item to do the same. This was easy in MFC. protected void Page_Load(object sender, EventArgs e) { Button1.Attributes.Add("OnClick", "return getFile();"); } protected void Button1_Click(object sender, EventArgs e) { } protected void OnMenuItemClick(object sender, MenuEventArgs e) { }  Handle the MenuItemDataBound event of the Menu and attach it ...

Is it possible to call the js function after button click event for the same button.
Hi I need to two work with same button. When I am calling page as popup. It contains save button. After Inserting the record to database I want to call a javascript function to update the popup values to parent window. I want two functions in the same save button.? Is it possible. use page.registerscript to call a javascript function from the dot net code. when you register a script like that the script is executred after the the page is loaded. In the javascript you can write code to update the parent window Vikram www.vikramlakhotia.comPlease mark the answer if it helped you   ...

I double click a button but no event handle in source view why, go to button properties,events,onclick, try to create event handle but its where. Only the button markup.
I have a button, if double click it no handle is created. Iam only taken to the buttons' markup in source view. This is happening on all pages through out the whole solution. Possible cause is, this project was not ajax enabled, i installed ajax, edited the web.config file of this project to make the site ajax enabled. I had to build the solution and got no errors, every thing is ok a part from double clicking all controls not being able to get to their event handles.--------------------------------------------------No Project Too Eazy.-------------------------------------------------- ...

How to call button click event in VB.Net?
Hi All,         I have a button(button1) which has some line of code to be executed it its click.         I want to execute all those lines of code in some other  button click(button2).         I want to make use of the button event(button1) without writing the whole lines of code in the button(button1)       Example:::          button1_click(some arguments) { line 1; line 2 line 3; line 4; } I have another ...

ASPX Web Form window jumps when link button or button is clicked
Dear, i m senthil, and i m new to asp.net I m using asp.net with vb, my problem is, i m creating a forum, in all web pages while clicking any link button or command button the webpage jumps and the focus is going to some other controls. I want the page not to get jumped... Pls help in this regard. set the MaintainScrollPositionOnPostback property of your page to true. http://msdn.microsoft.com/en-us/library/system.web.ui.page.maintainscrollpositiononpostback.aspxMike Banavige~~~~~~~~~~~~Need a site code sample in a different language? Try converting it with: http://converter.telerik.co...

How to trigger the button's Click event only when user really click it. (Escape the return button)
Hi, I want the user really click the button, then exec the code in btnSubmit_Click event . Not by pushing the <return> button. Does anybody have any idea about this? Thanks Create a separate Subroutine that does all the stuff you need the click event to do (separate from the Button's click event)Then, in the button's click event - call that event. At this point, you can also call it from anywhere else in your application that you'd like.David WierMCP/ASPInsiderASPNet101.com - where to look first!Please Vote for ASPNet101 - 'Best Community Resource'!Control Grouper ...

Button click event vs button command event
Hi, Can any one tell me whats the difference between the two events, and when to use which one? Thanks. Check out this article on event bubbling: Bubbling Events Up the Control Hierarchy Summary: The DataGrid control, as you likely know, can easily be configured to add a column of buttons. By adding a ButtonColumn, whenever one of the DataGrid's buttons in the column is clicked, the Web page posts back and the DataGrid's ItemCommand event fires. In fact, the same behavior can be noted if you manually add a Button or LinkButton control into a DataGrid TemplateColum...

How to programmatically in vb.net invoke a button click event for a button inside a Formview?
Hi, my name is Bonfa', would like to invoke a button click event programmatically in vb.net, but the button is inside a formview, how can i accomplish this?I was trying to do like this FormView1.FindControl("btn_update_click.PerformClick()") but it is not working.Thanks in advance.  Hi,In the markup you can do this: <asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick="btnUpdateClick" />Then in the code behind you can write your sub:  Protected Sub btnUpdateClick() 'your code here ...

button click click click sound!
Ok stupid question time!Could somebody tell me if it is possible to remove the 'click' sound a button makes when you click on it?The image button doesn't do this. Only normal and linkbuttons. (Ok, auto-postback on dropdownlists do this as well!)I'm busy building a internal system using ATLAS which makes for an awesome experience.However the button click sound is giving it my secret away and killing mah magic! It's a client thing, not a server thing... Dont use a standard button, use your own control with onmousedown or some such.... The sounds are determined by t...

How to call a java script after clicking in a web form button
Hi all How can I call a java script after clicking in a web form button? thanks jsn in the Page_load event - add an OnClick attribute to the button, which calls the Javascript: btnPrint.Attributes("onClick") ="javascript:window.print();"David WierMCP/ASPInsiderASPNet101.com - where to look first!Please Vote for ASPNet101 - 'Best Community Resource'!Control Grouper - easily control properties for multiple controls with one control!Calendar Express - The Best HTML Calendar Generator on the web!(Please 'Mark as Answer' when it applies)...

Web resources about - How to call click event of button apart from clicking on the button. - asp.net.web-forms

WARNING: Unhappy Meals for Facebook Users Clicking Links to $250 McDonald’s Gift Cards
Facebook users who click on links promising them $250 gift cards to McDonald’s are becoming victims of The Hamburglar, in the form of online ...

Facebook tests subscribe button for pages to allow users to receive updates without clicking Like
... Marketing Land reader Craig Smith: We do not have the button on our own accounts, but here is the way to connect with a page without clicking ...

Macintosh User Manual - Clicking - Flickr - Photo Sharing!
Explore peterme's photos on Flickr. peterme has uploaded 5226 photos to Flickr.

Shoes of Prey not the only e-tailer clicking onto bricks and mortar
Aussie internet merchants are realising that an old-school shop front is still vital for building brand and sales.

Shutting down and clicking on (04:36)
While the American government shut down explored absurdity, we have had to make do with mere bemusement over Harvey Norman chief Gerry Harvey’s ...

Want a top Australian Public Service job? Get clicking on Facebook
Click 'Like' if you have what it takes to be a Canberra mandarin.

Shoes of Prey not the only e-tailer clicking onto bricks and mortar
Aussie internet merchants are realising that an old-school shop front is still vital for building brand and sales.

Shutting down and clicking on (04:36)
While the American government shut down explored absurdity, we have had to make do with mere bemusement over Harvey Norman chief Gerry Harvey’s ...

Shoes of Prey not the only e-tailer clicking onto bricks and mortar
Aussie internet merchants are realising that an old-school shop front is still vital for building brand and sales.

Shutting down and clicking on (04:36)
While the American government shut down explored absurdity, we have had to make do with mere bemusement over Harvey Norman chief Gerry Harvey’s ...

Resources last updated: 12/18/2015 10:00:39 AM