Change the Title of a Page

The Title Really says it all – you get to programmatically change the title of the page you’re on.

— In the Title Header -
<asp:literal ID=”literal1″ runat=”server” />
Then, create this script/sub:

<script language=”VB” runat=”server”>
Sub Button_click(Source as Object, E as EventArgs)
literal1.text=text1.text
End Sub
</script>

In the body of the document:
<Form id=”form1″ runat=”server”>
<asp:TextBox id=”text1″ runat=”server” />
<asp:Button id=”button1″ Text=”Change Page Title” onclick=”Button_click” runat=”server” />

</Form>
Then, change the text box and Click the Button

Exposing Properties in User Controls #2

OK – let’s say you want to add multiple dropdownlists to a page – each connecting to the same database and table, but showing different fieldnames. Yes, you could do all the connection and binding multiple times within the same ASPX page, but what if you needed to reuse this same scenario, maybe even using different fieldnames in other pages. The answer is simple – create a user control and give it a property that reflects a field name in the table.

This way, the control is totally reusable and the field names can be added or changed dynamically.

Here, the User Control is used twice, so the connection and binding is done twice, however, the code is kept to the minimum thanks to the inherant re-useablity of User Controls. All that is needed here is to assign different fields to the Public Property (fldName), along with unique ID changes. That’s all there is to it!

Here’s the ASCX page (user control):
<%@ Import Namespace=”System.Data” %>
<%@ Import Namespace=”System.Data.SqlClient” %>
<script language=”VB” runat=”server”>
Public fldName as string
Sub Page_Load(Source as Object, E as EventArgs)
Dim strConn as string =”server=localhost;database=pubs;uid=sa;pwd=password”
Dim Conn as new SQLconnection(strConn)

Dim strSQL as string =”select distinct ” & fldName & ” from authors”
Dim Cmd as New SQLCommand(strSQL,Conn)
Conn.Open()
state.DataSource = Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
state.datatextfield=fldName
state.DataBind()
End Sub
</script>

<asp:DropDownList id=”state” runat=”server” />

Here’s the ASPX code (in this sample — authors.aspx):
<%@ Register TagPrefix=”xprs” tagname=”state” src=”authors.ascx” %>
<html>
<head>
<meta name=”GENERATOR” Content=”ASP Express 2.0″>
<title>Untitled</title>
</head>
<body>
<Form id=”form1″ runat=”server”>
<xprs:state id=”NewState” fldName=”city” runat=”server” /> — <xprs:state id=”NewCity” fldName=”state” runat=”server” />
</Form>
</body>
</html>

Exposing Properties in User Controls

Sometimes you might need to expose a property in a User control (.ascx file). In this way, user controls can be more than static documents – they can have flexibility. Save the following (two files) and see how to access an assigned property through your main ASPX document.

Granted, this is no more than a text property and the implementation here could be handled easily in a label. However, this simple implementation shows a little of the power that can be had when using user controls. You’ll notice there’s a default string for the Whatever property in the user control, along with the Whatever property being explicitly used in the ASPX file. If you removed the explicity Whatever property in the ASPX file, the default string assigned to the Whatever property would be used.

ASCX File (save for this example, as usercontrol.ascx):
<script language=”VB” runat=”server”>
Public Whatever as String=”This is the Default String”
</script>
<hr>
<% =Whatever %>
<hr>


ASPX File:
<%@ Register TagPrefix="xprs" tagname="String" src="usercontrol.ascx" %>
<html>
<head>
<meta name="GENERATOR" Content="ASP Express 2.0">
<title>Untitled</title>

<script language="VB" runat="server">
Sub Button1_click(Source as Object, E as EventArgs)
xprsString.Whatever=text1.text
End Sub
</script>

</head>
<body>
<Form id="MyForm" runat="server">
<asp:TextBox id="text1" runat="server" /><br>
<asp:Button id="button1" Text="Enter Text Here" onclick="Button1_click" runat="server" />
</Form>
<div align="center"><b><xprs:String id="xprsString" runat="server" /></b> </div>

</body>
</html>

Paging AND Sorting with a DataGrid

Just copy and paste this code into a new blank page. Change the Database name, UID and password. It uses the Northwind database with SQL Server and save it as a ‘.aspx’ page.

To make it “Next-Previous” instead of numbering – change the PagerStyle-Mode to:


PagerStyle-Mode=’NextPrev’
PagerStyle-NextPageText=’Next ->’
PagerStyle-PrevPageText=”<- Previous”

<%@ Import Namespace=”System.Data” %>
<%@ Import Namespace=”System.Data.SQLClient” %>

<script runat=”server” language=”VB”>
Protected SQLStmt As String = “SELECT CompanyName, ContactName, ContactTitle, Phone, Fax FROM Customers”

