我已将下拉框放在我的网站中,但我不知道如何将每个选项(即url1,url2等)与新标签或页面绑定。
<form>
<label>
<u>S</u>earch Engines
</label>
<select accesskey="S" onchange="goToPage(this.options(this.selectedIndex).value)">
<option selected>Please select one</option>
<option value="http://search.msn.com/">MSN Search</option>
<option value="http://www.google.com/">Google</option>
<option value="http://www.search.com/">Search.com</option>
<option value="http://www.dogpile.com/">Dogpile</option>
</select>
</form>
我希望用户只选择一个选项,然后页面会将他重定向到他选择的网站。
请问怎么办?
答案 0 :(得分:1)
根据您的要求,以下代码是完美的
StringBuilder sb = new StringBuilder();
Label lbl = new Label();
sb.Append("<select accesskey='S' onchange='if (this.value) window.location.href=this.value'>");
using (SPSite oSiteCollection = new SPSite(SPContext.Current.Web.Url))
{
SPWebCollection collWebsites = oSiteCollection.AllWebs;
foreach (SPWeb oWebsite in collWebsites)
{
sb.Append("<option value='"+oWebsite.Url+"'>"+oWebsite.Name+"</option>");
}
}
sb.Append("</select>");
lbl.Text = sb.ToString();
this.Controls.Add(lbl);
只需添加到您的Webpart并部署它.Bingo:)
答案 1 :(得分:0)
这样做: -
<select name="siteURL" accesskey="S" onchange="window.open(siteURL.options[selectedIndex].value)">
答案 2 :(得分:-1)
请将此代码添加到您的ascx:
<asp:DropDownList runat="server" ID="ddlSites" AutoPostBack="True" OnTextChanged="SiteSelected" ></asp:DropDownList>
并在后面的代码中添加此代码:
private void BindSiteDropDown()
{
var sites = new Dictionary<string, string>()
{
{"MSN Search", "http://search.msn.com/"},
{"Google", "http://google.com"}
};
ddlSites.Items.Clear();
foreach (var site in sites)
{
ddlSites.Items.Add(new ListItem(site.Key, site.Value));
}
}
protected void SiteSelected(object sender, EventArgs e)
{
Page.Response.Redirect(((DropDownList)sender).SelectedValue);
}
并在页面加载中调用BindSiteDropDown()方法,例如:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)BindSiteDropDown();
}