hidden variable does not have value after postback asp.net - c#

I have following
<input type="hidden" id="hdnField" name="hdnField"/>
Request.Form.Set("hdnField", x.ToString());
after the page post back the value is not there.
I am new to this, any help would be appreciated.

Source:
You could define a property in your page class and then modify the property value in your code:
protected string HiddenFieldValue { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
HiddenFieldValue = x.ToString();
else
HiddenFieldValue = x.ToString();
}
Then define the hidden form field like this so that it's value is set to the property value:
<input type='hidden' id='hdnField' value='<%=HiddenFieldValue %>' />
If you only want to set the value form the property during a postback or non-postback you could add the condition as well:
<input type='hidden' id='hdnField' value='<% if(IsPostBack) { %> <%=HiddenFieldValue%> <% } %>' />

You should use asp:HiddenField tag provided by asp rather than using the basic HTML input.
<asp:HiddenField ID="hdnField" Value="" runat="server" ClientIDMode="Static" />
Using this, you can read and write value in c# using hdnField.Value and in jQuery using $('#hdnField').val().

Related

Execute the function I wrote in C # by changing the value (input)

How can I execute the function I wrote inside the c # code when I change the input value?
<input id="quantity2" runat="server" type="number" onserverclick="lod_gheymat" value="" min="1" max="20" />
protected void lod_Price(object sender, EventArgs e)
{
lbl_Plural.Text = quantity2.Value;
}
Use a asp:Textbox instead of an input control with ‘runat=server’.
Then set the property AutoPostback to true, and handle the event ‘OnTextChanged’.
See: https://meeraacademy.com/textbox-autopostback-and-textchanged-event-asp-net/ for an example.

How to implement input checkbox's checked action?

In a WebForm I have an input checkbox to which I wanna apply server side action.
For example, when the checkbox is checked, I want to change some label's text.
I have tried to use on client side:
<input id="auto" name="auto" type="checkbox" data-toggle="toggle" data-on="AUTOMAT" data-off="MANUAL" <%= string.IsNullOrEmpty(Request["auto"]) ? string.Empty : "checked" %> />
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
<asp:HiddenField ID="customSwitch1Change" runat="server" Value="0" />
<script>
$('#auto').click(function () {
$('#<%=customSwitch1Change.ClientID%>').val("1");
$('#form1').submit();
});
</script>
I have used this and this for the input checkbox.
On the server-side I have tried:
protected void CustomSwitch1Change(string auto)
{
if (string.IsNullOrEmpty(auto))
{
Label3.Text = $"customSwitch1 was not checked.";
}
else
{
Label3.Text = $"customSwitch1 was checked and the check value is {auto}.";
}
}
But what I've tried is not working.
What I'm doing wrong? Or is there another way to do this?
You don't need the HiddenField. If you change the jQuery to the code below it will do a form post on CheckBox change.
<script>
$('#auto').change(function () {
$('#form1').submit();
});
</script>
Then you can simply get the value in code behind with
string auto = Request.Form["auto"];

How to get the value of a textbox in code-behind

HTML:
<input type="text" runat="server" value="" placeholder="Search" id="searchB" class="styledTB searchB floatLeft" />
C#:
string strSMain;
protected void Page_Load(object sender, EventArgs e)
{
tbSearchMain = (System.Web.UI.WebControls.TextBox)sender;
strSMail = tbSearchMain.text; // gives me the following error: Exception Details: System.InvalidCastException: Unable to cast object of type 'ASP.site_master' to type 'System.Web.UI.WebControls.TextBox'.
strSMain = searchB.text; //.Text is not an option for me
}
Please help me resolve the issue.
I am creating a web application. And the control is in the MasterPage.
Use this:
<input id="searchB" x:Name="searchB" type="text" runat="server" value="" placeholder="Search" class="styledTB searchB floatLeft" />
The id attribute is used client side. x:Name is used for server side manipulation
I am not sure but no textbox will be sender for Page_Load() function.
Check about DataContext Property and how it works.
http://www.wpf-tutorial.com/data-binding/using-the-datacontext/

How to set html input type text value using ASP.NET C#?

I have an html control which I want to set its value ....
here's the control:
<input runat="server" id="first_name_txt" type="text" placeholder="First Name" />
in code behind, I use:
first_name_txt.Value = String.empty;
but the value of my input control still has the old value like "blah-blah" and not set to "".
Its old question , but may help someone.
You have to use Request.Form to get and call .Value to set the value.
HTML
<input runat="server" id="first_name_txt" type="text" placeholder="First Name" />
CODE BEHIND
//To get value:
string myname=Request.Form["first_name_txt"];
// To set value:
first_name_txt.Value="";
<td>
<input type="text" name="date" value="<%= tdate %>" />
</td>
Code Behind :
protected string tdate { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
this.tdate = DateTime.Now.Day.ToString() + "/" + DateTime.Now.Month.ToString() + "/" + DateTime.Now.Year.ToString();
}
Hello it's not that easy to set data into HTML input, but here is a link that may help you [Link].
1) If it didn't work up to you try to set a value and calling it through Javascript and set the text of this input like the gotten value.
2) You can use the [Div] tag using runat="server", clear it and create a new input with same id,name,etc. but different Text value
Try Step 2 as follow(it worked):
<div id="divTitle" runat="server">
<input type="text" class="input_Text" id="Title" name="Title" />
</div>
divTitle.Controls.Clear();
divTitle.InnerHtml = "<input type='text' class='input_Text' id='Title' name='Title' value='" + ds(0)("Title").ToString() + "' />";
Where ds is a data table that came from select query from database
Try put this in postback
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
first_name_txt.Value = String.empty;
}
}
here is very simple way to do this
{ text_Box.Value = "data";}

Defaulting hidden field value using inline server code

I have an ASP.NET page where I defined a property in the code behind as follows:
public int testProperty { get; set; }
In my page I define a hidden field and want to set the value using an inline server code as follows:
<asp:HiddenField ID="hftestProperty" runat="server" Value="<%= testProperty.ToString() %>" />
The problem I am having is that when the control renders in the browser, it renders the value the same way I defined it:
<input type="hidden" name="hftestProperty" value="<%= testProperty.ToString() %>">
Any idea why this is happening?
Try with this:
<input id="hftestProperty" type="hidden" value="<%=testProperty.ToString()%>" />
it render:
<input id="hftestProperty" type="hidden" value="0" />
You're going to have to set the value of the hidden field in the code behind, for example in the Page_Load event:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
hftestProperty.Value = testProperty.ToString();
}
}
You might need to cast testProperty to string for hidden field. If you are going to use the string value of testProperty at Server then testProperty.ToString() will always be available to you.
Try following code:
<input type="hidden" id="hftestProperty" value="<%= testProperty%>" />
For more info you may see this question.

Categories

Resources