【问题标题】:LinkButton click event not firing asp.netLinkBut​​ton单击事件未触发asp.net
【发布时间】:2017-01-06 20:01:25
【问题描述】:

我不知道为什么 linkbutton onclick 事件没有触发。我已经尝试重写代码。我试图直接在按钮点击功能上重定向。我尝试在redirectUser() 行的dashbut_Click() 内设置一个断点,但它从未到达那里。请帮我解决这个问题。

HTML:

<li><asp:LinkButton ID="dashbut" runat="server" 
           CausesValidation="false" 
           OnClick="dashbut_Click"
           Text="Dashboard">
               <img src="images/dash.png" height="25" width="25" class="fa fa-tachometer" /><span> Dashboard</span>
    </asp:LinkButton>
 </li>

代码背后:

protected void dashbut_Click(object sender, EventArgs e)
{
    //Response.Redirect("~/Views/Portal/AdminDashboard.aspx");
    redirectUser();
}

private void redirectUser()
{
    string myConnection = dbController.connectionString;
    SqlConnection conn = new SqlConnection(myConnection);

    string userCheckQuery = "SELECT UserType from tblUsers where ID = '" + USERid + "'";
    SqlCommand cmd1 = new SqlCommand(userCheckQuery, conn);
    conn.Open();
    bool userType = (bool)cmd1.ExecuteScalar();
    conn.Close();

    if (userType == true)
    {
        Response.Redirect("~/Views/Portal/AdminDashboard.aspx");
    }
    else if (userType == false)
    {
        Response.Redirect("~/Views/Portal/Dashboard.aspx");
    }
}

编辑:

似乎由于 JS 错误,LinkBut​​ton 点击​​事件没有触发。我不知道这有什么关系,但是当我单击按钮并在浏览器检查元素上查看错误时,我看到以下 TypeError。

Uncaught TypeError: theForm.submit is not a function
at __doPostBack (NewArtist.aspx:63)
at <anonymous>:1:1

这是脚本:

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['form1'];
if (!theForm) {
    theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>

错误出现在theForm.submit(); 行。

这超出了我的范围。帮帮我。

【问题讨论】:

  • 你的asp:LinkButtonform里面吗?
  • 否则会报错
  • 嗯,通常点击事件是回发到服务器,应该在处理点击事件之前先触发页面加载事件。你是在使用一些 JS,还是 ScriptManager 对象?
  • 尝试删除 LinkBut​​ton 和 dashbut_Click 方法。然后重新创建它们。它适用于this question。如果不是,@MartinE 也建议某处可能存在 javascript 错误/干扰
  • 好吧,我们就像在黑暗中拍摄,没有看到其余的代码。因此,创建一个新的 ASPX 页面 (无母版页),并添加一个带有 OnClickLinkBut​​ton 事件,并尝试调试它?

标签: javascript c# asp.net


【解决方案1】:

所以问题似乎出在 JavaScript 上。实际上,我的页面上有一个带有ID=submit 的按钮,它覆盖了表单上的submit() 函数,因此出现了错误。这个helped 为 Stackoverflow 社区点赞。

【讨论】:

    【解决方案2】:

    很抱歉,由于声誉点低,我无法发表评论。我想知道您是否需要将 CauseValidation 设置为 false。

    尝试添加 usesubmitbehavior="false"。

    【讨论】:

    • 我已经尝试过您的代码并且它有效。该事件可能在其他地方重复。导致这个不开火?
    • 尝试将此添加到您的链接按钮。 submitbehavior="false"
    • 我已经更新了这个问题。 JavaScript 中未捕获的异常导致事件不触发
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 2023-03-10
    • 1970-01-01
    • 2020-12-26
    • 1970-01-01
    相关资源
    最近更新 更多