Dynamically create asp:TextBox that accepts only Numbers - asp.net

I would like to know if it's possible to create asp.net TextBox from code behind that accepts only numbers. Is there any attribute I can Use?
I have this but If I put "a" it's Ok...
HtmlGenericControl divQta = (HtmlGenericControl)e.Row.FindControl("divQta");
TextBox txtQta = new TextBox();
txtQta.Attributes["type"] = "number";
txtQta.Text = dt.Rows[e.Row.RowIndex][2].ToString();
divQta.Controls.Add(txtQta);

You can set the text box's TextMode property to TextBoxMode.Number:
txtQty.TextMode = TextBoxMode.Number;
See this MSDN page for details.
Note: that enum value is only available since .NET 4.5. And since it makes use of a HTML 5 feature, it might not work in all (older) browsers.

Related

How can I access custom Textbox attributes in ASP.Net?

I am using/abusing CSS classes and custom html attributes to provide default data to a set of textboxes. The code-front for this looks like the following (with some supporting javascript to handle checking/setting the default data when the field is blank):
<asp:TextBox ID="TXT_LenderName" class='defaultText' data-default='Institution Name' runat="server"></asp:TextBox>
This works.
I am working on the code-behind to process this form. I would like to be able to compare the value of the TXT_LenderName.Text to the value of the data-default attribute, but I haven't been able to find a way to get the value of a custom html attribute. Suggestions?
This is tested and worked
string customAttrDataDefault = TXT_LenderName.Attributes["data-default"];
txtpassword.Attributes.Add("value","Password value");
try this:
TXT_LenderName.Attributes["AttributeName"]= value;//here get or set the value.
If the control, like the TextBox control inherits from the System.Web.UI.WebControls.Control class then it should have an Attributes property which is a name value pair collection of the control's attributes.

ASP.NET dynamic controls and validators - Unable to find control id '...' referenced by the 'ControlToValidate' property of '...'

I have seem many forum posts / responses on this subject but I am no closer to a solution.
I am loading a repeating data structure from the DB into UpdatePanel controls in an event handler. The number of repeats varies so controls are all created and added to a container panel dynamically. The user has the ability to edit the data so I am also dynamically creating validator controls (RegularExpressionValidators)
The problem is in setting the ControlToValidate property of the validator. No matter how I set this I get the Unable to find control id '...' referenced by the 'ControlToValidate' property of '...' error response from the server which is then raised by ASP.NET AJAX JS.
The short version of the control adding code looks like this:
TextBox licenseCode = new TextBox();
licenseCode.ID = this.getNextId(); // generated ID contains only [A-Za-Z], [0-9], :, -, .
licenseCode.MaxLength = 50;
this.purchaseEdit.Controls.Add(licenseCode); // this.purchaseEdit is an asp:Panel
RegularExpressionValidator licenseCodeVal = new RegularExpressionValidator();
licenseCodeVal.ControlToValidate = licenseCode.ClientID;
licenseCodeVal.ID = this.getNextId();
licenseCodeVal.ValidationGroup = this.purchaseEditSave.ValidationGroup; // save button within the panel
licenseCodeVal.ValidationExpression = #"^.{1," + licenseCode.MaxLength.ToString() + "}$";
licenseCodeVal.ErrorMessage = "Between 1 and " + licenseCode.MaxLength.ToString() + " chars.";
this.purchaseEdit.Controls.Add(licenseCodeVal);
As you can see both controls are added to the same container, the validator is added after the TextBox, and the ControlToValidate property is currently set to the ClientID of the TextBox. I have tried the ID value too but no joy.
The really irritating thing is that if I don't add the validator, and using a breakpoint inspect the ClientID of the TextBox, I can inspect the DOM of the updated page and find the ID attribute of the TextBox:
<input type="text" id="ctl00_ContentBody_PCTL::4890cc3a-8d07-4dee-aa9f-bdd8735fcaf8::licCode"... />
As far as I can tell I'm not doing anything wrong, so why am I getting this error??
Any suggestions much appreciated.
You should not use the ClientID but the ID of the TextBox.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.basevalidator.controltovalidate.aspx
"I have tried the ID value too but no joy."
But you didn't get an exception and only the validation didn't work?!
My suggestion is to wrap the control and the validator in an UserControl and add this dynamically to your container-control. On this way you can use fix ID's because they all have distinct NamingContainers.
The code will be cleaner. Encapsulation also increases reusability.
ARGH so I finally figured this thing out - it came down to special characters in the ID. I was having an issue with a JQuery selector which contained ':' and figured I should just check if that was causing the validator issues - it was.
In the end I created my GUIDs without any hyphens and replaced ':' with '_' and the problem went away. This question helped.
I haven't seen anything from MSDN saying certain characters are not permitted in IDs, but then I tend to refer to MSDN with specific problems after jumping in, so maybe there is something on there.. See comment below

