<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">
DataSet myDataSet = new DataSet();
void BuildDataSetTable(string commandText, string tableName)
{
string ConnectionString = Convert.ToString(ConfigurationSettings.AppSettings["MSDEConnectString"]);
SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlCommand myCommand = new SqlCommand(commandText, myConnection);
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
try {
myConnection.Open();
myAdapter.Fill(myDataSet, tableName);
} catch (Exception ex) {
throw(ex);
} finally {
myConnection.Close();
}
}
void Page_Load(object sender, EventArgs e) {
if (!(Page.IsPostBack))
{
string SelectPublisher = "SELECT PublisherID, PublisherName From Publisher";
BuildDataSetTable(SelectPublisher, "Publisher");
Label1.Text = "Select a Publisher";
DropDownList1.DataBind();
RadioButtonList1.DataBind();
ListBox1.DataBind();
}
}
void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e) {
string SelectBook = "SELECT * FROM Book WHERE BookPublisherID=" + RadioButtonList1.SelectedItem.Value;
BuildDataSetTable(SelectBook, "Book");
DataGrid1.DataBind();
}
void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) {
string SelectBook = "SELECT * FROM Book WHERE BookPublisherID=" + DropDownList1.SelectedItem.Value;
BuildDataSetTable(SelectBook, "Book");
DataGrid1.DataBind();
}
void ListBox1_SelectedIndexChanged(object sender, EventArgs e) {
string SelectBook = "SELECT * FROM Book WHERE BookPublisherID=" + ListBox1.SelectedItem.Value;
BuildDataSetTable(SelectBook, "Book");
DataGrid1.DataBind();
}
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:RadioButtonList id="RadioButtonList1" runat="server" DataSource='<%# myDataSet.Tables["Publisher"] %>' DataTextField="PublisherName" DataValueField="PublisherID" AutoPostBack="True" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged"></asp:RadioButtonList>
<h2>A DropDownList
</h2>
<asp:DropDownList id="DropDownList1" runat="server" DataSource='<%# myDataSet.Tables["Publisher"] %>' DataTextField="PublisherName" DataValueField="PublisherID" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>
<h2>A ListBox
</h2>
<asp:ListBox id="ListBox1" runat="server" DataSource='<%# myDataSet.Tables["Publisher"] %>' DataTextField="PublisherName" DataValueField="PublisherID" AutoPostBack="True" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"></asp:ListBox>
<asp:Label id="Label1" runat="server"></asp:Label>
<asp:DataGrid id="DataGrid1" runat="server" DataSource='<%# myDataSet.Tables["Book"] %>' BorderStyle="None" BorderWidth="1px" BorderColor="#CC9966" BackColor="White" CellPadding="4">
<FooterStyle forecolor="#330099" backcolor="#FFFFCC"></FooterStyle>
<HeaderStyle font-bold="True" forecolor="#FFFFCC" backcolor="#990000"></HeaderStyle>
<PagerStyle horizontalalign="Center" forecolor="#330099" backcolor="#FFFFCC"></PagerStyle>
<SelectedItemStyle font-bold="True" forecolor="#663399" backcolor="#FFCC66"></SelectedItemStyle>
<ItemStyle forecolor="#330099" backcolor="White"></ItemStyle>
</asp:DataGrid>
</form>
</body>
</html>
File: Web.config
<configuration>
<appSettings>
<add key="MSDEConnectString" value="server=(local)\YourDatabase;database=Books;uid=YourID;pwd=letmein;" />
</appSettings>
</configuration>
|