现在有一个DropDownList,数据是从数据库绑定的,在绑定的时候插入了一个默认项
DropDownList.Items.Insert(0, new ListItem("请选择", ""));
DropDownList的Value绑定的是htttp://www.xxx.com这种地址
在SelectedIndexChanged事件里获取Value的值,并打开对应的地址
if (this.DropDownList1.SelectedValue.ToString() != "")
{
string urlstr = this.DropDownList1.SelectedValue.ToString();
Page.RegisterStartupScript("打开", "<script language=javascript>window.open('" + urlstr + "','_blank');</script>");
}
问题是在页面我选择了一项打开对应的地址后,再刷新页面,会又打开一个我刚才选择项的地址,我在Page_Load里初始化
this.DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(zyljdlist1.Items.FindByText("请选择"));
this.DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(zyljdlist1.Items.FindByValue(""));
DropDownList1了也没用,哪位知道这是什么原因?
DropDownList.Items.Insert(0, new ListItem("请选择", ""));
DropDownList的Value绑定的是htttp://www.xxx.com这种地址
在SelectedIndexChanged事件里获取Value的值,并打开对应的地址
if (this.DropDownList1.SelectedValue.ToString() != "")
{
string urlstr = this.DropDownList1.SelectedValue.ToString();
Page.RegisterStartupScript("打开", "<script language=javascript>window.open('" + urlstr + "','_blank');</script>");
}
问题是在页面我选择了一项打开对应的地址后,再刷新页面,会又打开一个我刚才选择项的地址,我在Page_Load里初始化
this.DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(zyljdlist1.Items.FindByText("请选择"));
this.DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(zyljdlist1.Items.FindByValue(""));
DropDownList1了也没用,哪位知道这是什么原因?
<div>
<asp:DropDownList ID="DropDownList1" runat="server"
onselectedindexchanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="请选择" Value=""></asp:ListItem>
<asp:ListItem Text="博客园" Value="http://www.cnblogs.com"></asp:ListItem>
<asp:ListItem Text="CSDN" Value="http://bbs.csdn.net/"></asp:ListItem>
</asp:DropDownList>
</div>
</form>
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string url = DropDownList1.SelectedValue;
if (!string.IsNullOrEmpty(url) && (Session["Url"] == null || Session["Url"].ToString() != url))
{
Session["Url"] = url;
Page.RegisterStartupScript("打开", "<script language=javascript>window.open('" + url + "','_blank');</script>");
}
}
{
string urlstr = this.DropDownList1.SelectedValue.ToString();
Page.RegisterStartupScript("打开", "<script language=javascript>window.open('" + urlstr + "','_blank');</script>");
}
你判断的是 选中某一个值相应的跳转 而不是选择之后触发的事件 刷新页面当然会跳转咯
this.DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(zyljdlist1.Items.FindByText("请选择"));
this.DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(zyljdlist1.Items.FindByValue(""));
初始化了DropDownList1,这个时候我再点"博客园"就不会打开对应的页面,这个没明白。
this.DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(zyljdlist1.Items.FindByText("请选择"));
this.DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(zyljdlist1.Items.FindByValue(""));
初始化DropDownList1,使DropDownList1在每次选择后回到初始状态,然后,我点击"博客园"打开对应的页面,这个时候DropDownList1显示的是请选择,这个时候我再点击"博客园"就不会打开对应的地址,这是为什么?
{
Session["Url"] = null;
zyljdlist1bind();
this.DropDownList1.SelectedIndex = zyljdlist1.Items.IndexOf(zyljdlist1.Items.FindByText("请选择"));
}
这样写没有啊
楼主是想刷新后不选择所以正确的做法就应该是在
if(!IsPostBack){
this.DropDownList1.SelectedIndex = 0;
}
if (!Page.IsPostBack)
{
Session["Url"] = null;
DropDownList1bind();
this.DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(zyljdlist1.Items.FindByText("请选择"));
}
这样写没用啊。请指点。
<div>
<asp:DropDownList ID="DropDownList1" runat="server"
onselectedindexchanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="请选择" Value=""></asp:ListItem>
<asp:ListItem Text="博客园" Value="http://www.cnblogs.com"></asp:ListItem>
<asp:ListItem Text="CSDN" Value="http://bbs.csdn.net/"></asp:ListItem>
</asp:DropDownList>
</div>
</form>
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string url = DropDownList1.SelectedValue;
Page.RegisterStartupScript("打开", "<script language=javascript>window.open('" + url + "','_blank');location.href=location.href</script>");
}
这样应该是满足了你的需求了