How to get the value from a TextBot on a submit button?

77 views Asked by At

I'm creating a webpage that will recieve data from a QR Code. I'm new at asp .Net and I'm trying to recieve a value from a TextBox and pass it to SQL server. How can I do it using JavaScript or any language?

Here is my code:

I tried to do it using this command: String text = email.Text.ToString();

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="SampleDB.aspx.vb" Inherits="SampleDB.SampleDB" %>



<!DOCTYPE html>

<html  lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>MS SQL SAMPLE DATABASE</title>
<link rel="stylesheet" href="font/Audiowide.css" />
<link rel="stylesheet" href="css/style.css"/>
</head>
<body>
<section class="cus-top-section">
    <h2 class="cus-main-heading">MS SQL SAMPLE DATABASE</h2>
</section>
    <form id="form1" runat="server">
<section class="cus-bot-section">
<h2 class="hidden">MS SQL Database</h2>
    <div style="overflow-x:auto;" class="cus-table-outer">
        <div class="cus-btn" >
            <asp:Button ID="btnSampleData" runat="server" Text="Show Sample Data" CssClass="submit" />
        </div> 
        <div class="tb-left"> </div>
        <div class="tb-rgt">
        <asp:Label ID="lblMessage" runat="server"></asp:Label>
        <br />
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
        <asp:SqlDataSource ID="sampledataSource" runat="server" ConnectionString="<%$ ConnectionStrings:connection %>" SelectCommand="SELECT * FROM [People]"></asp:SqlDataSource>
        </div>
    </div>
</section>

        <section class ="campos">
            //Campo inserir o nome
            <table>
        <tr>
            <td>
                <div style="overflow-x:auto;" class="cus-table-outer"/>
                <asp:Label Text="Insira o seu nome completo" runat="server" CssClass="cus-bot-section" />
            </td>
            <td colspan="2">
                <div style="overflow-x:auto;" class="cus-table-outer"/>
                <asp:TextBox ID="TextBox2" runat="server" CssClass="cus-bot-section"></asp:TextBox>
            </td>
        </tr>
    </table>
        // Campo inserir endereço
            <table>
        <tr>
            <td>
                <div style="overflow-x:auto;" class="cus-table-outer"/>
                <asp:Label Text="CPF" runat="server" CssClass="cus-bot-section" />
            </td>
            <td colspan="2">
                <div style= "overflow-x:auto;" class="cus-table-outer"/>
                <asp:TextBox ID="CPF" runat="server" CssClass="cus-bot-section" onkeydown = "return (!(event.keyCode>=65) && event.keyCode!=32);" 
                    Rows="10" onkeypress="return this.value.length<=10" Width="100%"></asp:TextBox>

            </td>
        </tr>
    </table>
            <script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : '307419739906888',
      cookie     : true,
      xfbml      : true,
      version    : 'v3.2'
    });

    FB.AppEvents.logPageView();

  };

  (function(d, s, id){
     var js, fjs = d.getElementsByTagName(s)[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement(s); js.id = id;
     js.src = "https://connect.facebook.net/en_US/sdk.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));
    </script>

    <fb:login-button scope="public_profile,email"
                     onlogin="checkLoginState();">
    </fb:login-button>


            <table>
        <tr>
            <td>
                <div style="overflow-x:auto;" class="cus-table-outer"/>
                <asp:Label Text="email" runat="server" CssClass="cus-bot-section" />
            </td>
            <td colspan="2">
                <div style= "overflow-x:auto;" class="cus-table-outer"/>
                <asp:TextBox ID="email" runat="server" CssClass="cus-bot-section" Width="100%"></asp:TextBox>

            </td>

        </tr>
    </table>

        </section>
              <script  runat="server">
                  Sub submit(sender As Object, e As EventArgs)


                  End Sub


</script>
        <asp:TextBox id="txt1" runat="server" />
<asp:Button OnClick="submit" Text="Submit" runat="server" />
<p><asp:Label id="lbl1" runat="server" /></p>
        </form>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
<script>

$(document).ready(function(){
    $("input.submit").click(function(){
        var mohara = $("email")            
$() 
        $("table").toggle();
    });
});
</script> 

</body>
</html>

Should I use an external class to do it or is it possible to do it inside the file aspx?

1

There are 1 answers

2
SomoKRoceS On BEST ANSWER

First, it seems like you want to code in C# but actually the project is written in VB (i can tell by the header), so you might want to open the project in ASP.NET C#.

Now for the question, Logic behind the aspx file should be written inside SampleDB.aspx.vb (as written in the top of your file: CodeBehind="SampleDB.aspx.vb" ).

(When you will switch to c# the file will end with .cs instead of .vb since it will be a class written in c#)

In SampleDB.aspx.vb you will be able to access to the TextBox by using its ID, so lets assume this is the TextBox:

<asp:TextBox ID="EmailTextBox" runat="server" CssClass="cus-bot-section"></asp:TextBox>

You will be able to get the text in it by writing String text = EmailTextBox.Text;

In that class, you should also access your Database. (It is called CodeBehind for a reason :) It is the code behind the view).

I would recomment creating a new static class that handles connection to the database and then wherever you want to use it, you call the right function from the code behind.

Hope i helped :)