Calling Button Click Event From Another Event With Client Side Code

Hi All,

I am trying to call a (server control) button's click event from another event where the button has a client side code as well. I defined my button as:

 

<asp:Button runat="server" id="button1" onclick="button1_Click" onclientclick="someJSFunction(); return false;" text="..." />

  

The button1_Click event does not contain any server side code, I just need a reference for my other event to call to, like so:

 

void OtherEvent(object sender, EventArgs e) {
button1_Click(sender, e);
}

  

What happens is that button1_Click is being fired but the client side code is not. What I want to happen is be able to call programmatically the button1_Click event and fire the client side code attached to it and not post back the page - the reverse of what is happening. Makes sense?  

0
charliecb2
2/21/2007 3:33:26 AM
asp.net.web-forms 93655 articles. 6 followers. Follow

3 Replies
1754 Views

Similar Articles

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

I may be missing something but why not just remove the onclick="button1_Click" from the Button?
Steven Bey

Recursion: see Recursion
0
stevenbey
2/21/2007 3:56:19 AM

Hi Steven,

I already did that (remove the onclick="button1_Click" attribute of the button) but still the client side code won't fire when programmatically calling it (button1_Click). Ofcourse the button1_Click itself fires.

To make it much clearer, what I want to happen is from another event/method, I call on it's (the button's) Click event (programmatically) as if the user pressed or clicked the button. I do this so that I can call the client side code that I attached to the button. The button1_Click does not have any server side code, it is just there so that I can attach a client side code and be able to access it via another event calling the method. Sort of a dummy control since the client side code always returns false. The important thing is the client side code fires.

So, the following is what I have:

aspx page:

 

<asp:Button runat="server" id="button1" onclientclick="return someJSFunction();" />

<script>
 function someJSFunction() {
  // do something here...
  return false;
 }
</script>
  

aspx.cs page

 

protected void button1_Click(object sender, EventArgs e) {
// Does not do anything } void SomeOtherEvent(object sender, EventArgs e) {
// Call button's click event to mimic user clicking on the button.
// Click supposed to fire client side code (someJSFunction)
button1_Click(sender, e); }
  
0
charliecb2
2/21/2007 4:48:36 AM

Your explanation still doesn't help but I get the impression, from the comments in your code, that you want to call the client-side, javascript function from the server. The only way to do this is to include a client-side method call. For example:

void SomeOtherEvent(object src, EventArgs e)
{
    ClientScript.RegisterStartupScript(typeof(Page), "someJSFunction", "someJSFunction();", true);
}
The button is completely unnecessary.
Steven Bey

Recursion: see Recursion
0
stevenbey
2/21/2007 11:56:07 AM
Reply:

Similar Artilces:

