C# get/set value of html input field - c#

Is it possible to get the value of the html input field (expiration) in C# after the user clicks outside of the field? And set the value from database to the html input field on page load or on refresh.
Here is my code:
<ext:Panel runat="server" Border="false">
<Content>
<label for="expiration"> Unknown date 2 </label>
<input id="expiration" type="text" placeholder="DD(uu)/MM(uu)/YY(uu)" class="masked" title="title" runat="server"
pattern="^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$|(uu\/([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1])\/[0-9]{1,4})|(uu\/uu\/[0-9]{1,4})|(uu\/uu\/uu)"
data-valid-example="01/05/18">
</Content>
</ext:Panel>

It should normally be done with Request.Form["elementName"].
In this case, Request.Form["expiration"] to access its value, change your input to use name instead of id <input name="expiration" ... /> for it to work

Related

how to get value of html Input text placed inside ListView in asp.net C# code

I am having List view with Input Text box
If I am using asp TextBox and trying following Code while ItemUpdating, i am getting the value
obj.FName = (ObjView.Items[e.ItemIndex].FindControl("txtFName") as TextBox).Text;
how I have replace asp text box with html input text in aspx page, after which I am not getting the value
To get html input value in the code-behind, first take input as follows:
<input type="text" runat="server" id="Details" value= '<%# Eval("Details") %>' />
Then in the code-behind, use the following:
string details = ((HtmlInputText)row.FindControl("Details")).Value;
By the way, without controls, you can use the following for html inputs:
string details = Request.Form["Details"];
In that case, you should add a name attribute with the name Details.
To access any html input in your code behind, you must add runat="server" attribute for the control. Like below.
<input type="text" name="txtFName" id="txtFName" runat="server" />

How to Get the Value of HTML5 date element in c# Asp.net

I am using C# Asp.Net for creating a website. I have created an HTML5 date element in the web page. The date element is along with many other .Net elements (i.e textbox). I am unable to retrieve the value of date element.Below is my code
<asp:TextBox ID="TextBox10" runat="server"></asp:TextBox>
<input type="date" id="myDate" name="sel_date" value="YY-MM-DD" />
I have tried tried many methods from stackoverflow but nothing worked. i.e
1- String.Format("{0}", Request.QueryString["sel_date"]); // Not Working <br>
2- myDate.Value; <br><br>
I can get the value of textbox as its a .Net element but how can I get the value of Html5 Date element ?.
Try this code in .Aspx file
<input type="date" runat="server" id="myDate" name="sel_date" value="YY-MM-DD" />
and Access the value like this in .Aspx.cs file
String DOB = myDate.Value;
You will never get the value of HTML control directly in code behind.
Just a clarification to the above statement before it becomes "truth" :)
Yes, you can get any/all input field values, whether they are "server side control" or not (plain HTML <input />), from "code behind".
There is nothing wrong with this plain HTML5 <input /> field, it doesn't have to be a "server side" control:
<input type="date" id="myDate" name="sel_date" value="YY-MM-DD" />
It will be in the Request.Form collection on POSTback:
Request.Form["sel_date"];
Request["sel_date"];
The original code "didn't work" because it was looking for it in Request.QueryString["sel_date"]
Hth....

display asterisk instead of no value in password field

I would like to display asterisk instead of no value in password field.
When I am setting up the field as type=text then I can see the plain text. When I set the type as password then I am getting blank field.
Is there a way to display the value as asterisks?
<input type="text" id="txtPwd" runat="server" />
This is the field I have.
Thanks in advance, Laziale
You can use the HTML5 placeholder attribute:
<input type="password" placeholder="●●●●●●●●●">
Example: http://jsfiddle.net/AJ2kn/
If you want the user's text input to be replaced by asterisks you need to use a password input—a text input just won't do it:
<input type="password" id="txtPwd" runat="server" />
However, the default value will be blank (this applies to text input as well). If you want to initialize this to a default value, you can do it like this:
<input type="password" value="dummy" id="txtPwd" runat="server" />
Note: I highly recommend not putting a users' password as the default value of a password field! It may compromise your users' password.
If you want to display some asterisks as placeholder text when the field is empty you can do that with with the placeholder attribute (again, this applies to text input as well):
<input type="password" placeholder="*****" id="txtPwd" runat="server" />

