Test This Script: Label Web Server Control
Test This Script: Label Web Server Control
The Label web server control is one of the simplest control. The output of label web
server control is the HTML <span> tag. In this discussion we will see the following
aspects.
In the following example the usage of label web server control is explained. The property "Text" is
used to assign a value (label) to the label control.
<html>
<head>
<title>Label Web Server Control</title>
</head>
</body>
</html>
In our next example, we can observe that, we assign the text dynamically to the label control. In the
page load event, the value "This is my FIRST ASP.NET Page!" is assigned to the property text.
<html>
<head>
<title>Label Web Server Control</title>
</head>
<body style="font: 10pt verdana">
<h3 align=center>ASP.NET Label Server Control</h3>
<form ID="Form2" runat=server>
<asp:label id="label1" runat="server" />
</form>
</body>
</html>
In our final example, we will see how can we make use of other properties of the label control. The
properties which is shown in the following example include tooltip, borderwidth, bordercolor,
backcolor and forecolor. The property, borderwidth expects a unit. That is why we have declared a
variable called "bdw", which is of type unit. And to assign colors for border, background and
foreground, we use the Color object. By default, the color object is not available, since we import the
namespace, "system.drawing".
<html>
<head>
<title>Label Web Server Control</title>
</head>
<body style="font: 10pt verdana">
<h3 align=center>ASP.NET Label Server Control</h3>
</body>
</html>
In the following example the usage of label web server control is explained. The property "Text" is
used to assign a value (label) to the label control. We also have a button control which is used to
submit the value back to the page.
<html>
<head>
<title>Textbox Web Server Control - by Das
</head>
<body style="font: 10pt verdana">
<h3 align=center>ASP.NET Textbox Server Control</h3>
</body>
</html>
If we have web application, then we will definitely have a Login module. For any
login password is a must. In ASP .NET we have to make use of the property,
textmode=password. We can access the password value by saying, txtPwd.Text,
were txtPwd is a textbox of type password. It should be also noted that, to
concatinate a string, we can use the operator += just as in C/C++.
<html>
<head>
<title>Textbox Web Server Control - by Das</title>
<script language="VB" runat="server">
Sub MySub(sender As Object, e As EventArgs)
lblMsg.text = "<br>Is Your First Name, <b>" & txtFname.text & " </b>?"
lblMsg.text += "hmmm, you typed the password, <b>" & txtPwd.text & "
</b>right ?"
End Sub
</script>
</head>
<body style="font: 10pt verdana">
<h3 align=center>ASP.NET Textbox Server Control</h3>
</body>
</html>
In Classic ASP, we have the tag <textarea> to accept values in text area. The
same can be achieved in ASP.NET using the textbox web control. We have set the
property, "textmode", to "multiline", such as textmode=MultiLine. You can also specify
the rows and columns that you may need.
<html>
<head>
<title>Textbox Web Server Control - by Das</title>
</head>
<body style="font: 10pt verdana">
<h3 align=center>ASP.NET Textbox Server Control</h3>
Another aspect that you may need to note in these examples is, when we click the
submit button, all the values are retained in the form without any additional coding.
This is the most important feature, Maintaining State, of ASP.NET
Listbox web server control is equivalent to the <Select> tag in HTML. This control
will be very useful to get inputs from user, were all values are pre-defined. For eg:
inorder to accept the "State in which the user is living", we can have a list of all
states available. In this session, we will learn how to populate a listbox control.
Populating a listbox control is very simple. It is the same way as we do in Classic ASP. In classic
ASP, we use the <option> tag to add an item to the listbox. In ASP .NET, we use the tag
<asp:ListItem> to populate the Listbox.
<HTML>
<HEAD>
<title>Listbox Web Server Control - by Das</title>
</HEAD>
<body style="FONT: 10pt verdana">
<h3align=center>ASP.NET Listbox Server Control</h3>
</body>
</HTML>
We can add items to a Listbox control during run time. This is achieved by populating an arrayList.
Once we have an arrayList, we can assign this arrayList to the Listbox control. Actually, we have
to bind the arrayList to the listbox control. Binding is done using the method DataBind. Before
any binding to take place, we should assign a datasource, in our case, it will be the arrayList. The
following example does this.
<HTML>
<HEAD>
<title>Listbox Web Server Control - by Das</title>
</HEAD>
<body style="FONT: 10pt verdana">
<h3align=center>ASP.NET Listbox Server Control</h3>
</body>
</HTML>
To allow users to select multiple values from a listbox, you need to set two
properties, such as rows and SelectionMode. The value for rows can be any
integer and for Selectionmode, it will be Multiple. To retrieve the all the values
selected by the user, we need to make use of the properties such as
Items.Count. And the property, selected will tell us whether the item have been selected
or not. See the following example.
<HTML>
<HEAD>
<title>Listbox Web Server Control - by Das</title>
<html>
<head>
</head>
<body style="font: 10pt verdana">
<h3 align=center>ASP.NET Listbox Server Control</h3>
<form runat=server>
<asp:ListBox id="lstStates" Rows="5" SelectionMode="Multiple"
runat="server">
<asp:ListItem>Ohio</asp:ListItem>
<asp:ListItem>Michigan</asp:ListItem>
<asp:ListItem>Wisconsin</asp:ListItem>
<asp:ListItem>Texas</asp:ListItem>
<asp:ListItem>Dallas</asp:ListItem>
<asp:ListItem>Indiana</asp:ListItem>
</asp:ListBox><br>
<asp:button Text="Submit" Tooltip="To select multiple items hold the CTRL KEY
and then select the items" OnClick="mySub" runat="server" /><br>
<asp:Label id="lblMsg" runat="server"/>
</form>
</body>
</html>
If we allow a user to add item to a listbox, then we also may need to allow them to
delete items from the listbox. The method remove is used to remove a selected
item from the list box. Also, to insert a value to a particular position, say 2nd
position, we need to use the method called insert. In the coming example, we will see
how to remove an item, insert an item into a particular position and also another method to
popopulate a Listbox.
<HTML>
<HEAD>
<title>Listbox Web Server Control - by Das</title>
<html>
<head>
</head>
<body style="font: 10pt verdana">
<h3 align=center>ASP.NET Listbox Server Control</h3>
<form runat=server>
<asp:ListBox id="lstStates" Rows="5" SelectionMode="Multiple"
runat="server">
</asp:ListBox>
</body>
</html>
Listbox control will be useful to get inputs, which has a pre-determined number of
values. Also, you should note that, in the method, PageLoad and mySub we have
IF statements. Have you noticed that, we don't have the keyword "Then" for IF
statements. "Then" is not mandatory, as it was in classic ASP.
DropDownLists are very useful controls which can be used to accept input
from users. This control can be better used, if we know the range for the
input. User feels more comfortable with DropDownLists (combo boxes) rather
than textbox, since keyboard should be used to provide input to the latter. In
this article, we will see how can we use a dropdownlist inside a datagrid. We
will take the table, "stores" which is available under the database pubs
(SqlServer 2000).
One of the field in the table, "stores" is state. We will create another lookup
table to store the values of states available in United States. We are going to
create an editable datagrid. In the view mode, we will have just Labels. When
we switch to the edit mode, we will allow the user to modify the state, by
means of dropdownlist.
For our example, we will take the table STORE in the PUBS Database. Since
stored procedures are very much better than inline query, we use a stored
procedure called, sp_stores_sel, which contains a single SQL statement. The
SQL statement would be Select * from stores.
The important aspect that is to be noted is that, we cannot explicitly bind the
dropdownlist from the page_load event or any other method. The
dropdownlist control in a <EditTemplateColumn> is only accessible to the
Update Method (which is specified in the OnUpdateCommand event).
<EditItemTemplate>
<asp:Label ID="lblTempState"
Text='<%# DataBinder.Eval(Container.DataItem, "state") %>'
Runat=server />
<asp:DropDownList id="cboState"
DataSource="<%# BindState() %>"
DataTextField="Statename"
DataTextValue="Statename"
runat="server" />
</EditItemTemplate>
How it works?
myCommand.CommandType = CommandType.StoredProcedure
myConnection.Open()
Return myCommand.ExecuteReader(CommandBehavior.CloseConnection)
End Function
How it works?
In this last section, we will see how to get the selected/updated item from the dropdownlist. We
know that, we show the dropdownlist only during the edit mode. When the user clicks the update
button, we have to get the selected item in the combo box. The following two lines of code does
this. First let us take a look at the code.
How it works?
First, we use the Findcontrol method to get the combo box object. After the
first statement, we have a dropdownlist object named, cboStateTemp. Note
the type casting done using the Ctype method. Once we have a dropdownlist
object, we can work with all properties and methods that are available in in
this object. The property to get the selected value is SelectedItem. We need
the value, so we have to say, SelectedItem.Value. If you need the text, then
you have to say, SelectedItem.Text.
This is the most tricky part of this article. The event that will be helpful for us
to do this operation in the ItemDataBound event. So, in the datagrid, you
should invoke a method for this event. Then, in the event, all we need is to
find the control "cbostate" and set the SelectedIndex property. Apart from
this, we need to know which item we need to preset. Well, we can get this
value from the edit event. If you do a FinControl on the Edit Event for the
column that we need the value (in our case, the state column), we will get the
value that needs to be preset. Plese see the following piece of code in the
ItemDataBound event.
ItemDataBound Event
How it works?
In the above event, we just have three lines of code. The first two statements
inside the IF statement is pretty straight forward. The third one is the
important line, which presets the item in the combo box. The string variable,
strCurrentState is populated in the Edit event and is a global variable. You
can download the complete code later in this article.
Conclusion
I have used stored procedures in all places. For your convenience, I have
also included source code for these stored procedures. If you have any
questions regarding this article or any of my article please feel free to email
me at [email protected]
Links
Checkbox - How to assign a text to Checkbox control and how to know the checkboxes that
are selected?
Creating a checkbox is very easy indeed. The following line creates a Checkbox
control. <asp:Checkbox Runat=Server />. To assign a text to this checkbox
control, we can either do it in the declaration itself, or we can generate the text from
server side itself. To assign a text while creating, all you have to do is to use the
property text. The following example depicts how to create checkbox web server control and
how to assign values from the server side.
<HTML>
<HEAD>
<title>Checkbox Web Server Control - by Das </title>
If chkMusic.Checked Then
strResult += chkMusic.Text & ", "
End If
If chkDriving.Checked Then
strResult += chkDriving.Text & ", "
End If
If chkFishing.Checked Then
strResult += chkFishing.Text & ", "
End If
If chkBoating.Checked Then
strResult += chkBoating.Text & ", "
End If
If chkEating.Checked Then
strResult += chkEating.Text
End If
</script>
</head>
<body>
</form>
</body>
</HTML>
The above example is very simple. There are two things to learn from the above
example. First, to assign a text to the textbox web server control, you have to use
the property text. And to know whether the checkbox has selected or not, you have
to use the property checked.
By default, checkbox control does not support the property value. In classic ASP,
we used this property to assign any value to the checkbox control. So, in ASP
.NET, how to assign a value to the checkbox control. We have to make use of the
Attributes method. We have to add an attribute to every checkbox web control. The
following line, adds an attribute to the checkbox, Music.
chkMusic.Attributes.Add("Value", 1). So, the method add takes two parameters.
The first parameter is the attribute name. And the second parameter is the Value
for this attribute. In the above example, we have created an attribute called Value.
The following example exaplains how to assign attributes and how to retrieve them.
<HTML>
<HEAD>
<title>Checkbox Web Server Control - by Das </title>
chkMusic.Attributes.Add("Value", 1)
chkDriving.Attributes.Add("Value", 2)
chkFishing.Attributes.Add("Value", 3)
chkBoating.Attributes.Add("Value", 4)
chkEating.Attributes.Add("Value", 5)
End Sub
If chkMusic.Checked Then
strResult += chkMusic.Attributes("Value") & ", "
End If
If chkDriving.Checked Then
strResult += chkDriving.Attributes("Value") & ", "
End If
If chkFishing.Checked Then
strResult += chkFishing.Attributes("Value") & ", "
End If
If chkBoating.Checked Then
strResult += chkBoating.Attributes("Value") & ", "
End If
If chkEating.Checked Then
strResult += chkEating.Attributes("Value")
End If
lblResult.Text = strResult
End Sub
</script>
</head>
<body>
</form>
</body>
</HTML>
Conclusion
Check box controls are very useful control for getting inputs for multiple choice
questions.
CheckboxList web server control is the same as Checkbox control. Before reading
this article, please read my article on CheckBox Web Server Control. In this article we will
see how to use the CheckboxList Web server control
CheckBoxList is almost equivalent to the Listbox web server control. The following example
explains how to declare a CheckboxList web server control and how to assign and retrieve values
depending upon the values selected by the user.
<HTML>
<HEAD>
<title>CheckboxList Web Server Control - by Das </title>
End Sub
</script>
</head>
<body style="font: 10pt verdana">
</form>
</body>
</HTML>
Conclusion
CheckBoxList controls are very useful control for getting inputs for multiple choice
questions.
RadioButton Web Server Control is perfect for questions who has only one answer.
The RadioButton web server control is almost the same as the CheckBox web
server control. The only thing that is different here is that, you have to use a
property called GroupName to the collection of your radio buttons. Other than that
it is the same as the Checkbox control.
RadioButton - How to assign a text how to know radio button have been selected?
To assign a text, we need to use the text property and to know whether the radio button have
been selected, we need to use the selected property, which returns either true or false.
<HTML>
<HEAD>
<title>RadioButton Web Server Control - by Das </title>
</script>
</head>
<body>
</form>
</body>
</HTML>
You can also add attributes and retrieve attributes for this control. To know more
about adding and retrieving attributes, please read my article on Checkbox web
server control.
I saw them code to persist data across pages (steps of the wizard) and in the process
loaded the Session object.
I was assigned a fairly simple wizard with about 4 steps. The methodology, I followed
was this.
The user gets a feel that each step is a different page, but as a developer, I had an
easy time! Across the steps, I did just nothing to persist data. ASP.NET ViewState did it
for me!
And at the last click, I had all the data in page to be saved! No hassles!
DownLoad
How to Create a text file in ASP .NET? Written on: May, 13th 2002.
Introduction
One of the frequent task in any web application is dealing with the text files. In classic
ASP, we used the FileSystemObject to deal with files. In this article, we will see how to
create text files in ASP .NET.
How to capture errors which may occur while creating a text file?
We require the namespace, System.IO to work with files. So, we should import this
namespace in our ASPX page such as
To start with, we need to create an instance of the object, StreamWriter. The instance
will be the file pointer for us. Once we have a File Pointer, we need to invoke the
method, CreateText method of the object, File. The method, CreateText takes a
string as an argument. The string is nothing but the path of file that is going to get
created. Now, let us see an example. Let us assume, we have a textbox with textmode
set to MultiLine and a button. On the click event of the button, we need to create a text
file. The code within the Click event is shown below. </FONT< p>
Dim fp As StreamWriter
Try
fp = File.CreateText(Server.MapPath(".\Upload\") & "test.txt")
fp.WriteLine(txtMyFile.Text)
lblStatus.Text = "File Succesfully created!"
fp.Close()
Catch err As Exception
lblStatus.Text = "File Creation failed. Reason is as follows
End Try
End Sub
How it works?
Conclusion
Creating a text file is very easy. We can easily create a text file in just 3 lines of code.
To read content from a text file, visit my article Read data from Text File
Links
Some times you may notice that, you are getting the following values in your
dropdownlists.
<option
value="System.Data.DataRowView">System.Data.DataRowView</option>
When binding to the DropDownList (and some of the other controls), you need to
specify the DataTextField and DataValueField (if you don't specify the DataValueField
then the control will automagically use what you specified for the DataTextField).
DropDownList1.DataTextField = "FieldNameThatIsToBeDisplayedForEachItem"
DropDownList1.DataValueField = "FieldNameThatRepresentsTheValueOfTheItem
Introduction
<asp:DropDownList id="cboState"
OnSelectedIndexChanged="PopulateNextCombo"
AutoPostBack="True"
runat="server" />
How it works?
The above declaration pertains to the master DropDownList. Please note that, the
AutoPostBack is set to true. This is the key aspect. When a user changes the current
value in the master DropDownList, we should populate the child DropDownList. That is
why, we have set the OnSelectedIndexChanged property.
PopulateNextCombo
Public Sub PopulateNextCombo(ByVal sender As Object, ByVal e As System.EventArgs)
Dim tmpcboState2 As DropDownList
Dim tmpDropDown1 as DropDownList
Dim myConnection As SqlConnection = New
SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
Dim myCommand As SqlCommand = New SqlCommand("sp_das_state_sel",
myConnection)
Dim myDataReader As SqlDataReader
myCommand.CommandType = CommandType.StoredProcedure
Try
myConnection.Open()
myDataReader =
myCommand.ExecuteReader(CommandBehavior.CloseConnection)
tmpcboState2 =
CType(DG_Combo.Items.Item(DG_Combo.EditItemIndex).FindControl("cboState2"),
DropDownList)
tmpcboState2.DataSource = myDataReader
tmpcboState2.DataBind()
tmpDropDown1 =
CType(DG_Combo.Items.Item(DG_Combo.EditItemIndex).FindControl("cboState"),
DropDownList)
tmpcboState2.SelectedIndex = tmpDropDown1.SelectedIndex
Finally
If Not myDataReader Is Nothing Then
myDataReader.Close()
End If
How it works?
The above method, populates the second (child) DropDownList. cboState2 is the ID of
our second DropDownList. The third statement inside the Try block gets the reference for
the second DropDownList. Fourth and fifth statements in the Try block populates the
second DropDownList. In this example, we are setting the SelectedItemIndex for the
second DropDownList the same as the master DropDownList.
Conclusion
I have used stored procedures in all places. For your convenience, I have also included
source code for these stored procedures. If you have any questions regarding this article
or any of my article please feel free to email me at [email protected]
Links