使用 UseSubmitBehavior=false 访问代码隐藏中文本框的值

accessing values of textboxes in codebehind with usesubmitbehavior=false

本文关键字:文本 中文 隐藏 代码 UseSubmitBehavior false 访问 使用      更新时间:2023-09-26

我正在研究 asp.net 应用程序。我有一个带有两组文本框的母版页:

<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<div class="loginPswd">                           
    <asp:TextBox ID="txtPassword" TextMode="Password" runat="server"></asp:TextBox><br />
    <a href="javascript:;">Forgot Password?</a>
</div>
<div class="loginBtn">
    <input type="button" name="" value="Sign In" />
    <a href="#signUpModal" id="signupBtn">Sign Up</a>
</div>

<div id="signUpModal" style="display: none;" class="modal-example-content">
    <div class="modal-example-header">
        <button type="button" class="close" onclick="$.fn.custombox('close');">&times;</button>
    </div>
    <center>
        <div class="modal-example-body">
            <p>
                Start searching now<br />
                <a style="color:#fff;" href="http://joelinks.com/" target="_blank">.com</a><br />
                Get Started - Its free.
            </p>
            <div>
                <label>First Name</label>
                <%-- <input type="text" value="" name="" /> --%>
                <asp:TextBox ID="TextFirstName" runat="server"></asp:TextBox>
            </div>
            <div>
                <label>Last Name</label>
                <asp:TextBox ID="TextLastName" runat="server"></asp:TextBox>
            </div>
            <div>
                <label>Email</label>
                <asp:TextBox ID="TextEmail" runat="server"></asp:TextBox>
            </div>
            <div>
                <label>Password (6 or more characters)</label>
                <asp:TextBox ID="TextPassword" TextMode="password" runat="server"></asp:TextBox>
            </div>
            <div>
                <label>Retype Password</label>
                <asp:TextBox ID="TextRetypePassword" TextMode="password" runat="server"></asp:TextBox>
            </div>
            <div>
                <label>User</label>
                <asp:DropDownList ID="DropDownUser" runat="server">
                    <asp:listitem text="Business User" Value="3" />
                    <asp:ListItem Text="Individual User" Value="2"></asp:ListItem>
                </asp:DropDownList>
            </div>
            <div>
                <asp:Button ID="JoinNow" runat="server" Text="Join Now" OnClick="JoinNow_Click" UseSubmitBehavior="False"></asp:Button>
            </div>
            <p style="font-weight:normal; margin-top:10px;">
                Already on <a style="color:#fff;" href="http://.com/">JoeLinks.com</a>? <a style="color:#fff;" href="http://.com/">Sign In</a>
            </p>
        </div>
    </center>
</div>

我在标签后有一个form元素body。我在寄存器(第二个代码(的文本框中使用了Submitbehavor=false。当我点击提交按钮时,它会在代码隐藏中转到该事件,但文本框的值不在代码隐藏中。如果我不使用提交行为,那么它就不会进入代码隐藏。单击登录按钮时,值在代码隐藏中可用。

我该如何解决这个问题?

由于这是社区的帮助,请先转到以下链接。

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.usesubmitbehavior(v=vs.110(.aspx

让我们知道您是否清楚UseSubmitBEehaviour的概念?

有时asptextbox在某些模板中不起作用,因此您可以使用html文本框来获取值试试这个

<form id="form1" runat="server">
Name:
<input type="text" id="txtName" name="Name" value="" />
<br />
Email:
<input type="text" id="txtEmail" runat="server" value="" />
<br />
<br />
<asp:Button Text="Submit" runat="server" OnClick="Submit" />

C# 代码

protected void Submit(object sender, EventArgs e)
{
  string name = Request.Form["Name"];
 string email = txtEmail.Value;
 }