超链接、按钮的列表,以及打开到新浏览器窗口的任何内容
本文关键字:浏览器 窗口 任何内 按钮 列表 超链接 | 更新日期: 2023-09-27 18:00:05
我需要在ASP.NET网页(aspx)中创建一个显示用户名列表的小窗口,当您单击用户名时,我需要一个新的浏览器窗口(而不是选项卡)来打开特定的大小。我可以打开新的浏览器窗口,假设我使用的任何控件都能让我进入一个代码隐藏函数,在那里我可以调用。。。。
string url = "http://www.dotnetcurry.com";
ScriptManager.RegisterStartupScript(this, this.GetType(), "OpenWin", "<script>openNewWin ('" + url + "')</script>", false);
这是链接页面中的标记。
<script language="javascript" type="text/javascript">
function openNewWin(url) {
var x = window.open(url, 'mynewwin', 'width=600,height=600,toolbar=1');
x.focus();
}
</script>
1.)对于这个问题,我应该使用什么控件,从数据库中取回相应的用户名后,结构是什么样子的?
这是我最近的一次。。。这段代码使用了一个ASP.NET项目符号列表,我正试图将其绑定到一个HTML链接列表,我希望它不指向任何地方,而是将我带到代码后台。相反,这段代码实际上在页面上呈现为HTML(它没有被解析为超链接。)
protected void Page_Load(object sender, EventArgs e)
{
UsersBulletedList.DataSource = theContext.GetOnlineFavorites(4);
UsersBulletedList.DataBind();
}
public IQueryable<String> GetOnlineFavorites(int theUserID)
{
List<String> theUserList = new List<String>();
IQueryable<Favorite> theListOfFavorites= this.ObjectContext.Favorites.Where(f => f.SiteUserID == theUserID);
foreach (Favorite theFavorite in theListOfFavorites)
{
string theUserName = this.ObjectContext.SiteUsers.Where(su => su.SiteUserID == theFavorite.FriendID && su.LoggedIn == true).FirstOrDefault().UserName;
yourOnlineFavorites.Add("<a href='RealTimeConversation.aspx?UserName=" + theUserName + "'>" + theUserName + "</a>");
//this needs to help me get into a codebehind method instead of linking to another page.
}
return yourOnlineFavorites.AsQueryable();
}
我会在页面上创建一个Repeater
,并通过GetOnlineFavorites
方法绑定结果。在Repeater
中,放置一个带有ItemCommand
事件的LinkButton
,该事件会将脚本添加到页面中。
标记:
<asp:Repeater ID="repeater" runat="server" OnItemCommand="repeater_ItemCommand">
<ItemTemplate>
<asp:LinkButton runat="server" ID="linkButton"
Text='<%# Eval("PropertyFromBindingCollection") %>'
CommandName="OpenWindow"
CommandArgument='<%# Eval("AnotherProperty") %>' />
</ItemTemplate>
</asp:Repeater>
在这里,LinkButton
的Text
属性和Repeater
的CommandArgument
属性将设置为集合中的某些属性。
代码背后:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
repeater.DataSource = theContext.GetOnlineFavorites(4);
repeater.DataBind();
}
}
protected void repeater_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
if(e.CommandName == "OpenWindow")
{
string arg = e.CommandArgument; // this could be the url, or a userID to get favorites, or...?
//Your open window script
}
}
现在,当用户单击其中一个链接按钮时,它将为Repeater
触发ItemCommand
事件,检查CommandName
(在链接按钮上设置),然后在LinkButton
上获得CommandArgument
设置。如果您将CommandArgument
设置为URL,则可以在OpenWin脚本中使用该URL,否则,使用您设置的任何数据作为参数来获得要打开的URL。