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.

el323
  • 2,220
  • 7
  • 35
  • 68
  • Is your `asp:LinkButton` inside the `form`? – Paul Abbott Jan 06 '17 at 20:04
  • it would get an error if not – Adrian Iftode Jan 06 '17 at 20:06
  • @PaulAbbott yes it is. – el323 Jan 06 '17 at 20:07
  • it doesn't go in to redirectUser ? did you put a break point and see if its throwing some kind of exception or something? – psj01 Jan 06 '17 at 20:16
  • Yes I tried putting a break point on redirectUser() function call. But when I click on the LinkButton nothing happens. It doesn't redirect nor does it throw an exception – el323 Jan 06 '17 at 20:18
  • is your PageLoad event fired when clicking the button? – Martin E Jan 06 '17 at 20:40
  • @MartinE It is fired when the page loads but not fired when clicking on the button. But PageLoad should fire on button click, right? Why do you think its not? – el323 Jan 06 '17 at 20:43
  • 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? – Martin E Jan 06 '17 at 20:49
  • yes I am using JavaScript, But how is that related to this link button? – el323 Jan 06 '17 at 20:52
  • 2
    Try removing the LinkButton and the `dashbut_Click` method. Then recreate them. It worked for [this question](http://stackoverflow.com/questions/41490795/ontextchanged-event-not-triggering-http-post#comment70189841_41490795). If not there could be a javascript error/interference somewhere as @MartinE also suggests – VDWWD Jan 06 '17 at 20:55
  • I would like to see all events are not fire or just one button click event. Could you set a break point at Page_Load event, and see it stop? – Win Jan 06 '17 at 21:03
  • @Win Setting a breakpoint on PageLoad event does stop when page loads for the first time but later on when I click on the linbutton it doesn't which means that the event is not firing. As I see it there is something wrong with JS. I have updated the question. – el323 Jan 06 '17 at 21:09
  • @MartinE Yes you are right. Its something with JS but I am not sure what. I have updated the question. – el323 Jan 06 '17 at 21:11
  • @VDWWD I have recreated the button and function but it seems its JS that is the problem here. – el323 Jan 06 '17 at 21:11
  • 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? – Win Jan 06 '17 at 21:14

2 Answers2

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.

Community
  • 1
  • 1
el323
  • 2,220
  • 7
  • 35
  • 68
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".

jmag
  • 578
  • 1
  • 6
  • 16
  • I have tried your code and it worked. Is possible that the event is duplicated somewhere else. causing this one not to fire? – jmag Jan 06 '17 at 20:52
  • try adding this to your Linkbutton. submitbehavior="false" – jmag Jan 06 '17 at 21:12
  • I have updated the question. Uncaught exception in JavaScript is causing the event not to fire – el323 Jan 06 '17 at 21:12