user1808908
user1808908

Reputation: 13

C#/aspx.net process Post Values form

I'm quite new on aspx development, and I'm struggling a lot with the connection of aspx code and aspx.cs, precisely I've following problem :

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"];

So I'm struggling with getting the Form-values some how. I've tried Request.Form.GetValues,Request.Form.Get even Request["Form_id_Name"].

The Question is, if this approach is even right, as I've experience in Object-oriented programming, but not in this combination of HTML aspx pseudo server code, and a cs-file behind it.

Upvotes: 1

Views: 534

Answers (2)

Roland Mai
Roland Mai

Reputation: 31077

While you can do

Request.Form["Champ_name"]

It is not the asp.net way. You have to make the element a server control by adding runat="server" so you can reference it from code behind.

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

Then in your codebehind can add a method to fire when that button is clicked:

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

If you needed to find out what the text of the button is:

string text = Champ_name.Text;

Basically, ASP.NET doesn't rely on Request.Form normally. You set the controls to runat="server" so you can address them directly from code-behind on postback.

Upvotes: 1

Dennis Traub
Dennis Traub

Reputation: 51634

If you add runat="server" to you HTML tags and you can access their properties from the code-behind:

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

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

Upvotes: 2

Related Questions