How to solve the following compilation error in C#?

My .aspx looks like the following:
<div id="valueIntroduction" type="text" class="labelarea" runat="server">
<input name="$labeluniversityid" type="text" id="labeluniversityid" style="color:#7D110C;border:0;background-color:#C0D5F2;width:100%" />
</div>
.cs File looks like:
if (results.Read())
{
labeluniversityid.value = results["TEXT_CONTENT"].ToString();
}
Exactly what am trying do is am getting the data from the database and displaying it in the valueIntroduction div. That is workiing fine. Now i added a text box with readonly mode. So that in my page if I press EDIT button, the value could be edited.
Use a TexBox component:
<asp:TextBox ID="labeluniversityid" runat="server" CssClass="yourcssclass"></asp:TextBox>
As for the styling:
.yourcssclass
{
color:#7D110C;
border:0;
background-color:#C0D5F2;
width:100%
}
Then, in your code behind you can easily use it like this:
labeluniversityid.Text = results["TEXT_CONTENT"].ToString();
Keep in mind that ASP.NET Controls are translated into common HTML tags, so you can wrap it and style it as you would with any other normal input of type text.
Also: type="text" is not valid for div
Try putting runat="server" attribute in <input id="labeluniversityid"> tag.
Or use a <asp:TextBox> control as areks suggests.
You need to add -
runat="server"
to your input field
Or, even better, use an
<asp:textBox ..>

Getting data from an Input control in to code behind

I am using asp.net C#
I am also using the jQuery UI Calendar control.
calendar control
It appears the calendar control wants to work with an input control with an ID of "datepicker".
<input type="text" id="datepicker" />
I want to use value of the input control in my code behind but seeing how it is not an asp.net control I am not sure how I can reference it in the code behind.
Does anyone know how to get the value of the input control in the code behind?
use
<input type="text" id="datepicker" name="datepicker" />
and in the code behind:
Request.Form["datepicker"]
In fact, Form property of Request is populated with form values.
Here you have to mention your form name which contains input tag
<input name="txtemail" id="txtemail" runat="server" type="text" class="cssspa" form="form"/>
in C#
string email = txtemail.Value.ToString();
now u can get data from textbox
If you want to use basic form pulling, you need to add a name attribute:
<input type="text" id="datepicker" name="datepicker" />
if(Page.IsPostBack)
{
string value = Request.Form["datepicker"];
}
Alternatively, you can mark is as an HtmlInputControl
<input type="text" id="datepicker" runat="server" />
System.Web.UI.HtmlControls.HtmlInputControl datepickerControl = this.FindControl("datepicker") as System.Web.UI.HtmlControls.HtmlInputControl;
if(datepickerControl != null)
string value = datepickerControl.Value;
You can access the value via the Request object like onof wrote, or you can turn every html tag into a server control by adding the attribute runat:
<input type="text" id="datepicker" runat="server" ClientIdMode="static" />
The attribute ClientIdMode="static" ensures that the tags ID is not changed by the ASP.NET runtime.
Then you can access the input by the autogenerated member datePicker.
Since you explicitly say code-behind, I assume you probably want to stay with the WebForms/Controls model? With clientidmode you can do that while also having a determinite id value for the calendar.
<asp:TextBox id="datepicker" ClientIDMode="Static" runat="server" />
Then you can interact directly with the control from the code-behind as normal.
But, the calendar control does not require a specific ID. You can initialize it with whatever ID you want
<asp:TextBox id="DateTextBox" runat="server" />
<script>
$(function() {
$( "#<%=DateTextBox.ClientID%>" ).datepicker();
});
</script>

Categories