如何以编程方式设置单击按钮

发布于 2025-01-01 21:21:11 字数 83 浏览 0 评论 0原文

我在 ASP .Net 中使用 LinkBut​​ton(将其视为按钮)。我需要使用后面的 C# 代码以编程方式单击该按钮。我怎样才能实现这个目标..?

I'm using LinkButton(Consider it as Button) in ASP .Net. I need to click that button programmatically using C# code behind. How can I achieve this..?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愚人国度 2025-01-08 21:21:11

不要将事件处理程序用作方法,它们的唯一目的是处理事件。所有功能都应该封装在可以从事件处理程序内部以及您需要的任何地方调用的方法中。

protected void Button_Click(sender As Object, e As CommandEventArgs) 
{
     int id=int32.Parse(e.CommandArgument.ToString());
     doSomething(id);
}

然后,您也可以从页面的加载事件中调用此方法:

protected void Page_Load(object sender, EventArgs e)
{
    //get the ID of the first record in GridView's DataSource here or where you databind the GridView
   doSomething(id);
}

public void doSomething(int id) { //do something }

如何将 GridView 记录中的 ID 作为 CommandArgument 传递:

<asp:TemplateField>
      <ItemTemplate>
          <asp:LinkButton ID="Button1" runat="server" CommandArgument='<%#Eval("IdColumn")%>' OnCommand="Button_Click" Text="Do Something">
          </asp:LinkButton>
       </ItemTemplate>
</asp:TemplateField>

Don't use event-handlers as methods, their only purpose is to handle events. All functionality should be encapsulated in methods that can be called from within the event-handler as well as from wherever you need it.

protected void Button_Click(sender As Object, e As CommandEventArgs) 
{
     int id=int32.Parse(e.CommandArgument.ToString());
     doSomething(id);
}

Then you can call this method for example from page's load event as well:

protected void Page_Load(object sender, EventArgs e)
{
    //get the ID of the first record in GridView's DataSource here or where you databind the GridView
   doSomething(id);
}

public void doSomething(int id) { //do something }

How to pass the ID from the GridView record as CommandArgument:

<asp:TemplateField>
      <ItemTemplate>
          <asp:LinkButton ID="Button1" runat="server" CommandArgument='<%#Eval("IdColumn")%>' OnCommand="Button_Click" Text="Do Something">
          </asp:LinkButton>
       </ItemTemplate>
</asp:TemplateField>
从﹋此江山别 2025-01-08 21:21:11

好吧,我不太确定你想在这里实现什么目标。但第一种方法是简单地调用事件处理程序的方法代码。

但如果您有兴趣将响应发送回客户端并将按钮单击作为新请求,您可以尝试一下

ASPX:

    <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">LinkButton1</asp:LinkButton>
    <br />
    <br />
    <asp:LinkButton ID="LinkButton2" runat="server" onclick="LinkButton2_Click">LinkButton2</asp:LinkButton>

C#:

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Write("I was programatically called!");
    }

    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        string pbref = Page.GetPostBackEventReference(LinkButton1);
        Page.ClientScript.RegisterClientScriptBlock(GetType(), "KeyName", "<script>" + pbref + "</script>");
    }

但是存在 1 个问题。您可能会遇到事件验证错误。在这种情况下,您可能必须禁用事件验证,如下所示:

<%@ Page Language="C#" EnableEventValidation="false" ... %>

我不推荐它。还有其他方法可以克服它..但这是一个不同的谷歌搜索:)

Well, i'm not quite sure what you're trying to achieve here. but 1 method is to simply call the event handler method code.

But if you're interested in sending the response back to the client and taking the button click as a new request, you can try this out

ASPX:

    <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">LinkButton1</asp:LinkButton>
    <br />
    <br />
    <asp:LinkButton ID="LinkButton2" runat="server" onclick="LinkButton2_Click">LinkButton2</asp:LinkButton>

C#:

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Write("I was programatically called!");
    }

    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        string pbref = Page.GetPostBackEventReference(LinkButton1);
        Page.ClientScript.RegisterClientScriptBlock(GetType(), "KeyName", "<script>" + pbref + "</script>");
    }

However there's 1 problem. You might run in to an event validation error. in which case you might have to disable event validation as follows:

<%@ Page Language="C#" EnableEventValidation="false" ... %>

I wouldn't recommend it. there are other ways to overcome it..but that's a different google search :)

卷耳 2025-01-08 21:21:11
private void Page_Load()
{
    if (!Page.IsPostBack)
    {
        //when the pages is rendered and loaded for the first time execution goes here
        //so... call the method that selects the first row
        SelectsFirtsRow();
    }
    else
    {
     //do something else
     ...
    }
}
protected void FirstRowLinkButton_Click(object sender, EventArgs e)
{
    //move all the code that selects the first row to a method.
    //you can also supply some arguments if they are needed for method execution... that's up to you
    SelectsFirtsRow();
}
public void SelectsFirtsRow();
{
    //your logic goes here. i.e. selects the first row
}

附注
从代码隐藏中调用事件处理程序是一种不好的做法。事件处理程序旨在触发某些客户端操作。

private void Page_Load()
{
    if (!Page.IsPostBack)
    {
        //when the pages is rendered and loaded for the first time execution goes here
        //so... call the method that selects the first row
        SelectsFirtsRow();
    }
    else
    {
     //do something else
     ...
    }
}
protected void FirstRowLinkButton_Click(object sender, EventArgs e)
{
    //move all the code that selects the first row to a method.
    //you can also supply some arguments if they are needed for method execution... that's up to you
    SelectsFirtsRow();
}
public void SelectsFirtsRow();
{
    //your logic goes here. i.e. selects the first row
}

p.s.
invoking event handlers from code behind is a bad practice. event handlers are meant to fire on some client side actions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文