【问题标题】:How to run onclick event in asp.net from another button's javascript?如何从另一个按钮的 javascript 在 asp.net 中运行 onclick 事件?
【发布时间】:2011-03-09 04:13:13
【问题描述】:

void 按钮接收到true后有一个确认框,期望运行另一个按钮的点击事件 但是,Void2_Button_Click 没有运行,哪里出错了?

protected void Void2_Button_Click(object sender, EventArgs e)
{
        // do something    
}


Void_Button.Attributes.Add("onclick", "var agree=confirm('Confirm to void?'); if (agree){document.getElementById('Void2_Button').click();}");

这个问题还没解决,谁能回答?

【问题讨论】:

  • 为什么要以编程方式运行另一个按钮的点击事件?让两个按钮在后面的代码中触发相同的事件处理程序不是更好吗?

标签: c# asp.net


【解决方案1】:

您也可以对第一个按钮进行普通的服务器端点击,这将简单地调用另一个按钮的点击事件:

<asp:Button id="Void_Button" runat="server" Text="Void" OnClick="Void_Button_Click" OnClientClick="return confirm('Confirm to void?');" />

protected void Void_Button_Click(object sender, EventArgs e)
{
   Void2_Button_Click(sender, e)
}

【讨论】:

    【解决方案2】:

    您必须将 .ClientID 用于 javascript。试试

    Void_Button.Attributes.Add("onclick", "var agree=confirm('Confirm to void?'); if (agree){document.getElementById('"+Void2_Button.ClientID+"').click();}");
    

    【讨论】:

    • @Mathew 你要在Page_Load 中添加这个吗?
    • 是的,在 if (!Page.IsPostBack) 之后添加页面加载,不触发
    • 如果我不这样做,还有其他简单的方法可以像上面那样从 javascript 调用 asp .net cs 代码中的函数吗?
    • 你可以尝试不使用 page_load 中的 if(!IsPostBack) 条件吗?
    • 试过了,没有!postback,没有触发,标签没有更新也没有server.transfer到另一个页面,好像没有运行函数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-18
    • 2022-10-15
    • 2021-12-21
    • 1970-01-01
    • 2020-10-06
    相关资源
    最近更新 更多