📄 nested-binding.aspx
字号:
<%@ Page Language="C#" EnableViewState="True" EnableSessionState="True" SmartNavigation="False"%>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient"%>
<script runat="server">
// -------------------------------------------------------------------
// page-level variable to hold DataSet of values from database
DataSet oCarsDS;
// page-level variable to hold random number generator
Random oRandom = new Random();
// -------------------------------------------------------------------
void Page_Load() {
// get DataSet from function shown next in this page
oCarsDS = GetCarDetailsDS("5");
// display details of this vehicle
dgrNesting.DataSource = oCarsDS.Tables["CarDetails"].DefaultView;
dgrNesting.DataBind();
}
// -------------------------------------------------------------------
DataSet GetCarDetailsDS(String sCarID) {
// function to get selection of car details in a DataSet
// to hold DataSet for the results
DataSet dsCarDetail = null;
// to hold stored procedure names
String sCarProcName = "GetAllCarDetails";
String sColorProcName = "GetAllCarColors";
String sExtrasProcName = "GetAllCarExtras";
String sConnect = ConfigurationSettings.AppSettings["XroxCarsConnectString"];
SqlConnection sqlConn = new SqlConnection(sConnect);
SqlCommand sqlComm = new SqlCommand(sCarProcName, sqlConn);
sqlComm.CommandType = CommandType.StoredProcedure;
try {
// create a new empty DataSet for the results
dsCarDetail = new DataSet();
// and fill it from the database
sqlComm.Parameters.Add("@CarID", sCarID);
SqlDataAdapter daCarDetail = new SqlDataAdapter(sqlComm);
sqlConn.Open();
daCarDetail.Fill(dsCarDetail, "CarDetails");
sqlComm.CommandText = sColorProcName;
daCarDetail.Fill(dsCarDetail, "CarColors");
sqlComm.CommandText = sExtrasProcName;
daCarDetail.Fill(dsCarDetail, "CarExtras");
}
catch (Exception e) {
return null;
}
finally {
sqlConn.Close();
}
return dsCarDetail;
}
// -------------------------------------------------------------------
void FillLists(object oSender, DataGridItemEventArgs oArgs) {
// runs when the OnItemDataBound event occurs
// used to bind nested list controls and select values
// in this example, values selected at random but could come from rows
// make sure its an Item or AlternatingItem row
if (oArgs.Item.ItemType == ListItemType.Item || oArgs.Item.ItemType == ListItemType.AlternatingItem) {
// get references to the three list controls and Image in this row
RadioButtonList oRadioList = (RadioButtonList)oArgs.Item.FindControl("radEngine");
CheckBoxList oCheckList = (CheckBoxList)oArgs.Item.FindControl("chkExtras");
DropDownList oDropList = (DropDownList)oArgs.Item.FindControl("selColor");
Image oImage = (Image)oArgs.Item.FindControl("imgColor");
// set DataSource and DataBind the RadioButtonList control
oRadioList.DataSource = oCarsDS.Tables["CarDetails"].DefaultView;
oRadioList.DataTextField = "EngineName";
oRadioList.DataValueField = "EngineID";
oRadioList.DataBind();
// set DataSource and DataBind the CheckBoxList control
oCheckList.DataSource = oCarsDS.Tables["CarExtras"].DefaultView;
oCheckList.DataTextField = "ExtraText";
oCheckList.DataBind();
// set DataSource and DataBind the DropDownList control
oDropList.DataSource = oCarsDS.Tables["CarColors"].DefaultView;
oDropList.DataTextField = "Color";
oDropList.DataValueField = "ColorID";
oDropList.DataBind();
// create random values for the controls
int iEngine = oRandom.Next(0, 3);
int iExtra1 = oRandom.Next(0, 6);
int iExtra2 = oRandom.Next(0, 6);
int iExtra3 = oRandom.Next(0, 6);
int iColor = oRandom.Next(0, 8);
// select RadioButton in "Engines" RadioButtonList control
oRadioList.SelectedIndex = iEngine;
// select three CheckBoxes in "Extras" CheckBoxList control
oCheckList.Items[iExtra1].Selected = true;
oCheckList.Items[iExtra2].Selected = true;
oCheckList.Items[iExtra3].Selected = true;
// could select item(s) in a list control by iterating
// through values to find a match for a specific value
foreach (ListItem oItem in oDropList.Items) {
if (oItem.Value == iColor.ToString()) oItem.Selected = true;
}
// or instead, if we know the SelectedIndex value (as here)
oDropList.SelectedIndex = iColor;
// get text from drop-down list and use to set Image source
String sColorName = oDropList.SelectedItem.Text;
oImage.ImageUrl = "../xroxcars/images/colors/" + sColorName + ".gif";
}
}
// -------------------------------------------------------------------
</script>
<!doctype html public "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<basefont size="2" face="Tahoma,Arial,Helvetica,sans-serif">
</head>
<body bgcolor="#ffffff" class="body-text">
<form runat="server">
<asp:DataGrid id="dgrNesting" runat="server" AutoGenerateColumns="False"
CellPadding="4"
Font-Size="10"
Font-Name="Tahoma,Arial,Helvetica,sans-serif"
HeaderStyle-HorizontalAlign="center"
HeaderStyle-BackColor="#b50055"
HeaderStyle-ForeColor="#ffffff"
HeaderStyle-Font-Bold="True"
ItemStyle-HorizontalAlign="center"
SelectedItemStyle-BackColor="#ffffcc"
OnItemDataBound="FillLists">
<Columns>
<asp:TemplateColumn HeaderText="Select Engine">
<ItemTemplate>
<asp:RadioButtonList style="font-size:12px" id="radEngine" runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Select Extras">
<ItemTemplate>
<asp:CheckBoxList style="font-size:12px" id="chkExtras" runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Select Color">
<ItemTemplate>
<asp:DropDownList style="font-size:12px" id="selColor" runat="server" /><p />
<asp:Image id="imgColor" border="1" runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -