Page.Parsecontrol converts asp:textbox to textarea not input - asp.net

I'm injecting some generated asp.net controls into a page using Page.ParseControl. I'm injecting the markup as follows:
Me.phScript.Controls.Add(Me.Page.ParseControl("<asp:TextBox runat=""server"" id=""txtAreaOfConcern"" TextMode=""multiline"" Rows=""5"" Width=""300"" MaxLength=""5"" />", True))
Now when this is rendered the source html comes out as
<textarea name="txtAreaOfConcern" rows="5" cols="20" id="txtAreaOfConcern" style="width:300px;">
Wheras a textbox added as per normal renders as follows:
<input name="_tbStaticInput" type="text" maxlength="20" id="_tbStaticInput" style="height:104px;width:263px;" />
The reason this is such a problem is that i no longer have the maxlength attribute which is needed for this page.
Is it possible to render an input through control injection, if so how?

You are setting the TextMode to multiline in the string you are passing to ParseControl, you will always get a textarea, that's how the TextBox control works. It will do the same thing if you put that in the markup on the page or instantiated a TextBox class and set the TextMode property to multiline.
Not related to your problem but it seems a strange way to create a control dynamically, the text you are parsing is a string literal and is not dynamic in any way. Why don't you just instantiate a TextBox class and set the properties, that way you get compile time checking that you are setting the properties to valid values. The way you are doing it your string could be changed to "AnIvalidValue" and everything would still compile fine but you would get a run time exception.

Related

Can client-side generated controls affect ViewState?

I have a page that generates new text inputs, checkboxes, selects with JavaScript. So none of these controls has runat="server" set.
I would like to know if these controls are sent to the server on PostBack and become part of the Viewstate, altering it in any way.
The short answer is yes, by assignment.
Dynamically generated plain HTML elements in ASPX page will be treated as LiteralControl elements rather than WebControl elements, hence they don't affect ViewState directly like some ASP .NET server controls. However, since they're placed inside form tag (usually with runat="server" attribute), their values are posted together as postback event stage triggered by submitting the form, which identified as key-value pair in Request.Form collection (the key is recognized by name attribute of literal HTML element).
Suppose you have dynamically generated text box with JS like this:
<input name="FirstName" type="text" />
Then you can retrieve its value using Request.Form:
If Not String.IsNullOrEmpty(Request.Form("FirstName")) Then
Dim firstName As String = Request.Form("FirstName")
And, the important point, you can omit the string assignment above and assign literal text box value to ViewState:
ViewState("FirstName") = Request.Form("FirstName").ToString()
Note that only HTML server controls (e.g. <input runat="server" />) and ASP .NET server controls have control name directly accessible in code-behind and ViewState maintained automatically (unless having EnableViewState attribute set to false).
Additional ViewState reference:
Understanding ASP.NET View State
Related issues:
How to viewstate in normal HTML input in asp.net
Which controls have ViewState maintained?
viewstate for HTML control

Disable html required tag in vb.net

i need to disable the required tag of this textbox when a submit button is click
<asp:Textbox runat="server" id="username" required name="username" type="text" placeholder="myusername"/>
i tried to write the following code but it did not work
Dim tbUserName As TextBox =Page.FindControl("username")
tbUserName.required = False
can you please help me !
Since required is not part of the ASP.NET TextBox control's properties, thus there is no server-side property equivalent.
You can use the following to remove it:
username.Attributes.Remove("required")
Try this:
tbUserName.Enabled = False
Also, "Required" isn't a valid propert for the Textbox control. ref: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.aspx
required is client-side attribute which does not have direct equivalent on server-side.
You can try
username.Attributes("required") = "false"
this works when you're already on the server. If you need to do this on client - handle form's onsubmit event and do something like
$get('username').setAttribute('required', 'false');
Of course the easiest way would be simple remove that attribute from textbox markup.

HTML5 Types in ASP.NET

Site will exclusively be used on mobile devices. So for fields requiring only numeric input, I want to bring up the numeric keypad. I can successfully do this by using an html input element with the type set to tel. I want to add an asp:RequiredFieldValidator to this field and based on MSDN.
I need to set the input to runat="server". When I do this, I get this error;
tel is not a valid type for an input tag.
If I remove the runat="server", I get
Unable to find control id Contract referenced by the ControlToValidate property of ''
Here is my code:
<input type="tel" runat="server" id="Contract"></input>
<asp:RequiredFieldValidator runat="server" ControlToValidate="Contract"
ValidationGroup="IfOnRent" Text="*"
ErrorMessage="Contract Required">
</asp:RequiredFieldValidator>
Am I just out of luck and have to code my own validations?
You may want to leave "type" undeclared in your code "front". You can set this in the code behind (maybe in the Page_Init or Page_Load):
this.Contract.Attributes.Add("type", "tel");
If you have Microsoft .NET Framework 4 Reliability Update 1 (KB2533523) installed, than TextBox accepts new html5 input types (you would need it for UpdatePanel to recognize them on postback too).
Or you can just inherit HtmlInputText in your own control (any .net version - any input type):
[ToolboxData("<{0}:Input runat='server' />"), Themeable(true)]
public sealed class Input: HtmlInputText { }
If you are using C#
Contract.Attributes["type"]="tel"
Writing inline types for html inputs may produce parse error in .net. So add the above line of code in your PageLoad()
you can use
Make sure that the submit button has the same validationgroup property value as the requiredfieldvalidator

