How can I "pick up" values from one or more INPUT after clicking on a SUBMIT button in ASP . NET C#

Asked

Viewed 469 times

3

I’ve been having the following problem:

I have a registration form in which the customer must enter his data through the Inputs, at the end there is a button of type Submit, and when clicking it would be triggered the event click to there to get the data. It turns out that by clicking on the Submit button it submits the form but does not execute my click event, follows a snippet of code:

aspx:

<form runat="server" name="sign">

     <input runat="server" type="text" placeholder="Seu nome" ID="userName"/>
     <br/>

     <input runat="server" type="email" placeholder="Seu e-mail" ID="userEmail"/>
     <br/>

     <input runat="server" type="text" placeholder="Sua senha" ID="userPassword"/>
     <br/>

     <asp:Button runat="server" ID="sign" Text="Cadastrar" OnClick="sign"/>

</form

C# (Code Behind):

public partial class Index : System.Web.UI.Page
{

   string userNm;
   string userEml;
   string userPass;

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void sign(object sender, EventArgs e)
    {
        userNm = userName.Value;
        userEml = userEmail.Value;
        userPass = userPassword.Value;
    }
}
  • You are using Webforms?

  • Make sure that there is nothing preventing the button from firing, such as some validation. Try adding "Causesvalidation="false"" to the test button only.

  • 1

    But the Sign method of the event OnClick is server side... it will need to do a post to run on the server, you are not falling there? put a breakpoint?

1 answer

2


First make sure that Index.aspx starts with the following line, which will bind your Markup page with your code-Behind.

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

If the code is exactly as presented in the question you are in serious trouble...

<asp:Button runat="server" ID="sign" Text="Cadastrar" OnClick="sign"/>

This would already be impossible and would present an error when compiling the project because you would have a method and a component with the same identifier. And the ideal would be to give more elusive names. And in fact, in the case of events, you shouldn’t even worry and let Visualstudio nominate you by maintaining the conventions of. Net pressing Ctrl+Space when the event value is empty OnClick=""

inserir a descrição da imagem aqui

<asp:Button runat="server" ID="btnCadastrar" Text="Cadastrar" OnClick="btnCadastrar_Click"/>

At the end, you should have something like in the example below, which has been tested and works:

Index.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Index.aspx.cs" Inherits="Index" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form  runat="server">
        <div>
            <input runat="server" type="text" placeholder="Seu nome" id="userName" />
            <br />
            <input runat="server" type="email" placeholder="Seu e-mail" id="userEmail" />
            <br />
            <input runat="server" type="password" placeholder="Sua senha" id="userPassword" />
            <br />
            <asp:Button runat="server" ID="bntCadastrar" Text="Cadastrar" OnClick="bntCadastrar_Click" />
        </div>
    </form>
</body>
</html>

Index.aspx.Cs

public partial class Index : System.Web.UI.Page
{
    string userNm, userEml, userPass = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {

    } 

    protected void bntCadastrar_Click(object sender, EventArgs e)
    {
        userNm = userName.Value;
        userEml = userEmail.Value;
        userPass = userPassword.Value;
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.