简体   繁体   中英

Cant get text from textbox

So, I need to get a string from a textbox to use in a backend program, but I'm unable to get it when im one the site, however it works if i set Text="Test1" prior, even though i change the text in the textbox, "Test1" always gets sent to to my function and works! (Talking about Box3)

Code to the text boxes below

                <div class="row">
                <form action="#" method="post" class="contact-form">
                    <div class="col-md-4 col-sm-6">
                    <asp:TextBox ID="Box3" Text="Test1" runat="server" placeholder="Gamepin..."></asp:TextBox>


                    </div>
                    <div class="col-md-4 col-sm-6">
                     <asp:TextBox ID="Box2" runat="server" placeholder="Name  ..."></asp:TextBox>

                    </div>
                    <div class="col-md-4 col-sm-12">
                     <asp:TextBox ID="Box1" runat="server" placeholder="Amount ..."></asp:TextBox>

                    </div>
                    <div   >
                     <asp:CheckBox ID="CheckBoxREPLY"  runat="server" style="vertical-align: central" Text="YesNo" TextAlign="Left" />

                    </div>
                    <div class="col-md-12 col-sm-12">
                        <input type="button" runat="server" onserverclick="Funcactivate" class="button big green" value="Launch!"/>
                    </div>
                </form>
            </div>

And my code to get textbox.text

        protected void Funcactivate(object sender, EventArgs e)
    {
        //Get textbox(s)
        string Game = Box1.Text;
        string Namepref = Box2.Text;
        string Amount = Box3.Text;
     }

更改按钮可能有效。

<asp:Button ID="btnLaunch" runat="server" Text="Launch!" class="button big green" OnClick="Funcactivate" CausesValidation="False"/>

If you are using web forms, you should be able to capture the text box entry in the "if(!IsPostBack){...}" event and set it to a variable and use it in your "Funcactivate" method.

Hello Sleepwalker, here is a code sample that works for me, I am not using the isPostBack. I am also using an asp:button Instead of using an HTML input control.

 <div>
    <asp:TextBox ID="txtBox1" runat="server"></asp:TextBox>
</div>
    <div>
        <asp:TextBox ID="txtBox2" runat="server"></asp:TextBox>
    </div>
    <div>
        <asp:TextBox ID="txtBox3" runat="server"></asp:TextBox>
    </div>
    <div>
        <asp:Button ID="btnSubmit" runat="server" Text="Button" OnClick="btnSubmit_Click" />
    </div>


protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string game = this.txtBox1.Text.ToString();
        string Namepref = this.txtBox2.Text.ToString();
        string Amount = this.txtBox3.Text.ToString(); //box3;
    }

I fixed it by moving my form to only get the < asp:X, because i discovered that if the < asp:x was nested too deep within a ton of divs, it didnt work properly.

It may have been due to something else, but it worked for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM