|
|
32.1 How to cast the value entered in a textbox to integer?
|
 |
Int32.Parse(TextBox1.Text)
|
Int32.Parse(TextBox1.Text);
|
32.2 How to allow only numeric values in a textbox using ASP.NET Validator control?
|
 |
<asp:TextBox id="txtNumber" Runat="server" />
|
<asp:RegularExpressionValidator ID="vldNumber" ControlToValidate="txtNumber" Display="Dynamic" ErrorMessage="Not a number" ValidationExpression="(^([0-9]*|\d*\d{1}?\d*)$)" Runat="server">
|
</asp:RegularExpressionValidator>
|
32.3 Why does TextBox retain its values when posting in spite of having ViewState property disabled for TextBox,?
|
 |
32.4 I am running the query SQL="Select name from profile where proID=1"; and I am getting the result in Dataset dsdata. How can I read the text from the dataset and assign it to textbox1.text ?
|
 |
Textbox1.Text= dsData.Tables(0).Rows(0)("FieldName").ToString()
|
TextBox1.Text=dsData.Tables[0].Rows[0]["FieldName"].ToString();
|
32.5 Why do I get error message "Option Strict On disallows implicit conversions from 'System.Web.UI.Control' to 'System.Web.UI.WebControls.TextBox'."?
|
 |
For type Casting apply the following steps
VB.NET
|
aprtxt= CType(e.Item.FindControl("txtapr"), TextBox)
|
aprtxt = (TextBox) e.Item.FindControl["txtapr"];
|
32.6 How to align the Text property of the Textbox Control?
|
 |
TextBox1.Style("text-align")="right"
|
TextBox1.Style["text-align"]="right";
|
32.7 How to clear all the textboxes in my form?
|
 |
For Each ctl In Page.Controls(1).Controls
|
If TypeOf ctl Is TextBox Then
|
CType(ctl, TextBox).Text = ""
|
foreach (Control ctl in Page.Controls[1].Controls )
|
TextBox tb = ctl as TextBox;
|
Note: Page.Controls[1]=> control is within the |
32.8 How to convert TextBox value into a DateTime variable?
|
 |
Dim dt As DateTime = DateTime.Parse(TextBox1.Text)
|
DateTime dt = DateTime.Parse(TextBox1.Text);
|
By default Date.Parse check the date as MDY format
Refer DateTime.Parse for a specified Date Format. |
32.9 How to programmatically set the width of the textbox control?
|
 |
TextBox1.Width = Unit.Pixel(100)
|
TextBox1.Width = Unit.Pixel(100);
|
32.10 How to get the textbox value at the client side ?
|
 |
<script lang="javascript">
|
if (document.getElementById('<%=textbox2.ClientID%>').value == "")
|
alert("Please enter a value");
|
<asp:textbox id="textbox2" runat="Server"></asp:textbox>
|
<input type=button id="btn1" onclick="javascript:CheckFunction();" value="Click">
|
32.11 How to display data in Textboxes using DataSet?
|
 |
Product ID : <asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 80px; POSITION: absolute; TOP: 64px" runat="server"></asp:TextBox>
|
Product Name:<asp:TextBox id="TextBox2" style="Z-INDEX: 102; LEFT: 80px; POSITION: absolute; TOP: 112px" runat="server"></asp:TextBox>
|
'Display in TextBoxes using Column Name
|
TextBox1.Text = ds.Tables (0).Rows(0)("ProductId").ToString ();
|
TextBox2.Text =ds.Tables (0).Rows(0)("ProductName").ToString ();
|
'Display in TextBoxes using Column Index
|
TextBox1.Text = ds.Tables (0).Rows(0)(0).ToString ();
|
TextBox2.Text =ds.Tables (0).Rows(0)(1).ToString ();
|
//Display in TextBoxes using Column Name
|
TextBox1.Text = ds.Tables [0].Rows[0]["ProductId"].ToString ();
|
TextBox2.Text =ds.Tables [0].Rows[0]["ProductName"].ToString ();
|
//Display in TextBoxes using Column Index
|
TextBox1.Text = ds.Tables [0].Rows[0][0].ToString ();
|
TextBox2.Text =ds.Tables [0].Rows[0][1].ToString ();
|
32.12 How to display data in Textboxes using DataReader?
|
 |