Calling Client Event From Server Side Click Event
Hi ,Would appreciate if someone could help me out with my issue as below Issue : This whole process Fetching Data from WebService ,Writing a File on Server from it Finally passing the path of file created to Browser so as to give User the option to either Open or Save the File at User end needs to be done in one click event.I am fine with Server event getting everything done but not sure how to open the file for end User.One way is using Window.open(FilePath.However, not sure how I can do all the Server event and then using the FilePath , call client end Window.open()Please suggest.Rega...

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...

Firing both Client side event and server side event for server side button
I am having some difficulties getting this to work.. I have button_click (Asp.net) and ClientClick (Javascript).  Javascript is firing fine.  Server script is not. may be you have return return false in the in the javascript which is why its not firing server side event.You should add javascript evelt like thisbutton1.onclientclick = "functionname" Function should nopt return false. Vikram www.vikramlakhotia.comPlease mark the answer if it helped you...

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...

How to call a javascript function on a button click event after the server side code is run?
Hi, I have a button on click of which i navigate to a different search screen, select some items and come back. While I am coming back to the original screen, I want to invoke a javascript function which actually shows up a small dialog window. Is this possible ? I appreciate any help.   Thanks, Lavanya     On page_load event of the main page you can check the UrlReferrer to know from which address this request is coming and if it was from the search page use Page.ClientScript.RegisterStartyopScript(this.GetType(),"ss","alert('hell...

button click event : how to prevent service code fire from client side script
Button have client side click event and server side click event, my question is: how can I prevent server side click event code execute in conditional from client side script code below: <script language="javascript" type="text/javascript">function Client_onclick() { if (cb.checked == false) do not execute service side code.... elseexecute service side code..... } </script> <form id="form1" runat="server"> <input id="cb" type="checkbox" /><asp:Button ID="Button1" runat="serv...

How to raise a button click event in another event
Hi, I need to raise button 1 onlick event when button 2 is clicked. Pls advice me. thanks. -VJ Hi, you can just call the method and pass in as parameters: (this, e); or (this, EventArgs.Empty); Why would you want to do this? Is  there some logic inside of button1's click eventhandler? If so I would recommend that you place that logic in another dedicated method that you can call from button1 and button2 click event. Grz, Kris.Read my blog. Handy Firefox plugins for web developers.Workaround for non working Mark as answer buttons. Thanks XIII, It worked. actually i'll sti...

CreateChildControl Event Called after Button Click event
I have a page with Update button and a custom server control. This custom server control has radio button's in it. My problem is that when i click Update button, the update click event gets called before the CreateChildControl event of the custom server control. I want to find the value of the RadioButtons present in my custom server control in Update Button's click event. But since these controls aren't created yet in Update Button's click event, i can't find the radio button's value. Any ideas on why is event order that way?...

How to access Client Side control in server side button click event?
Hi all, I need to access client side control from server side code. For an example <form id="form1" runat="server"> <table> <tr> <td>UserName:</td> <td><input type="text" id="textBoxUserName" /></td> <td><asp:Button ID="buttonShow" runat="server" Text="show" OnClick="buttonShow_Click" /></td> </tr> </table>   </form> I need to access the textBoxUsername in the server side button click event. How to do that? Than...

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. 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  ...

CreateChildControl Event Called after Button Click event #2
I have a page with Update button and a custom server control. This custom server control has radio button's in it. My problem is that when i click Update button, the update click event gets called before the CreateChildControl event of the custom server control. I want to find the value of the RadioButtons present in my custom server control in Update Button's click event. But since these controls aren't created yet in Update Button's click event, i can't find the radio button's value. Any ideas on why is event order that way? Hi, EnsureChildControls (which again calls CreateChildControls...

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){   &...

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.-------------------------------------------------- ...

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...

Web resources about - Calling Button Click Event From Another Event With Client Side Code - asp.net.web-forms

Calling Dr. Death - Wikipedia, the free encyclopedia
Calling Dr. Death (1943) is the first of the Universal Pictures Inner Sanctum mystery films . The "Inner Sanctum" franchise originated with a ...

List of country calling codes - Wikipedia, the free encyclopedia
This is a list of country calling codes defined by ITU-T recommendations E.123 and E.164 , also called IDD ( International Direct Dialling ) ...

Calling Batman: Bane Takes Over Ecko Unlimited Facebook Page
Bane, the villain in upcoming Batman feature film The Dark Knight Rises , turned his criminal mind toward the Ecko Unlimited Facebook page , ...

Facebook Video Calling - Facebook
Bring your conversations to life on Facebook. With face-to-face video calling, now you can watch your friends smile, wink and LOL. To get started, ...

Facebook’s Talktime program in India pays new mobile users in calling credit
... Facebook has launched its Talktime program in India which gives every new user who signs up with a mobile device 50 Rupees (almost $1) in calling ...

Berlin Calling (@neuraum) on Twitter
Sign in Sign up To bring you Twitter, we and our partners use cookies on our and other websites. Cookies help personalize Twitter content, tailor ...

Calling all foodies: Here comes @TwitterFood
There are many thousands of food-related Tweets people send on Twitter each day – Tweets about meals, ingredients, favorites, recipes and dining ...

Rounds Video Calling + Chat for iPhone 4, iPhone 4S, iPhone 5, iPhone 5c, iPhone 5s, iPad 2 Wi-Fi, iPad ...
Get Rounds Video Calling + Chat on the App Store. See screenshots and ratings, and read customer reviews.


Malcolm Turnbull makes surprise visit to Iraq, calling for Europe to 'step up'
Prime Minister Malcolm Turnbull has called on European countries to &quot;step up&quot; with stronger help to defeat the so-called Islamic State ...

Resources last updated: 1/23/2016 11:02:07 PM