C#/aspx.net process Post Values form - c#

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.

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;

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.

Related

Send data from HTML form to server using ASP.NET

I'm trying to take the input from an HTML form and send it to the server to be added to a sqlite database. I'm struggling a little with the implementation and am wondering if I am on the right path or need to completely change course.
Currently when I click the button it redirects to the aspx page which displays another form and the data that I put in there will be added to the database, but I would like to be able to just send the data from the html page.
Here is what I've got for the HTML form where I'm letting the user input the data.
<form method="POST" action="players.aspx">
<input type="text" name="playername" id="playername" placeholder="Player"/>
<input type="text" name="points" id="points" placeholder="Points" />
<input type="text" name="steals" id="steals" placeholder="Steals" />
<input type="text" name="blocks" id="blocks" placeholder="Blocks" />
<input type="text" name="assists" id="assists" placeholder="Assists" />
<input type="text" name="mpg" id="mpg" placeholder="MPG" />
<input type="text" name="shotpct" id="shotpct" placeholder="Shot %" />
<input type="text" name="threepct" id="3pct" placeholder="3 %" />
<input type="submit" value="add player" id="addbtn" name="addbtn" />
</form>
Here's my aspx file, this what I have come up with after some googling
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="playername" runat="server"></asp:TextBox>
<asp:TextBox ID="points" runat="server"></asp:TextBox>
<asp:TextBox ID="steals" runat="server"></asp:TextBox>
<asp:TextBox ID="blocks" runat="server"></asp:TextBox>
<asp:TextBox ID="assists" runat="server"></asp:TextBox>
<asp:TextBox ID="mpg" runat="server"></asp:TextBox>
<asp:TextBox ID="shotpct" runat="server"></asp:TextBox>
<asp:TextBox ID="threepct" runat="server"></asp:TextBox>
<asp:Button ID="addbtn" runat="server" Text="Button" OnClick="Submit" />
</div>
</form>
</body>
And finally here is my method in the aspx.cs file for loading the data into the database.
protected void Submit(object sender, EventArgs e)
{
NBAPlayer player = new NBAPlayer();
player.PlayerName = String.Format("{0}", Request.Form["playername"]);//playername.Text;
player.Points = Convert.ToInt32(String.Format("{0}", Request.Form["points"]));
player.Steals = Convert.ToInt32(String.Format("{0}", Request.Form["steals"]));
player.Blocks = Convert.ToInt32(String.Format("{0}", Request.Form["blocks"]));
player.Assists = Convert.ToInt32(String.Format("{0}", Request.Form["assists"]));
player.MPG = Convert.ToInt32(String.Format("{0}", Request.Form["mpg"]));
player.ShootingPercentage = Convert.ToSingle(String.Format("{0}", Request.Form["shotpct"]));
player.ThreePointPercentage = Convert.ToSingle(String.Format("{0}", Request.Form["threepct"]));
NBAPlayerRepository players = new NBAPlayerRepository();
players.AddRecord(player);
loadTable();
}

how can i get data from text type input, radio button,select tag and checkbox for asp.net C#, in aspx.cs page?

<form id="form1" runat="server">
<div>
<pre>
UR NAME <input type="text" id="text1" name="name"/>
UR AGE <input type="text" id="text2" name="age"/>
UR Gender <input type="radio" id="Gender" name="Gender" /> Male
<input type="radio" id="Radio1" name="Gender" /> Female
Select UR club
<input type="checkbox" id="chckbox1" runat="server" name="ABC" value="ABC" />ABC
<input type="checkbox" id="chckbox2" runat="server" name="ACC" value="ACC" />ACC
<input type="checkbox" id="chckbox3" runat="server" name="APAC" value="APAC" />APAC
<select id="d" name="select_catalog_query1">
<option>Primary</option>
<option>Secondary</option>
</select>
<input type="submit" id="sub" value="Submit" runat="server" onserverclick="show_Info"/>
</pre>
<div><label id="labl1" runat="server" />
</div>
</div>
</form>
I want to show all the info what I get in a label tag id labl1. here is my aspx.cs page's code.
protected void show_Info(object sender, EventArgs e)
{
string txt1 = "us choosen club";
string txt= "ur name"+" "+"UR age"+" ";
labl1.InnerText = txt+" "+txt1;
labl1.Visible = true;
}
how can I get all the data of text,select tag, checkbox into show_Info function??
I'm not sure why are you not using the asp.net controls instead of using HTML controls with runat="server" attribute. You can use asp.net controls in such cases they make thing a lot more easier for you.
By the way you can use the Value property of html controls.
//for input type text
string textValue = text1.Value;
//for checkbox
string checkBoxValue = chckbox1.Value;
// for select-option
string selectedOption = d.Value;
Those controls are not accessible on your code behind (.cs page), you need to add runat="server" property with them.
You can fetch value from Request.Form[] collection
var name = Request.Form["name"];
var age= Request.Form["age"];

