簡體   English   中英

在由 LinkBut​​ton (ASP.net, C#) 操作的 CodeBehind 上調用函數

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

嗨 StackOverflow 社區,

我在 CodeBehind 上有這個代碼:

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

當我單擊LinkBut​​ton時執行此代碼。 當另一個方法(在同一頁面中)完成執行時,我想調用這個函數。

但我不知道作為object sender and EventArgs e傳遞什么參數。 實現這一目標的最佳方法是什么?

預先感謝您, 最好的問候。

好的,所以在 5 分鍾后,我有了創建第三個方法的想法,該方法將由單擊 LinkBut​​ton 時調用的函數和我想要執行相同代碼的方法調用。

我對實現這一目標的更好方法持開放態度。 所以,它有以下幾點:

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

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

如果您有一個名為 Btn_Search 的按鈕,並且您已經為按鈕單擊事件Btn_Search_Click(object sender, EventArgs e).創建了一個事件處理程序Btn_Search_Click(object sender, EventArgs e).

然后,在您的另一種方法中,您可以調用:

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

或者

Btn_Search_Click(null, EventArgs.Empty);

或為您的功能

Btn_Search_Function(null, EventArgs.Empty)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM