Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

newbie - how do you call a button_click event from another method 1

Status
Not open for further replies.

AdamSpattam

Technical User
May 21, 2010
4
US
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
 
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
 
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
 
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?
 
Have you tried

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

Patrick
 
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:
class Calculator
{
  public int Add(int x, int y)
  {
     return x+y;
  }
}
Code:
//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
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top