我有2个下拉列表ddl1和ddl2。当我为ddl1设置所选值时,它可以正常工作,但是在为ddl2设置选定值之后也会更改ddl1的值。我不知道它是怎么做的。
<asp:DropDownList ID="ddlPickup" runat="server" CssClass="tb_date"
>
</asp:DropDownList>
Dropoff:
<asp:DropDownList ID="ddlDropoff" runat="server" CssClass="tb_date" >
</asp:DropDownList>
<asp:RadioButtonList ID="radiobuttonlist1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="radiobuttonlist1_SelectedIndexChanged"
RepeatDirection="Horizontal">
<asp:ListItem Value="1">Day or Longer</asp:ListItem>
<asp:ListItem Value="2">1/2 Day</asp:ListItem>
<asp:ListItem Value="3">Hourly</asp:ListItem>
</asp:RadioButtonList>
protected void radiobuttonlist1_selectedindexchange(object sender, EventArgs e)
{
if(radiobuttonlist1.SelectedValue=="1")
{
Setddl();
}
}
public void Setddl()
{
if (ddlPickup.Items.Count > 0)
{
ddlPickup.Items.Clear();
}
if (ddlDropoff.Items.Count > 0)
{
ddlDropoff.Items.Clear();
}
ListItem li = new ListItem();
DateTime date = Convert.ToDateTime(StartDate.Text);
int day = Convert.ToInt32(date.DayOfWeek);
long itemid = Convert.ToInt64(ddlRentalItem.SelectedValue);
if (itemid != 0)
{
var dropoff = _objRitems.GetHourlyHourByDay(itemid, day);
if (dropoff.Count > 0)
{
int stimeh = Convert.ToDateTime(dropoff[0].OpenTime).Hour;
int etimeh = Convert.ToDateTime(dropoff[0].CloseTime).Hour;
DateTime dt = Convert.ToDateTime(dropoff[0].OpenTime);
for (int i = 0; i <= (etimeh - stimeh) * 2; i++)
{
string time = string.Format("{0:t}", dt);
li = new ListItem(time, time);
ddlPickup.Items.Add(li);
ddlDropoff.Items.Add(li);
dt = dt.AddMinutes(30);
}
ddlPickup.DataBind();
ddlDropoff.DataBind();
ddlPickup.SelectedValue=Request.QueryString["droptime"].ToString();
ddlDropoff.SelectedValue=Request.QueryString["droptime"].ToString();
//as soon as ddlDropoff SelectedValue assigns ddlPickup.SelectedValue changes to ddlDropoff.SelectedValue..Very Weird!!!
}
else
{
}
}
else
{
}
}
答案 0 :(得分:0)
如果有条件,你需要在下面写下你的下拉代码。
如果(!的IsPostBack) {
}
答案 1 :(得分:0)
忘掉ddlPickup_DataBound
。依赖于隐藏在下拉列表中的ViewState来重新填充选定的值。在这种情况下,ondatabound方法是不必要的。
此外,您只需要在Page.IsPostBack
上绑定下拉列表 - 就像另一张海报所说的那样
这是处理跌落的更标准方法,即
您正在这样做 - 数据绑定将在每个页面加载时触发(如果autopostback为true,则在每次下拉列表后)。这两个下拉列表每次都会重新填充 - 取决于您Request
集合中的内容。这不是你想要的,并且会解释这种奇怪的行为。
修改强>
要明确这是应该遵循的模式
<强>标记强>
<asp:DropDownList ID="ddlPickup" runat="server" ></asp:DropDownList>
<asp:DropDownList ID="ddlDropoff" runat="server" OnSelectedIndexChanged="selectedIndexEvent"></asp:DropDownList>
代码
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
BindDropDownLists();
}
}
protected void ddl2_selectedindexchange(object sender, EventArgs e)
{
//.. legit - do whatever is needed but don't reset selected value
}
protected void radiobuttonlist1_selectedindexchange(object sender, EventArgs e) {
{
BindDropDownLists();
SetInitialDropDownValues();
}
不需要ondatanound