ASP TextBox loses ViewState - html

I have a simple TextBox:
<asp:TextBox runat="server" ID="tbText" MaxLength="3" Width="80" type="number">
</asp:TextBox>
When I add type="number" the control loses its content after a post-back. I know there is no native support for this but it's a comfortable way to make sure only numbers can be entered.
How can I make the value remain in the TextBox and add the attribute type="number"?

In your Page_Load() method you can maintain the value and add the attribute in IsPostBack check:
If(IsPostBack)
{
tbText.Attributes.Add("type", "number");
tbText.Attributes["value"]= tbText.Text;
}
Explanation:
IsPostBack returns true which means a post-back is happened
tbText.Attributes.Add adds attribute type="number" in textbox
tbText.Attributes["value"] used to set the entered value in textbox

Use TextMode
<asp:TextBox ID="tbText" runat="server" TextMode="Number" />

Related

asp input field type date

For simple html input we use:
<input type="date" placeholder="From" />
How can we use this method for asp field:
<asp:TextBox ID="from_date" runat="server" placeholder="From"></asp:TextBox>
when user clicks the field, calendar should appear.
Question still unanswered:
Here is even more detail to back up my answer:
From Microsoft: https://support.microsoft.com/en-us/kb/2468871
"New syntax lets you define a TextBox control that is HTML5 compatible. For example, the following code defines a TextBox control that is HTML5 compatible":
<asp:TextBox runat="server" type="some-HTML5-type" />
-
Original:
I use asp.net and you can just use it like normal. An asp.net textbox ends up as a normal input element once processed by the server.
If you are using 4.0 or above you can just do :
<asp:TextBox ID="from_date" runat="server" placeholder="From" type="date"></asp:TextBox>
If you try this and it does not work, it is probably the browser you are using, type="date" is not supported by any IE, or any Firefox browsers, chrome is one of the only browsers that support type="date" right now.
See can i use for browser support: http://caniuse.com/#feat=input-datetime
I recommend that you find another option if you need browser support.
You can also use AJAX Calender Extender
<asp:TextBox ID="from_date" runat="server" />
<ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server"
TargetControlID="from_date" PopupButtonID="from_date">
</ajaxToolkit:CalendarExtender>

to find out whether you have "clicked" of the checkbox if it is empty or not

it is such that I have to find out whether you have "clicked" of the checkbox if it is empty or not, because if it is empty then it should come up with an error that you need to check on before you can move forward.
<div class="checkbox pi-margin-bottom-20">
<label class="pi-small-text">
<asp:CheckBox ID="CheckBoxBetingelser" runat="server" /> Jeg accepterer Betingelser for brug
<asp:CustomValidator
ID="CustomValidator1"
runat="server"
ErrorMessage="Godkende vores betingelser"
Font-Bold="True"
ControlToValidate="CheckBoxBetingelser"
ValidationGroup="opretbruger"></asp:CustomValidator>
</label>
</div>
What is the error that it says is this: Control 'CheckBoxBetingelser' referenced by the ControlToValidate property of 'CustomValidator1' cannot be validated.
ControlToValidate property should point to input control or left blank for the CustomValidator control.
A reference from MSDN:
and use this for validation checkbox

how to bypass required field validation when clicking the cancel button?

I've used HTML input type for my textboxes to utilize the required attribute. now my dilemma is i cannot go out of the page without filling-up the fields with required attributes. Ive tried using causeValidation set to false but its not working. i can not change my textboxes to asp textboxes because it's going to be a large changes in the page. is there any other way to this?
<asp:Button runat="server" ID="buttonCancel" CssClass="cu-btn-direction" style="float:right; margin-right: 15px; margin-bottom: 60px;" Text="Cancel" CausesValidation="false" />
EDIT:
I tried adding validation group but didn't work... what works is setting the UseSubmitBehavior to false http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.usesubmitbehavior%28v=vs.110%29.aspx but as explained in the link it will not work without js enabled. any other way?
use formnovalidate
<input type="submit" value="Cancel" formnovalidate>
this bypass all form validation like required

asp Textbox & HTML Input - Values always empty

I have an asp Textbox for a username and a HTML input for a password, these are both server controls and are contained in the master page (in a modal popup).
<input runat="server" Class="login-input" id="txtPassword" placeholder="Password" type="password"
<asp:Textbox style="float:left; margin-right:25px;" runat="server" id="txtUsername" Class="login-input" Placeholder="Username" name="txtUserName"/>
I also have a login button hooked up to a handler in the code behind. However the value of the 2 input boxes is always empty.
I had to include UseSubmitBehavior="False" on my button as this was the only way i could get my click event to
<asp:Button UseSubmitBehavior="False" runat="server" Class="login-button" ID="btnLogin" Text="Login" />
Not sure what's going on here, i have checked my code and definitely only have 1 form tag, which i heard can cause this.
It turned out that at runtime my modal was not contained within the tags.
Therefore i had to use
$('#divname').parent().appendTo($("form:first"));
within my script to move the elements back within the form tags.
(There were 2 modal elements outside) I found this out by using f12 dev tools/firebug.

Is their any asp net server control which could render <label> tag?

I want to render a <label> tag. But want to set some of it's properties while it's rendering like for and text value.
Actually my real problem is I want to associate a label with a radio button and this is the code so far I have:
<asp:RadioButton ID="Option4" GroupName="A" runat="server" />
<label for='<%=Option4.ClientID %>' id="lblOption4" runat="server">4</label>
But the problem with this code is that it is not working and rendering the for attibute's value as it is i.e. <%=Option4.ClientID %>. :-(
Is their any asp net server control which would render tag?
I don't want to set the Text property of the radio button due to some CSS limitations so plz do not give answers like why don't you set the Text property of the radio button.
if this is .NET 2.0 or later, then use the ASP.NET LABEL control.
<asp:RadioButton ID="Option4" GroupName="A" runat="server" />
<asp:Label AssociatedControlId="Option4" Text="4" runat="server" />
If this is to ultimately handle accessiblity issues you could try the following tutorial: Creating an Accessible LABEL control in ASP.NET