1

I dont know why linkbutton onclick event is not firing. I have tried rewriting the code. I have tried to redirect directly on button click function. I have tried setting a break point inside dashbut_Click() on redirectUser() line but it never reaches there. Kindly help me figure this out.

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>

Code Behind:

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");
    }
}

EDIT:

It seems that the LinkButton click event is not firing because of a JS Error. I dont know how that is related but when I click on the button and view the error on browser Inspect Element I see the following TypeError.

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

This is the script:

<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>

Error is on theForm.submit(); line.

This is beyond me. Help me out.

15
  • Is your asp:LinkButton inside the form? Commented Jan 6, 2017 at 20:04
  • it would get an error if not Commented Jan 6, 2017 at 20:06
  • 2
    Hm, normally the click event is a postback to the server and should trigger the page load event first before handling the click event. Are you using some JS, or a ScriptManager object? Commented Jan 6, 2017 at 20:49
  • 2
    Try removing the LinkButton and the dashbut_Click method. Then recreate them. It worked for this question. If not there could be a javascript error/interference somewhere as @MartinE also suggests Commented Jan 6, 2017 at 20:55
  • 1
    Well, we are like shooting in the dark without seeing the rest of the code. So, create a new ASPX page (without master page), and add a LinkButton with OnClick event, and try debug it? Commented Jan 6, 2017 at 21:14

2 Answers 2

1

So the problem seemed to be with JavaScript. Actually there was a button on my page with ID=submit this was overriding submit() function on the form, hence the error. This helped Thumbs Up for Stackoverflow Community.

Sign up to request clarification or add additional context in comments.

Comments

0

Sorry if I can't comment due to low reputation points. I would like to know if you need CauseValidation set to false.

Try adding usesubmitbehavior="false".

3 Comments

I have tried your code and it worked. Is possible that the event is duplicated somewhere else. causing this one not to fire?
try adding this to your Linkbutton. submitbehavior="false"
I have updated the question. Uncaught exception in JavaScript is causing the event not to fire

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.