【发布时间】: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 错误,LinkButton 点击事件没有触发。我不知道这有什么关系,但是当我单击按钮并在浏览器检查元素上查看错误时,我看到以下 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:LinkButton在form里面吗? -
否则会报错
-
嗯,通常点击事件是回发到服务器,应该在处理点击事件之前先触发页面加载事件。你是在使用一些 JS,还是 ScriptManager 对象?
-
尝试删除 LinkButton 和
dashbut_Click方法。然后重新创建它们。它适用于this question。如果不是,@MartinE 也建议某处可能存在 javascript 错误/干扰 -
好吧,我们就像在黑暗中拍摄,没有看到其余的代码。因此,创建一个新的 ASPX 页面 (无母版页),并添加一个带有 OnClick 的 LinkButton 事件,并尝试调试它?
标签: javascript c# asp.net