How can I use HTML5 email input type with server-side .NET

As I understand it, the <input type=email> element in HTML5 will render as a simple text field in browsers that do not support the tag. On other browsers it will render properly, like on the iPhone it will bring up the e-mail keyboard layout.
I’d like to use this in a project but my input fields are <asp:TextBox> controls. How can I use the HTML5 element but still access its data server-side like the rest of my fields?
There is an update for .NET framework 4 which allows you to specify the type attribute
http://support.microsoft.com/kb/2468871.
See feature 3 way down the page
Feature 3
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" />
you can try adding the attributes manually, like:
TextBox1.Attributes["type"] = "email";
TextBox1.Attributes["type"] = "url";
TextBox1.Attributes["type"] = "number";
Sorry I'm a bit late to the party, though I think that others can benefit from what I did. I have a page which is HTML 5 though we still have .NET 3.5. We wanted to keep the .NET element, though have the type change to email. I've tried several methods (including Milox above) to no avail, though the one which worked for me was the following: I added a JavaScript property to the element itself inline (when I put it in a script tag it wouldn't pick up for some reason...)
Here is what your tag would look like if you use my changes:
<asp:TextBox runat="server" type="email" onfocus="this.type='email'"/>
Eli
Whether or not it is accessible as a server control, you should be able to access the HttpRequest.Form collection and retrieve the value. No matter what the browser does with the tag, it has to submit a string to the server.
in your .aspx file add
<input type="text" required autofocus placeholder="Email Address"
class="txt-input txt-input-username" ID="myTextBox" runat="server"/>
in your Code Behind .cs
myTextBox.Attributes["type"] = "email";
This Worked For Me
You need to create your own custom control and override the Render routines. Feel free to use either the source code or DLLs

What's the Literal control used for and what's the difference to the Label Control in asp.net?

What Literal control is used for in asp.net? and What is the difference between them and Label control?
The major difference is that the Label Control adds the span tag to the text (property) you set, allowing to apply a style to it:
<span>My Label text</span>
The Literal Control allows you to render any kind of content. You can use it to render scripts, hmtl and any other type of document content. It doesn't change the string you provide in the Text property.
Note: the Label control allows you to render straight HTML too, but it puts all your text in span tags as mentioned. So, for rendering large HTML portions a Literal control is the way to go.
P.S.: In HTML there is a <label> tag. If you use the AssociatedControlId property of the Label control, it will render as HTML <label> (thanks to Ray for pointing that out.)
For example:
<asp:Label runat="server" id="FirstNameLabel" AssociatedControlId="FirstNameTextBox">
Input First Name:
</asp:Label>
<asp:Textbox runat="server" id="FirstNameTextBox" />
Will render as:
<label for="FirstNameTextbox" id="FirstNameLabel">Input first name:</label>
<input type="text" id="FirstNameTextbox" name="FirstNameTextBox" />
See also here on W3 Schools.
One thing also to note is if you are justing using it to display something and have no need for formatting the text use a Literal control. The ViewState is not as heavy with a Literal vs a Label control and when you have many of these on a page using ViewState it can really bloat your page size.
I always ask myself, do I need to apply a custom style or formatting? Yes, use a Label. No, use a Literal.
It is used to display text on the page, the text that is displayed can be set at runtime via server side code.
The label control also has the AssociatedControlId property that associates the label with another control. An example of where this is useful is with a textbox control. Once these are associated, screen readers are more able to give better results.
Another example is a radio button with a label allows you to click on the label and the radiobutton will select if the AssociatedControlId property is set.
MSDN on AssoicatedControlId
As splattne mentions, the label encloses its text in a span, whereas the literal is simply a placeholder. However, be careful in making assumptions about how ASP.Net controls are going to render. It can depend on the user agent that you are using. For instance, the panel control renders as a div in IE, but renders as a table with Firefox.
It will place LITERALLY whatever text you place in it on the page. You can use it to write html, JavaScript or just plain text.
We can use literal control in the title tag whereas label cannot be used in the title tag
Label can be used to set focus on other controls like Textbox.
Whereas Literal simply rander the static text on the web page

Resources