How to get ids of checked checkboxes from a nested repeater c# when I click a button?

I am in trouble with nested repeaters. I used two repeaters. Rpt1 and rpt2. I used rpt1ID in rpt1_ItemDataBound to retrieve data from dbo.rpt_2 .I got that output.
<form method="post" action="Default2.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTAwOTY3Njk1OQ9kFgICAw9kFgICAQ8WAh4LXyFJdGVtQ291bnQCAxYGZg9kFgRmDxUBBXdoaXRlZAIDDxYCHwACARYCZg9kFgJmDxUBCXdoaXRlIGNhcmQCAQ9kFgRmDxUBBWJsYWNrZAIDDxYCHwACARYCZg9kFgJmDxUBCWJsYWNrIGNhdGQCAg9kFgRmDxUBBnllbGxvd2QCAw8WAh8AAgEWAmYPZBYCZg8VAQ15ZWxsb3cgZmxvd2VyZBgBBR5fX0NvbnRyb2xzUmVxdWlyZVBvc3RCYWNrS2V5X18WAwUccnB0MSRjdGwwMCRycHQyJGN0bDAwJGNiVGVzdAUccnB0MSRjdGwwMSRycHQyJGN0bDAwJGNiVGVzdAUccnB0MSRjdGwwMiRycHQyJGN0bDAwJGNiVGVzdFtBgK000gWzwpdFcFINp56BH3iEbpu32p0ucdmVkKqQ">
</div>
<div class="aspNetHidden">
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEdAAtOAOd9ybgP4HtwI1kjAG/MIN3G+dZU3QaatNlxgtD/xtMq/m65MPpIiFULd6envm8Yw3LjELwW1svU+QRcZXObI4OhWhKb5ze/nlyzwgtKpgcaK/fYMlrAwV8ml1fRddm1Nj+RQVgfhH19Mqy7JakoOGJJxEuYbMh7gt3d2UEgxjVWyspxiwdSrYPfy2ovFed8sdKhZF2WzHs512/4sp8RUbCI4mLLhd/eyEmnxetg1jh3iXH1t2bNB/y0oxlp17XcDdrSIJMmEc3BKiLo+WAV">
</div>
rpt1 white<br>
<input type="hidden" name="rpt1$ctl00$hfRpt1ID" id="rpt1_hfRpt1ID_0" value="1">
---rpt2 white car <input id="rpt1_rpt2_0_cbTest_0" type="checkbox" name="rpt1$ctl00$rpt2$ctl00$cbTest" checked="checked"><br>
<input type="hidden" name="rpt1$ctl00$rpt2$ctl00$hfCheckedCheckBoxes" id="rpt1_rpt2_0_hfCheckedCheckBoxes_0" value="1">
rpt1 black<br>
<input type="hidden" name="rpt1$ctl01$hfRpt1ID" id="rpt1_hfRpt1ID_1" value="2">
---rpt2 black cat <input id="rpt1_rpt2_1_cbTest_0" type="checkbox" name="rpt1$ctl01$rpt2$ctl00$cbTest" checked="checked"><br>
<input type="hidden" name="rpt1$ctl01$rpt2$ctl00$hfCheckedCheckBoxes" id="rpt1_rpt2_1_hfCheckedCheckBoxes_0" value="2">
rpt1 yellow<br>
<input type="hidden" name="rpt1$ctl02$hfRpt1ID" id="rpt1_hfRpt1ID_2" value="3">
---rpt2 yellow flower <input id="rpt1_rpt2_2_cbTest_0" type="checkbox" name="rpt1$ctl02$rpt2$ctl00$cbTest" checked="checked"><br>
<input type="hidden" name="rpt1$ctl02$rpt2$ctl00$hfCheckedCheckBoxes" id="rpt1_rpt2_2_hfCheckedCheckBoxes_0" value="3">
<input type="submit" name="btnFindCheckBoxes" value="Get Checked Textboxes" id="btnFindCheckBoxes">
</form>
You cant see but there are checkboxes after each rpt2 line. My question is how can I get the ids of checked checkboxes when I click a button. I can get ids of checkboxes but I couldnt find a way how to get them when I clicked a button. my codes are below. I appreciate every answer. And please dont be angry if I did wrong something in this message. I am brand new here and it is my first message.
<asp:Repeater ID="rpt1" runat="server" OnItemDataBound="rpt1_ItemDataBound">
<ItemTemplate>rpt1 <%#Eval("rptName").ToString() %><br />
<asp:HiddenField ID="hfRpt1ID" runat="server" Value='<%#Eval("rpt1ID").ToString() %>' />
<asp:Repeater ID="rpt2" runat="server" OnItemDataBound="rpt2_ItemDataBound">
<ItemTemplate>
---rpt2 <%#Eval("rpt2Text").ToString() %> <asp:CheckBox ID="cbTest" runat="server" Checked="true"/><br />
<asp:HiddenField ID="hfCheckedCheckBoxes" runat="server" Value='<%#Eval("rpt1ID").ToString() %>' />
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
<asp:Button ID="btnFindCheckBoxes" runat="server" OnClick="btnFindCheckBoxes_Click" Text="Get Checked Textboxes"/>
protected void rpt1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
DataRowView row = (DataRowView)e.Item.DataItem;
boolArray();
Repeater nestedRepeater = e.Item.FindControl("rpt2") as Repeater;
d.Parameters.AddWithValue("rpt1Id", row["rpt1ID"]);
nestedRepeater.DataSource = d.GetDataTable("Select * from rpt_2 where rpt1ID=#rpt1Id");
nestedRepeater.DataBind();
}
protected void rpt2_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
DataRowView row = (DataRowView)e.Item.DataItem;
int a = Convert.ToInt32(row["rpt2ID"]);
CheckBox ch = e.Item.FindControl("cbTest") as CheckBox;
if (ch.Checked)
{
findCheckboxes(a);
}
}
private void findCheckboxes(int a)
{
xxxxx.Add(a);
Response.Write(a + " cliecked-");
}