Sub Page_Load(Source As Object, E As EventArgs)
If Not Page.IsPostBack Then
SQLStatement.Text = SQLStmt
BindData()
End If
End Sub

Sub BindData()
Dim myDataSet As New DataSet
Dim myDataSetCommand As SQLDataAdapter
Dim ConString As String

ConString =”server=localhost;database=Northwind;uid=UserName;pwd=Userpassword;”
myDataSetCommand = New SQLDataAdapter(SQLStatement.Text, ConString)
myDataSetCommand.fill(myDataSet, “Customers”)

myDataGrid.DataSource = myDataSet.Tables(“Customers”).DefaultView
myDataGrid.DataBind()
End Sub

Sub PageIndexChanged_OnClick(Source As Object, E As DataGridPageChangedEventArgs)
MyDataGrid.CurrentPageIndex = e.NewPageIndex
BindData()
End Sub

Sub SortCommand_OnClick(Source As Object, E As DataGridSortCommandEventArgs)
SQLStatement.Text = SQLStmt & ” ORDER BY ” & E.SortExpression
BindData()
End Sub
</script>
<style>
.DataGrid {font:x-small Verdana, Arial, sans-serif}
</style>

<title>ASPExpress.com – Paging AND Sorting in the DataGrid</title>
</head>
<body>
<div align=”center”>Paging And Sorting Together with ASP.Net </div>
<form runat=”server” method=”post”>

<asp:DataGrid runat=”server” id=”myDataGrid”
borderwidth=”5″
bordercolor=”black”
Borderstyle=”double”
PagerStyle-VerticalAlign=”top”
Cellpadding=”4″
Cellspacing=”0″
ShowHeader=”True”
CssClass=”DataGrid”
HeaderStyle-ForeColor=”Black”
HeaderStyle-Font-Bold=”True”
AllowSorting=”True”
OnSortCommand=”SortCommand_OnClick”
AllowPaging=”True”
OnPageIndexChanged=”PageIndexChanged_OnClick”
PageSize=”10″
width=”100%”
HeaderStyle-BackColor=”#aaaadd”
AlternatingItemStyle-BackColor=”#eeeeee”
PagerStyle-Backcolor=”#aaaadd”
PagerStyle-Forecolor=”Black”
PagerStyle-HorizontalAlign=”center”
PagerStyle-Mode=”NumericPages”
PagerStyle-BorderStyle=”Inset”

/>
</form>
<asp:Label id=”SQLStatement” runat=”server” Visible=”false” />
</body>
</html>

Paging with a DataGrid

Just copy and paste this code into a new blank page. Change the Database name, UID and password. It uses the Northwind database with SQL Server and save it as a ‘.aspx’ page.

To make it “Next-Previous” instead of numbering – change the PagerStyle-Mode to:
PagerStyle-Mode=’NextPrev’
PagerStyle-NextPageText=’Next ->’
PagerStyle-PrevPageText=’<- Previous'
Just copy and paste this code into a new blank page. Change the Database name, UID and password. It uses the Northwind database with SQL Server and save it as a ‘.aspx’ page.

To make it “”Next-Previous”" instead of numbering – change the PagerStyle-Mode to:

PagerStyle-Mode=’NextPrev’
PagerStyle-NextPageText=’Next ->’
PagerStyle-PrevPageText=’<- Previous’

<%@ Import Namespace=”System.Data” %>
<%@ Import Namespace=”System.Data.SqlClient” %>
<script language=”VB” runat=”server”>

Sub Page_Load(sender As Object, e As EventArgs)
BindGrid
End Sub

Sub Page_Change(sender As Object, e As DataGridPageChangedEventArgs)
dim start as Integer
start = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize
MyDataGrid.CurrentPageIndex = e.NewPageIndex
BindGrid
End Sub

Sub BindGrid()
‘You can store the connection as a string in Web.Config, if you want:
‘Dim myConnection As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings(“Whatever”))

Dim myConnection As SqlConnection = New SqlConnection(“server=YourServer;uid=UserName;pwd=Password;database=northwind”)

dim ds as DataSet = new DataSet()
dim adapter as SqlDataAdapter = new SqlDataAdapter(“Select * from customers”, myConnection)
adapter.Fill(ds,”customers”)

MyDataGrid.DataSource=ds.Tables(“customers”).DefaultView
MyDataGrid.DataBind()
End Sub

Sub PagerButtonClick(sender As Object, e As EventArgs)
BindGrid
End Sub

</script>
<html>
<head>
</head>
<body>
<div align=”center”>Paging with ASP.Net</div>

<form runat=”server”>

