0

我开始学习简单的 C#。请帮助我如何将HTML参数传递给.ASPX页面到.CS页面?

索引.axpx 页

     <div class="title">
                            <h4>
                                User Login</h4>
                            <div class="option">
                                Sign up for free &raquo;</div>
                        </div>
                        <div class="content">
                            <form method="post" runat="server">
                            <div>
                                <input type="text" runat="server" id="username" name="username" value="" placeholder="username"/>


                            </div>
                            <div>
                                <input type="password" runat="server" id="password" name="password" value="" placeholder="password"/>
                            </div>
                            <div>
                                <input type="button" runat="server" value="Submit" id="submit" onclick=" button_onclick()" />
                            </div>
                            </form>
                            <!-- ## / Panel Content  -->
                        </div>

索引.aspx.cs 页

 public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        private void button_onclick(object sender, EventArgs e)
        {         
            string uname = Request[username.UniqueID];
            string pass = Request[password.UniqueID];
        }

    }
4

2 回答 2

1

输入元素是 runat server 并且可以直接引用,所以只需这样做:

public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        private void button_onclick(object sender, EventArgs e)
        {         
            string pass = password.Text;
            string uname = username.Text;
        }

    }
于 2013-10-02T04:59:45.757 回答
0

请注意,每个服务器元素都可以通过它们的 ID 访问:

runat="server" id="password"

在服务器端:

string passwd = password.Text;

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.aspx

于 2013-10-02T05:02:22.840 回答