ASP.NET How do I set the position of a control at runtime?

I'm adding a label to a page using the code below, how would I set the position of the label (i.e top right)?
Label lbl = new Label();
lbl.Text = "Test";
lbl.ForeColor = System.Drawing.Color.Black;
lbl.Font.Size = 10;
lbl.Font.Bold = false;
lbl.Font.Name = "Arial";
Page.Controls.Add(lbl);
Thanks
Update:
I really need to avoid using anything that can be altered via editing a file running on the server which is why I'm trying to do this at runtime.
Add a PlaceHolder control to your page in the position you want to add the label to and then add the control as a child control of the PlaceHolder eg.
<asp:PlaceHolder ID="LabelPlaceHolder" runat="server">
</asp>
And then...
LabelPlaceHolder.Controls.Add(lbl);
Generally, though, dynamically adding controls at run-time is something you want to avoid. As is setting styling through in-line properties (use CSS instead). If you only want the Label to appear under specific circumstances then add it to the page with it's Visible property set to False and then set it to true to when you want to see it.
I'd suggest you do all formatting etc. with CSS - so, at runtime all you would specify is a css class for the control and let the browser do the rest.

Selecting a rows in the standard .net 2.0 GridView Using VB.net & JavaScript

Does anyone know how i can get the grid to select a row by clicking on any cell in the row?
The only way i can do this at the moment is by setting the AutoGenerateSelectButton property to True, but this adds a column to the grid with a crude "select" hyperlink and only selects the row if the word "Select" is cliked on.
Surely there has to be a better way!?!?
NOTE - I do not use C#
You need to add some javascript to the row in RowDataBound
e.Row.Attributes["onclick"] =
ClientScript.GetPostBackClientHyperlink
(this.GridView1, "Select$" + e.Row.RowIndex);
There's a CodeProject article about it here, which goes into much more detail.
This is ancient, but here is the VB.NET equivalent:
Dim cs As ClientScriptManager = Page.ClientScript
Dim postbacklink As String = cs.GetPostBackClientHyperlink(lbtnSelectRow, "")
And if do not want to write any code check out the client-side selection of the Telerik grid. I am pretty content with it when using it during my web development.

Difference between assigning a property and adding a attribute in a asp.net control

I am creating ASP.NET textbox controls dynamically. I want to know the difference between assigning the property of the control and adding it as an attribute.
For ex:
I can do:
TextBox txtBox = new TextBox();
txtBox.MaxLength = 100;
or I could do
txtBox.Attributes.Add("maxlength", "100);
The first example is strongly typed, so the compiler will check to make sure that a) MaxLength exists and b) an integer is set for that property.
The second example will work, but the compiler has no way to check to see if the attribute you are adding is correct.
In the end they get translated to the maxlength HTML attribute. TextBox offers a property mostly for convenience.
First one is better because of misspelling or wrong using.
Also first one is more readable.

Resources