Answered by:
nothing happens after clicking on the submit button

Question
-
User-427368358 posted
Hi
i'm new in C# and have a problem because nothing happens after clicking on the submit button. (no error either) The submit button must check the password which is located in a table of sql server. I converted this code from vb.net (where it runs perfectly). The only thing that works is the focus in the input after the page is loading.
Thanks for help.
Here the aspx code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="start.aspx.cs" Inherits="start" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server"> <title></title></head>
<body><div style="height:0px"><asp:Image ID="Image2" runat="server" ImageUrl="~/logo.gif" /> </div>
<center><form id="form1" runat="server">
<div style="height:165px"> </div>
<fieldset style="background-color:#F7F7F7;width:600px">
<asp:Label ID="Label1" runat="server" Font-Size="XX-Large" ></asp:Label><div style="height:5px"> </div>
<asp:Label ID="Label2" runat="server" Text=" philippe mairiaux © 2021" Font-Size="XX-Small" />
<div style="height:30px"> </div>
<span style="font-size: large; font-weight: bold">Password in:</span>
<table><tr>
<td>Password:</td>
<td align="left"><input id="Password1" type="password" maxlength="10" style="width:80px" runat="server" /></td></tr>
<tr><td><input type="text" style="visibility: hidden;"/></td></tr>
<tr></tr> <tr><td><input id="Submit1" type="submit" value="Log in" runat="server" /></td>
<td><span style="font-family:Arial; ; font-size:xx-small; color:Blue; font-weight: bold"></span> </td></tr> </table> </fieldset> </form></center> </body></html>Here the c# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class start : System.Web.UI.Page
{
protected void Submit1_ServerClick(object sender, System.EventArgs e)
{
string pw;
HttpCookie ok = new HttpCookie("ok");
SqlCommand comd;
string sql = "";
string m = "";
SqlDataReader dtreader;
using (SqlConnection mConnection = new SqlConnection(param.ConnectionString))
{
mConnection.Open();
sql = "select pw from pw";
comd = new SqlCommand(sql, mConnection);
dtreader = comd.ExecuteReader();
if (dtreader.HasRows)
{
while (dtreader.Read())
m = dtreader.GetString(0);
}
dtreader.Close();
}
pw = Password1.Value;if (pw == m)
{
ok.Value = "y";
Response.Cookies.Add(ok);
Response.Redirect("start2.aspx", false);
}
else
ClientScript.RegisterClientScriptBlock(this.GetType(), "myscript", " alert('Wrong password.');", true);
}protected void Page_Load(object sender, System.EventArgs e)
{
Password1.Focus();}
}Monday, April 12, 2021 11:09 AM
Answers
-
User475983607 posted
The C# syntax for assigning an event handler in the code behind is shown below.
protected void Page_Load(object sender, EventArgs e) { Submit1.ServerClick += Submit1_ServerClick; } protected void Submit1_ServerClick(object sender, EventArgs e) { Label1.Text = "Hello World"; }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 12, 2021 3:14 PM -
User-427368358 posted
Thanks.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 12, 2021 4:11 PM
All replies
-
User475983607 posted
raffarin
i'm new in C# and have a problem because nothing happens after clicking on the submit button. (no error either)The issue you are facing has to do with understanding Web Forms and server control basics not C#. You are using an HTML submit button not a server control. Use the button server control to wire up the OnClick handler.
<asp:Button ID="Submit1" runat="server" Text="Log In" OnClick="Submit1_ServerClick" />
Monday, April 12, 2021 11:21 AM -
User-427368358 posted
Hi, thanks. This works indeed.
Is this problem specific to C#, because with vb.net, there is no problem with the use of html-input object?
Monday, April 12, 2021 12:33 PM -
User475983607 posted
You are mistaken. VB.NET, also uses server controls. The handlers typically use the handler syntax in the code behind. See any beginning level tutorial.
Monday, April 12, 2021 12:45 PM -
User-427368358 posted
In vb.net there is at the end of this line:
Protected Sub Submit1_ServerClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Submit1.ServerClick
which makes it possible to use the html input-object.
Does it not exist the same in C# to put at the end of this= protected void Submit1_ServerClick(object sender, System.EventArgs e) ...
in order to be able to use html input-object with C# too?
Monday, April 12, 2021 1:35 PM -
User475983607 posted
The C# syntax for assigning an event handler in the code behind is shown below.
protected void Page_Load(object sender, EventArgs e) { Submit1.ServerClick += Submit1_ServerClick; } protected void Submit1_ServerClick(object sender, EventArgs e) { Label1.Text = "Hello World"; }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 12, 2021 3:14 PM -
User-427368358 posted
Thanks.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 12, 2021 4:11 PM