2012-11-08 20 views
1

我很新的ASPX发展,我挣扎了很多与ASPX代码和aspx.cs连接,正是我下面的问题:C#/ aspx.net过程中提交值形成

DisplayChars.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文件。

回答

2

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

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

// DisplayChars.aspx.cs: 
string champName = Champ_name.Value; 
+0

好的。我学到了新东西。 – Sami

+0

非常感谢你,有点显而易见,因为我有一个隐藏的文本框已经这个属性! – user1808908

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",以便您可以在回发时直接从代码隐藏中解决它们。