好的,这个问题看起来是您在标记中设置了中继器的数据源。因此,对于任何回发,您的重新加载和重新绑定代码都会再次运行,这会导致您所做的任何选择丢失(重新绑定会一次又一次地重新运行行数据绑定代码)。因此,您所做的任何更改都会因回发而丢失。
简单的解决方案是将数据绑定到后面代码中的中继器,并且仅一次。
因此,说出这个简单的标记:
<div style="float: left; width: 30%">
<asp:Repeater ID="Repeater1" runat="server"
OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<h4>Booking Infomration</h4>
<asp:Label ID="lblPerson" runat="server" Width="200px"
Text='<%# Eval("FirstName") + " " + Eval("LastName") %>'>
</asp:Label>
<asp:Label ID="Label1" runat="server" Text="Hotel:"
Style="margin-left: 30px">
</asp:Label>
<asp:DropDownList ID="cboHotel" runat="server"
DataValueField="ID"
DataTextField="HotelName"
AutoPostBack="true"
OnSelectedIndexChanged="cboHotel_SelectedIndexChanged">
</asp:DropDownList>
<br />
<hr style="height: 2px; border: none; background-color: black" />
</ItemTemplate>
</asp:Repeater>
</div>
<div style="float: left; margin-left: 30px">
<h3>Combo box selected row information</h3>
<asp:TextBox ID="txtInfo" runat="server"
TextMode="MultiLine" Height="160px" Width="350px"></asp:TextBox>
</div>
我们的代码在后面,请注意我们如何确保仅在第一页加载时绑定数据(IsPostBack = false)。
因此这段代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadData();
}
void LoadData()
{
string strSQL =
@"SELECT ID, HotelName FROM tblHotels
ORDER BY HotelName";
rstHotels = General.MyRst(strSQL);
strSQL = "SELECT * FROM People ORDER BY FirstName";
Repeater1.DataSource = General.MyRst(strSQL);
Repeater1.DataBind();
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
DropDownList cboHotel = (DropDownList)e.Item.FindControl("cboHotel");
cboHotel.DataSource = rstHotels;
cboHotel.DataBind();
cboHotel.Items.Insert(0, new ListItem("Please select", "0"));
DataRowView rowData = (DataRowView)e.Item.DataItem; // current binding row data
if (rowData["Hotel_id"] != DBNull.Value)
cboHotel.SelectedValue = rowData["Hotel_id"].ToString();
}
}
protected void cboHotel_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList cboHotel = (DropDownList)sender;
RepeaterItem rptRow = (RepeaterItem)cboHotel.NamingContainer;
string sInfo = "";
sInfo += $"Row index selected = {rptRow.ItemIndex}\n";
Label lblPerson = (Label)rptRow.FindControl("lblPerson");
sInfo += $"Row person information = {lblPerson.Text}\n";
sInfo += $"combo Hotel id = {cboHotel.SelectedItem.Value}\n";
sInfo += $"combo Hotel Name (text) = {cboHotel.SelectedItem.Text}\n";
txtInfo.Text = sInfo;
}
现在的结果是这样的:
因此,通常可以在标记中放置一些数据源,但是一旦您有了回发和附加代码逻辑,那么我建议您通过使用后面的代码来“控制”数据绑定,而不是离开重新绑定可能会或可能不会再次发生。