对嵌入在用户控件中的gridview进行排序

时间:2011-03-10 13:12:44

标签: c# asp.net user-controls

我创建了一个asp.net usercontrol,它应该列出许多应用程序中的用户。为此目的,控件呈现转发器(foreach应用程序),转发器又呈现网格视图(与该应用程序的用户一起)。 控件呈现正常,但gridview中的列不可排序的事实除外。单击标题时没有任何操作(没有回发)。显然,单击标题时不会渲染任何JavaScript来执行回发。

这是代码:

[DefaultProperty("Text")]
[ToolboxData("<{0}:UserList runat=\"server\"></{0}:UserList>")]
public class UserList : WebControl
{
    #region Variables

    private Repeater list;
    private Literal heading;

    #endregion

    #region Events

    #endregion

    #region Constructors

    public UserList() : base()
    {
        // Default settings for list control. 
        list = new Repeater() { ID = "userList" };
        heading = new Literal() { ID = "userListHeading" };


    }

    #endregion

    #region Control event handlers

    protected override void OnInit(EventArgs e)
    {
        var css = "<link href=\"" + Page.ClientScript.GetWebResourceUrl(typeof(UserList), "Web.UI.Resources.CSS.UserList.css") + "\" type=\"text/css\" rel=\"stylesheet\" />";
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "UserListCss", css, false); 

        base.OnInit(e);
    }

    public override void RenderControl(HtmlTextWriter writer)
    {
        heading.RenderControl(writer);
        list.RenderControl(writer);
        base.RenderControl(writer);
    }

    protected override void OnLoad(EventArgs e)
    {




        // set properties
        list.ItemTemplate = new ListTemplate();

        // attach event handlers
        list.ItemDataBound += new RepeaterItemEventHandler(OnList_ItemDataBound);

        // bind data
        var allRoles = AdvancedRoleProvider.Instance.GetAllRoles();

        String appName = (Roles.ApplicationName.Equals("*") ? " alle applikationer" : Roles.ApplicationName);
        heading.Text = String.Format("<h2>{0}</h2>", "Brugere i " + appName);
        if (allRoles.Count == 0)
        {
            heading.Text += "Der kunne ikke findes nogen brugere.";
        }
        else
        {
            list.DataSource = allRoles;
            list.DataBind();
        }

        base.OnLoad(e);
    }

    #endregion


    #region List control event handlers

    protected void OnList_ItemDataBound(Object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            KeyValuePair<String, IList<String>> data = (KeyValuePair<String, IList<String>>)e.Item.DataItem;
            Literal litApplication = (Literal)e.Item.FindControl("litApplication");
            litApplication.Text = String.Format(litApplication.Text, data.Key);

            GridView gvUsers = (GridView)e.Item.FindControl("gvUsers");
            gvUsers.RowDataBound += new GridViewRowEventHandler(OnGridViewUsers_RowDataBound);
            gvUsers.Sorting += new GridViewSortEventHandler(OnGridViewUsers_Sorting);
            gvUsers.DataSource = AdvancedRoleProvider.Instance.GetApplicationUsers(data.Key);
            gvUsers.DataBind();
        }
    }

    #endregion

    void OnGridViewUsers_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    }

    void OnGridViewUsers_Sorting(object sender, GridViewSortEventArgs e)
    {
        // TODO: Impl. sorting logic
    }




    #region List layout template

    /// <summary>
    /// Layout template class for the repeater control.
    /// </summary>
    public class ListTemplate : ITemplate
    {

        #region Constructors

        public ListTemplate()
        {


        }







        #endregion

        #region Public methods

        public void InstantiateIn(Control container)
        {
            GridView gvUsers;
            gvUsers = new GridView() { ID = "gvUsers", AutoGenerateColumns = false, AllowSorting = true };

            BoundField userField = new BoundField() { HeaderText = "Bruger", DataField = "UserName", SortExpression = "UserName" };
            userField.HeaderStyle.CssClass = userField.ItemStyle.CssClass = "userName";
            BoundField fullNameField = new BoundField() { HeaderText = "Navn", DataField = "FullName", SortExpression = "FullName" };
            fullNameField.HeaderStyle.CssClass = userField.ItemStyle.CssClass = "userName";
            BoundField roleField = new BoundField() { HeaderText = "Rolle", DataField = "RoleName", SortExpression = "RoleName" };
            roleField.HeaderStyle.CssClass = roleField.ItemStyle.CssClass = "roleName";
            BoundField adField = new BoundField() { HeaderText = "AD Gruppe", DataField = "ADGroupName", SortExpression = "ADGroupName" };
            adField.HeaderStyle.CssClass = adField.ItemStyle.CssClass = "adGroupName";

            gvUsers.Columns.Add(userField);
            gvUsers.Columns.Add(fullNameField);
            gvUsers.Columns.Add(roleField);
            gvUsers.Columns.Add(adField);
            container.Controls.Add(new LiteralControl("<div id=\"userList\">"));
            container.Controls.Add(new Literal() { ID = "litApplication", Text="<h3>{0}</h3>" });
            container.Controls.Add(gvUsers);
            container.Controls.Add(new LiteralControl("</div>"));
        }


        #endregion

        #region Event handlers



        #endregion

        #region Properties




        #endregion
    }

    #endregion


}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我不知道这是否有效,但您可能想尝试通过designer / html添加网格。此外,您可能还想在html端添加处理程序而不是OnList_ItemDataBound。希望这会有所帮助。

<asp:GridView ID="gvUsers" runat="server" AllowSorting="True" onsorting="OnGridViewUsers_Sorting">