简体   繁体   中英

Call a function on CodeBehind that's operated by a LinkButton (ASP.net, C#)

Hi StackOverflow Community,

I have this code on CodeBehind:

protected void Btn_Search_Function(object sender, EventArgs e)
{
   GV_Results.PageIndex = 0;
   GV_Results.DataBind();
   hdnSelectedTab.Value = "1";
}

This code is executed when I click a LinkButton . I want to call this function when another method (in the same page) finishes executing.

But I don't know what arguments to pass as object sender and EventArgs e . What is the best approach to achieve this?

Thank you in advance, Best regards.

Ok so after 5 minutes I had the idea of creating a third method that would be called both by the function that is called when I click the LinkButton, and by the method I want to execute the same code.

I'm open to better ways of achieving this. So, it is has follows:

protected void Btn_Search_Function(object sender, EventArgs e)
{
    SearchFunction();
}

private void SearchFunction()
{
    GV_Results.PageIndex = 0;
    GV_Results.DataBind();
    hdnSelectedTab.Value = "1";
}

If you have a button called Btn_Search and you have created an event handler for the button click event Btn_Search_Click(object sender, EventArgs e).

then, in your another method you can call like:

public void my_function()
{
    //This simulates the button click from within your code.
    Btn_Search_Click(Btn_Search, EventArgs.Empty);
}

or

Btn_Search_Click(null, EventArgs.Empty);

or for your function

Btn_Search_Function(null, EventArgs.Empty)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM