Nate Nate - 4 months ago 19
ASP.NET (C#) Question

How to "send" form data to a C# code, then have C# "send" data back?

This is the HTML form I have, I want to send the user's selection to an aspx.cs file that will process their selection and output something onto the lower textbox.



<!-- Form -->
<form action="lGen.aspx" method="post" id="lForm" runat="server">
<div class="row">
<div class="input-field col s6">
<select id="list" form="lForm">
<option value="" disabled="disabled" selected="selected">Select</option>
<option value="1" class="validate" >Option 1</option>
</select>
</div>
<div class="input-field col s6">
<input placeholder="Instert Text" id="insText" type="text" class="validate" runat="server" />
<label for="system_id"></label>
</div>
</div>
<div class="row">
<div class="input-field col s6">
Checkbox
<p class="black-text">
<input type="checkbox" class="filled-in" id="check1" />
<label for="check1"><span class="black-text">Check 1</span></label>
</p>
<p class="black-text">
<input type="checkbox" class="filled-in" id="check2" />
<label for="check2"><span class="black-text">Check 2</span></label>
</p>
</div>
<div class="input-field col s6">
Radio
<p>
<input class="with-gap" name="licenseType" type="radio" id="radio1" />
<label for="radio1"><span class="black-text">Permanent</span></label>
</p>
</div>
</div>
<div class="row">
<br />
<button class="btn waves-effect waves-light black" onclick="generate_onclick()">
Generate
</button>
</div>
</form>

<!-- TEXT AREA-->
<div class="row">
<div class="input-field col s12">
<textarea id="genText" class="materialize-textarea"></textarea>
<label for="genText"><span class="black-text">Generated</span></label>
</div>
</div>





The aspx.cs I have is this:

namespace lGenerator
{
public partial class WebForm1 : System.Web.UI.Page
{
public string list{ get; set; }
public bool check1{ get; set; }
public bool check2{ get; set; }
public bool radio1{ get; set; }
public string genText { get; set; }

// Generate License on Click button
private void generate_onclick(object sender, EventArgs e)
{
genL();
}

// Creating actual license
private void genL()
{
string phrase = list;

licenseText = phrase;
}
}
}


Just for testing, I wanted to pass the value of whatever they selected on the list and then show it on the textbox below. I'm aware that in PHP, you can use things like $_POST to get values from post but how do I do it with HTML and C#?

Note that I had to change some of the values/IDs and I need to get it working with C# because I will be working with .dlls in the future.

Answer

make those controls as server side control by including the property Runat="server" and then you can access those control instance values anywhere in your code behind (I mean *.cs file).

Comments