<ASP:DataGrid id=”MyDataGrid” runat=”server”
AllowPaging=”True”
PageSize=”5″
PageCount=”1″
PagerStyle-Mode=”NumericPages”
PagerStyle-HorizontalAlign=”Center”
OnPageIndexChanged=”Page_Change”
BorderColor=”black”
BorderWidth=”1″
GridLines=”Both”
CellPadding=”3″
CellSpacing=”0″
Font-Name=”Verdana”
Font-Size=”8pt”
HeaderStyle-BackColor=”#aaaadd”
AlternatingItemStyle-BackColor=”#eeeeee”
width=”100%”
/>
<p>
</form>

</body>
</html>

Sorting with a DataGrid

Just copy and paste this code into a new blank page. Change the Database name, UID and password. It uses the Northwind database with SQL Server and save it as a ‘.aspx’ page.

<%@ Import Namespace=”System.Data” %>
<%@ Import Namespace=”System.Data.SQLClient” %>
<html>
<head>
<title>ASPExpress.com – Column Sorting in the DataGrid</title>
<script runat=”server” language=”VB”>
Protected SQLStmt As String = “SELECT CompanyName, ContactName, ContactTitle, Phone, Fax FROM Customers”

Sub Page_Load(Source As Object, E As EventArgs)
If Not Page.IsPostBack Then
BindData()
End If
End Sub

Sub BindData()
Dim myDataSet As New DataSet
Dim myDataSetCommand As SQLDataAdapter
Dim ConString As String
ConString = “server=YourServerName;database=Northwind;uid=internet;pwd=;”
myDataSetCommand = New SQLDataAdapter(SQLStmt, ConString)
myDataSetCommand.Fill(myDataSet, “Customers”)
myDataGrid.DataSource = myDataSet.Tables(“Customers”).DefaultView
myDataGrid.DataBind()
End Sub
</script>

<script language=”VB” runat=”server”>
Sub SortCommand_OnClick(Source As Object, E As DataGridSortCommandEventArgs)
SQLStmt = SQLStmt & ” ORDER BY ” & E.SortExpression
BindData()
End Sub
</script>
</HEAD>
<BODY>

<form runat=”server” method=”post”>
<asp:Datagrid runat=”server”
Id=”MyDataGrid”
OnSortCommand = “SortCommand_OnClick”
AllowSorting = “True”
BorderColor=”black”
BorderWidth=”1″
GridLines=”Both”
CellPadding=”3″
CellSpacing=”0″
Headerstyle-BackColor=”#aaaadd”
Headerstyle-Forecolor=”#FFFFFF”
Headerstyle-Font-Name=”Arial”
Headerstyle-Font-Bold=”True”
Headerstyle-Font-Size=”11″
Font-Name=”Arial”
Font-Size=”10″
AlternatingItemStyle-BackColor=”#eeeeee”
AlternatingItemStyle-Font-Name=”Arial”
AlternatingItemStyle-Font-Size=”10″
/>
</form>

</BODY>
</HTML>

DropDownList Selection

Just copy and paste this code into a new blank page. This is example uses MS Access. Just change the location of the database on your computer to use this code. If you want to use the pubs database with SQL Server, change the whole Provider stringconnection, command and the imported namespace.

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>

<html>
<head>
<meta name="GENERATOR" Content="ASP Express 2.0">
<title>Dropdown List</TITLE>
<Link REL=STYLESHEET HREF="/basicArial.css" TYPE="text/css">

<script language="VB" runat="server">

Sub Page_Load(Source as Object, E as EventArgs)

If not IsPostBack then
BindData()
end if
End Sub
Sub button1_click(Source as Object, E as EventArgs)
label1.text=" You selected " & MyList.selecteditem.text
End Sub

Sub Reset_Click(Source as Object, E as EventArgs)
label1.text=""
BindData()
End Sub

Sub BindData()
Dim strSQL as string
Dim strConn as string
strSQL="Select distinct city from publishers where city <>”"
strConn ="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & server.mappath("/biblio.mdb") & ";"
Dim Conn as New OLEDBConnection(strConn)
Dim Cmd as New OLEDBCommand(strSQL,Conn)
Conn.Open()
MyList.DataSource = Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
MyList.DataBind()
Conn.Close()
End Sub
</script>
</head>

<div align="center"><form runat="server">
<asp:dropdownlist id="MyList" datatextfield="city" runat="server"/>
<asp:Button id="button1" Text="Submit" onclick="button1_click" runat="server" />
<asp:Button id="Reset" Text="Reset" onclick="Reset_Click" runat="server" /> <p>
</form>
<b><font Color="#800000"><asp:Label ID="label1" runat="server" /></font></b> </div>

</body>
</html>