1

我对 aspx 开发很陌生,我在 aspx 代码和 aspx.cs 的连接方面遇到了很多困难,正是我遇到了以下问题:

显示字符.aspx:

<form id="form1" runat="server">
<div>   
    <div>Champion name: </div> <div><input id="Champ_name" type="text" /></div>
    <div>Champion Icon URL: </div> <div><input id="Champ_icon" type="text" /></div>
    <div>Champion Subtext: </div> <div><input id="Champ_subtext" type="text" /></div>
    <div> Free to play :</div><div><input id="Champ_freetoplay" type="checkbox" />
</div>
<div>Positions:</div>
<div>
        <input id="Top" type="checkbox" /> Top
        <input id="Mid" type="checkbox" /> Mid
        <input id="Jungle" type="checkbox" /> Jungle
        <input id="Carry" type="checkbox" /> Carry
        <input id="Support" type="checkbox" /> Support
</div>
</div>
    <input id="Champ_Submit" type="submit" value="submit" />

DisplayChars.aspx.cs

 if (IsPostBack)    
        {
            //NameValueCollection nvc = Request.Form.GetValues
            //Champion t1 = new Champion(Request.Form.Get("Champ_Name"), Int32.Parse(Request.Form.Get("Champ_freetoplay")), Request.Form.Get("Champ_subtext"), Request.Form.Get("Champ_description"), "10110");
            //t1.persistChampion();
            string temp = Request["Champ_name"];

所以我正在努力以某种方式获取表单值。我试过了Request.Form.GetValuesRequest.Form.Get甚至Request["Form_id_Name"]

问题是,如果这种方法是正确的,正如我在面向对象编程中所经历的那样,但不是在这种 HTML aspx 伪服务器代码和其背后的 cs 文件的组合中。

4

2 回答 2

2

如果您添加runat="server"HTML 标签,并且您可以从代码隐藏中访问它们的属性:

// DisplayChars.aspx:
<input id="Champ_name" type="text" runat="server" />
...

// DisplayChars.aspx.cs:
string champName = Champ_name.Value;
于 2012-11-08T11:10:50.710 回答
1

虽然你可以做

Request.Form["Champ_name"]

这不是 asp.net 的方式。您必须通过添加使该元素成为服务器控件,runat="server"以便您可以从后面的代码中引用它。

<asp:Button ID="Champ_name" runat="server" OnClick="button_Click" Text="Hello World" />

然后在您的代码隐藏中添加一个方法以在单击该按钮时触发:

protected void button_Click(object sender, EventArgs e) 
{
   // logic processing here
}

如果您需要找出按钮的文本是什么:

string text = Champ_name.Text;

基本上,ASP.NET 不正常依赖Request.Form。您将控件设置为,runat="server"以便您可以在回发时直接从代码隐藏中处理它们。

于 2012-11-08T11:12:50.330 回答