Javant Javant - 4 months ago 12
C# Question

Button won't trigger method

Title really says it all. I press the button and nothing happens trying to figure out why.

Default.aspx Markup (In form scope)

<div class="form-group">
<div style="text-align:center">
<asp:Button input type="submit" class="btn btn-default btn-lg" align="center" runat="server" OnClick="btnSubmit_Click" Text="Submit"/>
</div>
</div>


Default.aspx Code

protected void btnSubmit_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();

ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Connected to ASPX file');", true);
//...rest of code
}


Added Form

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<%-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --%>
<meta name="description" content="">
<meta name="author" content="">

<%--- Dependencies ---%>
<link rel="stylesheet" href="./Content/bootstrap.min.css"/>
<link rel="stylesheet" href="./Content/bootstrap-theme.min.css"/>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<link href="./Content/ie10-viewport-bug-workaround.css" rel="stylesheet"/>
<link href="./Content/starter-template.css" rel="stylesheet"/>
<script src="./Scripts/ie-emulation-modes-warning.js"></script>

<title>CCPOF</title>
</head>

<body>
<form class="form-horizontal" id="form1" runat="server">
<div>


<div class="form-group">
<div style="text-align:center">
<asp:Button class="btn btn-default btn-lg" align="center" runat="server" OnClick="btnSubmit_Click" Text="Submit"/>
<!--<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" CssClass="btn btn-default btn-lg" />
<asp:Button input type="submit" class="btn btn-default btn-lg" align="center" runat="server" OnClick="btnSubmit_Click" Text="Submit"/> -->
</div>
</div>

<br>

</div>
</form>

<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->

<script src="./Scripts/jquery-1.9.1.min.js"></script>
<script>window.jQuery || document.write('<script src="../../assets/js/vendor/jquery-1.9.1.min.js"><\/script>')</script>
<script src="./Scripts/bootstrap.min.js"></script>
<script src="./Scripts/ie10-viewport-bug-workaround.js"></script>
<embed id="embed_npfido" type="application/npfido" height="0"/>

</body>
</html>

Answer

Just to close this question, I'm writing an answer from my communication with OP (please refer to comments)

A couple of changes worked for OP to get post back functional

Changing button HTML to

<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" CssClass="btn btn-default btn-lg" />

Changing alert script to

ClientScript.RegisterClientScriptBlock(this.GetType(), "myalert", "<script>alert('Connected to ASPX file');</script>");