Id : <asp:TextBox id="TextBox1" style="Z-INDEX: 102; LEFT: 128px; POSITION: absolute; TOP: 32px" runat="server"></asp:TextBox>
|
Name : <asp:TextBox id="TextBox2" style="Z-INDEX: 103; LEFT: 128px; POSITION: absolute; TOP: 72px" runat="server"></asp:TextBox>
|
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
|
' Put user code to initialize the page here
|
cn = New SqlConnection("server=localhost;uid=sa;pwd=;database=northwind")
|
cmd = New SqlCommand("select * from Products where productid =1", cn)
|
rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
|
TextBox1.Text = rdr("ProductId").ToString()
|
TextBox2.Text = rdr("ProductName").ToString()
|
Response.Write(ex.Message.ToString())
|
private void Page_Load(object sender, System.EventArgs e)
|
// Put user code to initialize the page here
|
cn = new SqlConnection("server=localhost;uid=sa;pwd=;database=northwind");
|
cmd = new SqlCommand( "select * from Products where productid=1 ", cn);
|
rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection );
|
TextBox1.Text =rdr["ProductId"].ToString ();
|
TextBox2.Text =rdr["ProductName"].ToString ();
|
Response.Write (ex.Message.ToString ());
|
32.13 Is there a TextArea in ASP.NET?
|
 |
You can use the TextBox webserver control and set the TextMode = MultiLine |
32.14 How to right align the text in the TextBox?
|
 |
<asp:TextBox id="TextBox1" dir=rtl runat="server"></asp:TextBox>
|
32.15 How to force the max no. of characters in a multiline TextBox using RegularExpressionValidator?
|
 |
<asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 0px; POSITION: absolute; TOP: 112px" runat="server" TextMode="MultiLine"></asp:TextBox>
|
<asp:RegularExpressionValidator id="RegularExpressionValidator1" style="Z-INDEX: 102; LEFT: 216px; POSITION: absolute; TOP: 112px"
|
runat="server" ErrorMessage="MaxLength is 20" ControlToValidate="TextBox1" ValidationExpression="^\w{1,20}$">
|
</asp:RegularExpressionValidator>
|
32.16 How to change server control backcolor from a variable?
|
 |
<asp:TextBox id="TextBox1" BackColor =<%#colCon.ConvertFromString(bgcolor)%> runat="server">
|
Protected colCon As New System.Drawing.ColorConverter
|
Protected bgcolor As String = "#556600"
|
protected System.Drawing.ColorConverter colCon =new System.Drawing.ColorConverter();
|
protected string bgcolor = "#556600";
|
32.17 How to create an array of Web Controls?
|
 |
<asp:PlaceHolder id="PlaceHolder1" runat="server"></asp:PlaceHolder>
|
Dim textboxes(5) As TextBox
|
textboxes(i) = New TextBox()
|
textboxes(i).ID = "TextBox" + i
|
textboxes(i).AutoPostBack = True
|
PlaceHolder1.Controls.Add(textboxes(i))
|
TextBox[] textboxes = new TextBox[5];
|
textboxes[i] = new TextBox();
|
textboxes[i].ID = "TextBox" + i;
|
textboxes[i].AutoPostBack = true;
|
PlaceHolder1.Controls.Add(textboxes[i]);
|
32.18 How to show TextBox web server control with TextMode Property Password as **** rather than blank?
|
 |
For security reason, TextBox with TextMode="Password" setting cannot be assigned or populated via the Text property on Page_Load or PostBack event.
It's not recommend for to prefill a "Password" type TextBox with the("***") characters, though below is the solution to achieve this.
VB.NET
|
ViewState("Pwd")=TextBox1.Text ;
|
TextBox1.Attributes.Add("value", ViewState("Pwd").ToString ()) ;
|
ViewState["Pwd"]=TextBox1.Text ;
|
TextBox1.Attributes.Add("value", ViewState["Pwd"].ToString ()) ;
|
|
|
|
|