2

我需要在显示用户名列表的 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();
    }
4

1 回答 1

2

我会在您的页面上创建一个并从该方法Repeater绑定您的结果。GetOnlineFavorites在 中Repeater,放置一个LinkButton带有ItemCommand将您的脚本添加到页面的事件的事件。

标记:

<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>

在这里, the和 the的Text属性将被设置为您集合中的某些属性。LinkButtonCommandArgumentRepeater

代码背后:

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
    }
}

现在,当用户单击其中一个 LinkBut​​tons 时,它将触发 的ItemCommand事件Repeater,检查CommandName(在 LinkBut​​ton 上设置),然后CommandArgumentLinkButton. 如果将 设置CommandArgument为 URL,则可以在 OpenWin 脚本中使用它,否则使用您设置的任何数据作为参数来获取要打开的 URL。

于 2013-02-19T18:03:32.823 回答