
但是,請注意我們仍然必須加載單選按鈕串列并設定組合框以提供不同的選擇集。
因此,我們將此代碼添加到行資料系結事件中。
protected void GHotels_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView gData = e.Row.DataItem as DataRowView;
// based on Gender fill cbo box
string strSQL = "SELECT ID, Feature FROM tblHotelOptions ";
if (gData["Gender"].ToString() == "M")
{
strSQL = "WHERE Gender = 'M'";
}
else
{
strSQL = "WHERE Gender = 'F'";
}
strSQL = " ORDER BY Feature";
DropDownList cboOption = e.Row.FindControl("cboOption") as DropDownList;
cboOption.DataSource = MyRst(strSQL);
cboOption.DataBind();
cboOption.Items.Insert(0, new ListItem("Select", "0"));
if (gData["HotelOption"] != null)
cboOption.SelectedValue = gData["HotelOption"].ToString();
}
}
因此,對于每個網格行,我們處理資料行資訊,并根據性別有條件地加載下拉串列。
現在,這適用于“加載”,但現在我們需要添加事件和代碼,以便我們更改單選按鈕選項。
因此,在標記中,我們將此事件添加到單選按鈕:
我們必須使用標記和 intel-sense 來彈出創建新事件的選項,如下所示:

因此,我們將 autopostback=true 添加到單選按鈕串列和選定的更改事件中。
因此,現在當您更改單選選項時,我們需要運行代碼來更改下拉串列選項。
因此,該代碼可能如下所示:
protected void RGender_SelectedIndexChanged(object sender, EventArgs e)
{
RadioButtonList rBtn = sender as RadioButtonList;
GridViewRow gRow = rBtn.NamingContainer as GridViewRow;
string strSQL = "SELECT ID, Feature FROM tblHotelOptions ";
if (rBtn.SelectedItem.Text == "M")
// based on Gender fill cbo box
{
strSQL = "WHERE Gender = 'M'";
}
else
{
strSQL = "WHERE Gender = 'F'";
}
strSQL = " ORDER BY Feature";
DropDownList cboOption = gRow.FindControl("cboOption") as DropDownList;
cboOption.DataSource = MyRst(strSQL);
cboOption.DataBind();
cboOption.Items.Insert(0, new ListItem("Select", "0"));
}
事實上,代碼與我們加載網格的方式非常相似。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/462429.html
