【问题标题】:How to add tooltip for Checkboxlist for each item in asp.net如何为asp.net中的每个项目添加复选框列表的工具提示
【发布时间】:2011-12-28 18:06:09
【问题描述】:
<asp:CheckBoxList ID="ckl_EditRole" DataValueField="RoleName" runat="server">
                                    </asp:CheckBoxList>
public void BindListBoxPermission(int field)
    {
        MySqlCommand command = new MySqlCommand();
        DataSet ds = new DataSet();
        int newOrgID = field;
        string MysqlStatement = "SELECT RoleName from tbl_Role Where RoleID >1 order by RoleID desc";
        MySqlParameter[] param = new MySqlParameter[0];
        ds = server.ExecuteQuery(CommandType.Text, MysqlStatement, param);
        ckl_EditRole.DataSource = ds;
        ckl_EditRole.DataBind();
    }

每个项目的工具提示都不同,管理员工具提示是创建用户,用户工具提示是创建消息。如何为复选框内的每个项目添加工具提示

【问题讨论】:

  • 你实际上应该在正文中问一个问题。
  • 是的,我已经改变了我的问题,谢谢

标签: c# asp.net tooltip checkboxlist


【解决方案1】:
protected void Page_PreRender(object sender, EventArgs e)
{
    foreach (ListItem item in ckl_EditRole.Items)
    {
        item.Attributes["title"] = GetRoleTooltip(item.Value);
    }
}

private static string GetRoleTooltip(string p)
{
    // here is your code to get appropriate tooltip message depending on role
}

【讨论】:

    【解决方案2】:

    使用 ToolTip 属性:

    <asp:CheckBoxList ID="ckl_EditRole" DataValueField="RoleName" runat="server" ToolTip="Roles">
    </asp:CheckBoxList>
    

    这是你要问的吗?

    如果您想为每个项目更新 ToolTip,那么您需要分别对待它们:

    for (int i = 0; i < ckl_EditRole.Items.Count; i++)
       ckl_EditRole.Items[i].Attributes["title"] = "custom Tooltip";
    

    【讨论】:

    • 每个项目的工具提示都不同,管理员工具提示是创建用户,用户工具提示是创建消息
    【解决方案3】:

    您可以使用 PreRender 事件——循环遍历项目(应该是 ListItems),并且您可以根据复选框的值设置标题的 html 属性。

    如果我想对复选框有很大的控制权,我可能倾向于在转发器中放置一个复选框——但这可能不是必需的。

    【讨论】:

      【解决方案4】:

      可以在页面加载方法上编写如下sn-p代码: chkbox.Items[0].Attributes.Add("Title", "Admin"); chkbox.ToolTip = "管理员";

      chkbox.Items[1].Attributes.Add("Title", "User"); chkbox.ToolTip = "用户";

      【讨论】:

        【解决方案5】:

        这是我使用的,具有更多功能,例如使 ListItem 看起来像一个链接按钮。

            protected void FormatPaskWeeksPerStudentRow(GridViewRow gvRow)
            {
                    SqlDataSource sdsTETpastWeeks = (SqlDataSource)gvRow.FindControl("sdsTETpastWeeks");
                    sdsTETpastWeeks.SelectParameters["StudentID"].DefaultValue = hfStudentID.Value.ToString();
                    if (sdsTETpastWeeks != null)
                    {
                        CheckBoxList cbl1 = (CheckBoxList)gvRow.FindControl("listWeeksTracking");
                        if (cbl1 != null)
                        {
                            cbl1.DataBind();
        
                            foreach (ListItem litem in cbl1.Items)
                            {
                                //disable the checkbox for now
                                litem.Enabled = false;
        
                                //see if any of the past weeks (excluding this week) needs to be highlighted as a hyperlink to show past comments
                                //get the Tracking value. If set, then mark the checkbox as Selected or Checked
                                DataSourceSelectArguments dss = new DataSourceSelectArguments();
                                DataView dv = sdsTETpastWeeks.Select(dss) as DataView;
                                DataTable dt = dv.ToTable() as DataTable;
                                if (dt != null)
                                {
                                    //this loops through ALL the weeks available to the student, for this block
                                    //it tries to match it against the current ListItem for the week it's loading and determines if they match
                                    //if so then mark the item selected (checked=true) if the value in the sub query says it's true
                                    foreach (DataRow dr in dt.Rows)
                                    {
                                        if (litem.Text == dr.ItemArray[0].ToString() && litem.Text != ddlWeekNo.SelectedItem.Text)
                                        {
                                            if ((bool)dr.ItemArray[1])
                                                litem.Selected = true;
        
                                            //for those that were not ticked in prior weeks, make a ToolTip with the text/comment made in that week and underscore the week number
                                            else
                                            {
                                                litem.Attributes["title"] = dr.ItemArray[2].ToString();
                                                litem.Attributes.Add("style", "color:Blue;font-style:italic;text-decoration:underline;");
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
        }
        

        因此,实际上我放置了一个基于来自 DatSource 的数据的唯一工具提示,并将 ListItem 的外观更改为蓝色下划线。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-05-28
          • 2010-09-16
          • 2020-05-22
          • 2021-04-29
          • 2019-06-08
          • 2011-01-08
          相关资源
          最近更新 更多