Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Donate Today!

Do you enjoy these
technical forums?
Donate Today! Click Here

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

newbie - how do you call a button_click event from another methodHelpful Member! 

AdamSpattam (TechnicalUser) (OP)
21 May 10 15:06
newbie - how do you call a button_click event from another method.
I am trying to do a crosspage post back (Cs and asp.net 3.5) . in the wired up SelectedIndexChanged() event of a dropdown list, i want to call a button click event that already has the code i want in it. well basically it is a work around because this web control has the UrlPostBack attribute that goes in its asp tag and the dropdown list does not, so want on selectedIndexChanged() to call btnCrossPagePostback_OnClick like i used to do in vb6.


In either case, i will need to know how to call a controls event from a method in C# if it is possible.
thanks In Advance
Helpful Member!  Rhys666 (Programmer)
21 May 10 15:12
Put the code behind the Click event in its own method and call this method from the Click event, the SelectedIndexChanged event, or wherever else you need to call it from.

Rhys

"The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it"
Terry Pratchett

AdamSpattam (TechnicalUser) (OP)
21 May 10 16:52
oh I forgot that technique! thanks.

the actual calling code I got from msdn.:
C#
// Call the Click event of button1.
button1.PerformClick();
 
But I just realized for this particular instance, I want the compiler to think the button was clicked becuase button controls, and not the dropdownlists carry the attribute <asp:button  PostBackUrl="Default.aspx"  />

I am trying to bait the user to give feedback by selecting 1 little thing in the drop down on one page, then carring the value they selected over to the feedback page.

so i will do a little something like...

private void CrossPagePostBack()
    {
              btn1.PerformClick();
       
    }


Protected void lstFanType_SelectedIndexChanged(object sender,System.EventArgs e)
     
{
   CrossPagePostBack()
}

protected void btn1(object sender, System.EventArgs e)
{
      Response.Redirect("aFeedback.aspx")
}

In this case I Know I do not need the separate method.
I will try this and post my final solution case anyone else needs to do something similar.

thanks,
Adam
AdamSpattam (TechnicalUser) (OP)
21 May 10 18:05
PerformClick() is only for windows forms, not web apps. I tried googling and could not find out how to call a button_click from another method. Anyone know know?
PatrickIRL (Programmer)
24 May 10 8:27
Have you tried

CODE

private void CrossPagePostBack()    
{              
    btn1_Click(void,void);           
}

Patrick
jmeckley (Programmer)
24 May 10 8:34
rather than cross posting, and messing with the oddities of webforms, why not encapsulate the logic within a separate object. new up an instance whenever you need it. example

CODE --> common object

class Calculator
{
  public int Add(int x, int y)
  {
     return x+y;
  }
}

CODE --> aspx

//in an event handler
int x = int.Parse(ThisTextBox.Text);
int y = int.Parse(ThatTextBox.Text);

var calculator = new Calculator();
var result = calculator.Add(x, y);

AnswerTextBox.Text = result.ToString();

Jason Meckley
Programmer
Specialty Bakers, Inc.

FAQ855-7190: Database Connection Management
FAQ732-7259: Keeping the UI responsive

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close