There are many times which you might need to change the properties of an ASP.Net WebControl. There are many more properties available in ASP.Net than there are in pure HTML. Also, if you’ve ever programmed in VB (before DotNet), Though the concept is pretty much the same, The path to get to where you are going is quite different in some cases.
Remember, if you want to do any programmatic color changes, you must do a System.Drawing Import (<%@ Import Namespace=”System.Drawing” %>). In all cases, checking out the Class Browser in the Quickstart Tutorials will help you get a feel of the syntax needed for each control. Check out the syntax used in the example below, then, go over to the Class Browser for Web Controls – choose TextBox and you’ll see what I mean by drilling down through the properties.
As always, in order to try it out, merely copy the following code directly into a blank page, change the data connection code for your ownsituation, save it as an aspx file and run it from your web server. Naturally, we recommend using ASP Express for this.
<%@ Import Namespace=”System.Data” %>
<%@ Import Namespace=”System.Data.SQLClient” %>
<%@ Import namespace=”System.Drawing” %>
<!– Created: 06/18/2002 11:15:26 PM –>
<html>
<head>
<meta name=”GENERATOR” Content=”ASP Express 2.0″>
<title>TEST Page</TITLE>
<script language=”VB” runat=”server”>
Public strAltClr as string
Sub Page_Change(sender As Object, e As DataGridPageChangedEventArgs)
MyDataGrid.CurrentPageIndex = e.NewPageIndex
BindData
End Sub
Sub Page_Load(Source As Object, E As EventArgs)
If Not Page.IsPostBack Then
BindData()
End If
End Sub
Sub BindData()
Dim strConn as string = “server=(local);uid=sa;pwd=;database=pubs”
Dim MySQL as string = “Select au_fname, au_lname, phone, state from authors”
Dim MyConn as New SQLConnection(strConn)
Dim ds as DataSet=New DataSet()
Dim Cmd as New SQLDataAdapter(MySQL,MyConn)
Cmd.Fill(ds,”authors”)
MyDataGrid.Datasource=ds.Tables(“authors”).DefaultView
MyDataGrid.DataBind()
End Sub
Sub Button1_Click(sender As Object, e As System.EventArgs)
Dim intHeaderSize As Integer
Dim intNewSize As Integer
Dim intAltSize As Integer
Dim strGridLines As String
strAltClr=GClr.SelectedItem.Text
With MyDataGrid
intHeaderSize = HeaderSize.SelectedItem.Text
strGridLines = GLines.SelectedItem.Text
intAltSize = AltSize.SelectedItem.Text
intNewSize = GridSize.SelectedItem.Text
MyDataGrid.Font.Size = FontUnit.Point (intNewSize)
MyDataGrid.HeaderStyle.Font.Size = FontUnit.Point (intHeaderSize)
MyDataGrid.AlternatingItemStyle.Font.Size = FontUnit.Point (intAltSize)
if strAltClr=”Reset” then strAltClr=”LightGray”
.AlternatingItemStyle.BackColor = Color.FromName(strAltClr)
Select Case GLines.SelectedItem.Text
Case “None”
.GridLines = GridLines.None
Case “Both”
.GridLines = GridLines.Both
Case “Horizontal”
.GridLines = GridLines.Horizontal
Case “Vertical”
.GridLines = GridLines.Vertical
End Select
End With
End Sub
</script>
</head>
<body>
<div align=”center”><form runat=”server” method=”post”>
<asp:Datagrid runat=”server”
Width=”75%”
Id=”MyDataGrid”
cellpadding=”2″
cellspacing=”0″
Headerstyle-BackColor=”#8080C0″
Headerstyle-Font-Name=”Arial”
Headerstyle-Font-Bold=”True”
BackColor=”#8080FF”
Font-Name=”Arial”
Font-Bold=”True”
AlternatingItemStyle-BackColor=”LightGray”
AlternatingItemStyle-Font-Name=”Arial”
AlternatingItemStyle-Font-Bold=”True”
BorderColor=”Black”
AllowPaging = “True”
PageSize = “10″
PagerStyle-Mode = “NumericPages”
PagerStyle-PageButtonCount = “10″
PagerStyle-Font-Size=”12″
PagerStyle-HorizontalAlign=”Center”
PagerStyle-BackColor=”LightGoldenrodYellow”
OnPageIndexChanged = “Page_Change”>
</asp:DataGrid>
<table cellpadding=”2″ cellspacing=”2″ border=”0″ width=”75%”>
<tr>
<td valign=”top” Align=”Center”><b>Alternating BackColor:</b><br>
<asp:dropdownlist id=”Gclr” runat=”server”>
<asp:listitem>AntiqueWhite</asp:listitem>
<asp:listitem>Aqua</asp:listitem>
<asp:listitem>Azure</asp:listitem>
<asp:listitem>Bisque</asp:listitem>
<asp:listitem>Blue</asp:listitem>
<asp:listitem>Brown</asp:listitem>
<asp:listitem>Coral</asp:listitem>
<asp:listitem>CornSilk</asp:listitem>
<asp:listitem>Reset</asp:listitem>
</asp:dropdownlist></td>
<td valign=”top” Align=”Center”><b>Header FontSize:</b><br>
<asp:dropdownlist id=”HeaderSize” runat=”server”>
<asp:listitem>8</asp:listitem>
<asp:listitem>10</asp:listitem>
<asp:listitem>12</asp:listitem>
<asp:listitem>14</asp:listitem>
<asp:listitem>18</asp:listitem>
<asp:listitem>20</asp:listitem>
<asp:listitem>24</asp:listitem>
</asp:dropdownlist></td>
<td valign=”top” Align=”Center”><b>Grid FontSize:</b><br>
<asp:dropdownlist id=”GridSize” runat=”server”>
<asp:listitem>8</asp:listitem>
<asp:listitem>10</asp:listitem>
<asp:listitem>12</asp:listitem>
<asp:listitem>14</asp:listitem>
<asp:listitem>18</asp:listitem>
<asp:listitem>20</asp:listitem>
<asp:listitem>24</asp:listitem>
</asp:dropdownlist></td>
<td valign=”top” Align=”Center”><b>Alternating Item Font Size:</b><br>
<asp:dropdownlist id=”AltSize” runat=”server”>
<asp:listitem>8</asp:listitem>
<asp:listitem>10</asp:listitem>
<asp:listitem>12</asp:listitem>
<asp:listitem>14</asp:listitem>
<asp:listitem>18</asp:listitem>
<asp:listitem>20</asp:listitem>
<asp:listitem>24</asp:listitem>
</asp:dropdownlist><p>
</td>
<td valign=”top” Align=”Center”><b>Grid Lines:</b><br>
<asp:dropdownlist id=”GLines” runat=”server”>
<asp:listitem>None</asp:listitem>
<asp:listitem>Horizontal</asp:listitem>
<asp:listitem>Vertical</asp:listitem>
<asp:listitem>Both</asp:listitem>
</asp:dropdownlist></td>
</tr>
</table><br>
<br>
<asp:Button id=”button1″ Text=”Submit” onclick=”Button1_Click” runat=”server” />
</form>
</div>
</body>
</html>