HiddenField1 value is empty

I try to add form jquery on my page. I have this code piece:
<form id="form" class="blocks" action="form.aspx" method="post">
<p>
<label>Name:</label>
<input type="text" class="text" id="name1" name="name" />
</p>
<input runat="server" type="submit" class="btn" onclick="return xy();" value="Submit"/>
</p>
</form>
<form id="form1" runat="server" action="form.aspx" method="post">
<asp:HiddenField ID="HiddenField1" runat="server" />
</form>
And this:
function xy()
{
$("#HiddenField1").val($("#name1").val());
alert($("#HiddenField1").val());
}
This alert works correctly, and I want to use HiddenField1.Value as a string in C#. My code:
protected void Page_Load(object sender, EventArgs e)
{
string string1 = HiddenField1.Value;
}
string1 is null. Why it is null?
It depends. You must first call function xy() for the HiddelField1.value to have a value.
I cant see that you call it.
Like this:
protected void Page_Load(object sender, EventArgs e)
{
//call xy() here
string string1 = HiddenField1.Value;
}
You just need to move you Hidden variable to be in the same Form with the Submit button and remove the second Form tag.
So your HTML should be changed to be like the following:
<form id="form" class="blocks" action="WebForm1.aspx" method="post" runat="server">
<asp:HiddenField ID="HiddenField1" runat="server" />
<p>
<label>
Name:</label>
<input type="text" class="text" id="name1" name="name" />
</p>
<input id="Submit1" runat="server" type="submit" class="btn" onclick="return xy();"
value="Submit" />
</form>
You can use the jquery text() function
function xy() {
$("#HiddenField1").val($("#name1").text());
}

Validating Checkbox and Radio Button in C#

I am using c# for coding!
Below is my html for checkbox and radion button
<input type="radio" style="float: left;" name="documents" id="Checkbox9" value="yes"
runat="server" />
<label style="width: 35px!important;" class="checkbox">
<%=GetResourceString("c_HSGStudent")%>
</label>
<input type="radio" style="float: left;" name="documents" id="Checkbox10" value="no"
runat="server" />
<label style="width: 25px!important;" class="checkbox">
<%=GetResourceString("c_HSGParent")%>
</label>
<input type="radio" style="float: left;" cheked name="documents" id="Radio1" value="yes"
runat="server" />
<label style="width: 35px!important;" class="checkbox">
<%=GetResourceString("c_HSGStudent")%>
</label>
<input type="radio" style="float: left;" name="documents" id="Radio2" value="no"
runat="server" />
<label style="width: 25px!important;" class="checkbox">
<%=GetResourceString("c_HSGParent")%>
</label>
You can see I have two checkboxes and two radio buttons, My problem is that on my submit button click I want to check whether user have checked at-least one checkbox or radio button. It will be good if we can have .NET solution like (customvalidator).
Please suggest!
Thanks
First, add a CustomValidator to your page...
<asp:CustomValidator runat="server" ID="CheckBoxRequired" EnableClientScript="true"
OnServerValidate="CheckBoxRequired_ServerValidate"
OnClientValidate="CheckBoxRequired_ClientValidate">*</asp:CustomValidator>
You can then then validate them from a client side function with a simple jquery call...
<script type="text/javascript>
function CheckBoxRequired_ClientValidate(sender, e)
{
e.IsValid = $("input[name='documents']").is(':checked');
}
</script>
code-behind for server side validation...
protected void CheckBoxRequired_ServerValidate(object sender, ServerValidateEventArgs e)
{
e.IsValid = Checkbox9.Checked || Checkbox10.Checked || Radio1.Checked || Radio2.Checked;
}
Create a custom validator and then check wether the controls meet the criteria in the servervalidate event handler.

Categories