如何用text + LinkBut​​ton填充BulletedList? [英] How to populate a BulletedList with text+LinkButton?

查看:65
本文介绍了如何用text + LinkBut​​ton填充BulletedList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码,可用于使用网站中的当前角色以及一些其他信息来填充页面中的BulletedList.

I have this code that I use to populate a BulletedList in my page with the current roles in the site + some extra information.

到目前为止,由于所有这些都是与文本相关的,所以没问题.

So far, since all these are text-related, no problem.

现在,我希望在角色字符串的末尾有一个LinkBut​​ton控件,如果按下该按钮,它将删除角色(并重新填充列表)(如Visual Studio中的管理工具所具有的功能)

Now I would like to have a LinkButton control at the end of the role string which if pressed it will delete the role (and repopulate the list) (like the functionality that the admin tool in visual studio has)

我的问题是:是否可以在项目符号列表中执行此操作,还是应该使用其他控件(例如gridview)?

My question is: Is there a way to do that in a bulleted list, or should I use another control (like a gridview)?

private void BindRolesToList()
    {
        string[] roles = Roles.GetAllRoles();
        string[] data = new string[Roles.GetAllRoles().Count()];

        for (int i = 0; i<roles.Length; i++ )
        {
            data[i] = "Role name: " + roles[i] + ". Number of members in the role: " + Roles.GetUsersInRole(roles[i]).Count() + ".";
        }

        RoleList.DataSource = data;
        RoleList.DataBind();    
    }

推荐答案

仔细研究之后:

简短的回答:不,不能在 BulletedList 内添加另一个控件. BulletedList 不支持模板.

Short answer: No, adding another control inside a BulletedList is not possible. BulletedLists do not support templating.

如果您希望整个角色字符串是 HyperLink LinkBut​​ton ,则只需设置

If you want the whole role string to be a HyperLink or a LinkButton, you can simply set the DisplayMode of the BulletedList.

如果希望LinkBut​​ton出现在文本字符串的末尾,则需要使用更灵活的列表结构. Repeater 可能最容易获得您所描述的效果.

If you want the LinkButton to appear at the end of the text string, you need to use a more flexible list structure. Repeater is probably the easiest to get the effect you are describing.

类似

   <asp:Repeater id="Repeater1" runat="server">
      <HeaderTemplate>
         <ul>
      </HeaderTemplate>

      <ItemTemplate>
         <li>Role name: <%# DataBinder.Eval(Container.DataItem, "RoleName") %>. 
             Number of members in the role: <%# DataBinder.Eval(Container.DataItem, "RoleCount") %>. 
             <asp:LinkButton onCommand="LinkButton_Command" 
                 CommandArgument="<%# DataBinder.Eval(Container.DataItem, "RoleName") %>"
                 />
         </li>
      </ItemTemplate>

      <FooterTemplate>
         </ul>
      </FooterTemplate>

   </asp:Repeater>

带有代码后盖

private void BindRolesToList()
{
    string[] roles = Roles.GetAllRoles();

    //make a table for the data, with two columns, RoleName and RoleCount
    DataTable data = new DataTable();

    DataColumn column;

    column = New DataColumn();
    column.DataType = System.Type.GetType("System.String");
    column.ColumnName = "RoleName";
    table.Columns.Add(column);

    column = New DataColumn();
    column.DataType = System.Type.GetType("System.Int32");
    column.ColumnName = "RoleCount";
    table.Columns.Add(column);

    // Populate the data
    DataRow row;
    for (int i = 0; i<roles.Length; i++ )
    {
         row = data.NewRow();
         row["RoleName"] = roles[i];
         row["RoleCount"] = Roles.GetUsersInRole(roles[i]).Count();
         data.Rows.Add(row);
    }

    RoleList.DataSource = data;
    RoleList.DataBind();    
}

private void LinkButton_Command(Object sender, CommandEventArgs e) 
{
    string RoleName = e.CommandArgument;

    //whatever code deletes the role, e.g.
    //  Roles.DeleteRole(RoleName);

    BindRolesToList();

}

这篇关于如何用text + LinkBut​​ton填充BulletedList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