HiddenField1 value is empty - c#

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());
}

Related

Passing parameters beetween .aspx and .aspx.cs

I am using Visual Studio 2015 Ultimate. I have put a raw HTML web form in my Test.aspx file and want to pass the parameters of form-fields (like : Username, password, mobile no. etc.) from Test.aspx to Test.aspx.cs file. Although, I have written the code for passing the values in the button click function, I can't get the data. I have written "string u = Request.QueryString["username"];" for username field (without quotes) and so on and another line to print the value in that form itself, that is "Response.Write(u);" (without quotes). Here, I can't see the printed values in my form. How can I solve this issue?
Register.aspx file-
<!-- Start Register Section -->
<div id="login-page">
<div class="layer-stretch">
<div class="layer-wrapper">
<div class="layer-container">
<form class="form-container" action="Register.aspx" method="post" enctype="multipart/form-data" runat="server">
<input type="hidden" name="_token" value="15276e55e6cdfa6911f440f75f64501dc97cc6f4a19102dddb4c47f0c4dd1523ad639943996afef209d6a358056f3b3389a9bcb175b7413ef3547589673a2b7d">
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label form-input-icon">
<i class="fa fa-user-o"></i>
<input class="mdl-textfield__input" type="text" name="fullname" pattern="[A-Z,a-z, ]*" id="register-first-name" runat="server">
<label class="mdl-textfield__label" for="register-first-name">الاسم الكامل<em> *</em></label>
<span class="mdl-textfield__error">الرجاء إدخال اسم كامل صالح!</span>
</div>
<!--<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label form-input-icon">
<i class="fa fa-user-o"></i>
<input class="mdl-textfield__input" type="text" name="lastname" pattern="[A-Z,a-z, ]*" id="register-last-name">
<label class="mdl-textfield__label" for="register-last-name">الكنية <em> *</em></label>
<span class="mdl-textfield__error">الرجاء إدخال الاسم الصحيح!</span>
</div>-->
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label form-input-icon">
<i class="fa fa-envelope-o"></i>
<input class="mdl-textfield__input" type="text" name="email" pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,3}$" id="register-email" runat="server">
<label class="mdl-textfield__label" for="register-email">البريد الإلكتروني <em> *</em></label>
<span class="mdl-textfield__error">الرجاء إدخال بريد إلكتروني صحيح!</span>
</div>
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label form-input-icon">
<i class="fa fa-phone"></i>
<input class="mdl-textfield__input" type="text" name="mobile" pattern="[0-9]*" id="register-mobile" runat="server">
<label class="mdl-textfield__label" for="register-mobile">رقم الهاتف المحمول <em> *</em></label>
<span class="mdl-textfield__error">الرجاء إدخال رقم الجوال صحيح!</span>
</div>
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label form-input-icon">
<i class="fa fa-key"></i>
<input class="mdl-textfield__input" type="password" name="password" id="register-password" runat="server">
<label class="mdl-textfield__label" for="register-password">كلمه السر <em> *</em></label>
<span class="mdl-textfield__error">الرجاء إدخال كلمة المرور صالحة (الحد الأدنى 6 حرف)!</span>
</div>
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label form-input-icon">
<i class="fa fa-key"></i>
<input class="mdl-textfield__input" type="password" name="confirmpassword" id="register-confirm-password" runat="server">
<label class="mdl-textfield__label" for="register-confirm-password">تأكيد كلمة المرور <em> *</em></label>
<span class="mdl-textfield__error">الرجاء إدخال تأكيد كلمة المرور (الحد الأدنى 6 حرف)!</span>
</div>
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label form-input-icon form-bot-check">
<i class="fa fa-question"></i>
<input class="mdl-textfield__input" type="number" name="bot-check" id="register-bot" runat="server">
<label class="mdl-textfield__label" for="register-bot">ما هو 10 زائد 3 =<em>* </em></label>
<span class="mdl-textfield__error">الرجاء إدخال القيمة الصحيحة!</span>
</div>
<!--<div class="login-condition">بالنقر على "إنشاء حساب" فإنك توافق على موقعنا<br />شروط & الظروف</div>-->
<div class="form-submit">
<!-- <button class="mdl-button mdl-js-button mdl-js-ripple-effect button button-primary" name="register" onclick="Submit_Click">إصنع حساب</button>-->
<asp:Button ID="Button1" runat="server" OnClick="Submit_Click" Text="Submit" class="mdl-button mdl-js-button mdl-js-ripple-effect button button-primary" />
<asp:Button ID="Button2" runat="server" Text="Register" class="mdl-button mdl-js-button mdl-js-ripple-effect button button-primary" OnClick="Button2_Click" />
</div>
<div class="login-link">
<span class="paragraph-small">هل لديك حساب؟</span>
تسجيل الدخول
</div>
</form>
</div>
</div>
</div>
</div>
<!-- End Register Section -->
Register.aspx.cs file-
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MDC_web {
public partial class Register: System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
}
protected void Submit_Click(object sender, EventArgs e) {
System.Diagnostics.Debug.WriteLine("Register button has been clicked!!");
string fn = String.Format("{0}", Request.Form["fullname"]);
Response.Write("<script language='JavaScript'> alert('jhkjhjk');</script>");
Response.Write(fn);
string em = Request.QueryString["email"];
Response.Write(em);
string mob = Request.QueryString["mobile"];
Response.Write(mob);
string p = Request.QueryString["password"];
string cp = Request.QueryString["confirmpassword"];
/*if (p.Equals(cp))
{
string c = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
SqlConnection con = new SqlConnection(c);
con.Open();
SqlCommand cmd = new SqlCommand("insert into Register (R_Name, Email, Mobile, R_Pass) values ('" + fn + "','" + em + "'," + mob + ",'" + p + "')", con);
int r = cmd.ExecuteNonQuery();
if (r == 1)
{
Response.Write("<script language='JavaScript'> alert('Registration is successfull!!');</script>");
}
}*/
}
/*protected void Button2_Click(object sender, EventArgs e)
{
Response.Write("<script language='JavaScript'> alert('jhkjhjk');</script>");
//Response.Write(fn);
}*/
}
}
for use form-fields values in .cs you have to do targetting like this
string Username = txtUsername.Value.Trim(); (txtUsername is the name of field in aspx)
string Password = txtPassword.Value.Trim();
string Mobile = txtMobile.Value.Trim();
the query string is used for passing value trough aspx pages not for aspx to cs
You can access asp id from the .aspx in your .aspx.cs.
just give them a unique ID and runat like so:
.aspx
<asp:TextBox runat="server" ID="txtUsername"></asp:TextBox>
.aspx.cs
var username = txtUsername.Text;
this is for receiving the text of the textbox. If you want to set the text then simply change it to txtusername.Text = "test test". You can to this also with asp:labels, asp:button etc.
Check this post for more informations
If you want to do that on a button click just make use of the postback which happens after the click.
.aspx
<asp:TextBox runat="server" ID="txtUsername"></asp:TextBox>
<asp:Button runat="server" ID="btnSumbit" Text="submit"></asp:Button>
.aspx.cs
protected void btnSumbit_Click(object sender, EventArgs e){
var username = txtUsername.Text;
//do username stuff in here
}
if you need a Tutorial for a login form with websforms check one of these:
https://msdn.microsoft.com/en-us/library/ff184050.aspx
https://www.youtube.com/watch?v=QoPABrUknsE
Do not transmit a password, in plain text, via a query string. Set up a https connection for your production site - you can do it free with Let's Encrypt. You say you added a "raw HTML form" - add plain HTML elements but do not add an extra form element because ASPNET can only handle a single form with a runat="server" attribute.
Then, to use plain HTML, just add runat="server" so that you can grab the data on the server side:
Test.aspx:
User Name: <input type="text" name="userName" runat="server">
Test.aspx.cs
string userName = userName.Value.Trim();
UPDATE:
If you're using a master page, why have you got a form tag in the aspx? The master page usually contains the form tag so it looks like you're copying and pasting code from somewhere else. Remove the form tag from your aspx.
Don't pass sensitive information in the URL via a query string. Use a single form tag in your master page, then in your code-behind:
string fName = register-first-name.Value;
You're using ASPNET so there is no point in the pattern attribute. Simply add a Regular Expression validator control.

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-");
}

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.
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.

Categories