José Reis
José Reis

Reputation: 23

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.

Upvotes: 0

Views: 424

Answers (2)

S.I.J
S.I.J

Reputation: 989

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)

Upvotes: 1

José Reis
José Reis

Reputation: 23

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";
}

Upvotes: 2

Related Questions