如何为asp.net中的每个项目添加Checkboxlist的工具提示

时间:2011-11-14 15:20:55

标签: c# asp.net tooltip checkboxlist

<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();
    }

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

5 个答案:

答案 0 :(得分:15)

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
}

答案 1 :(得分:0)

使用ToolTip属性:

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

这是你要问的吗?

如果您想更新每个项目的工具提示,则需要单独处理它们:

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

答案 2 :(得分:0)

你可以使用PreRender event--循环遍历项目(应该是ListItems),你可以根据复选框的值设置标题的html属性。

如果我希望对复选框有很多控制权,我可能会倾向于在转发器中添加一个复选框 - 但这可能不是必需的。

答案 3 :(得分:0)

您可以在页面加载方法上编写以下代码段:  chkbox.Items [0] .Attributes.Add(“Title”,“Admin”);  chkbox.ToolTip =“Admin”;

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

答案 4 :(得分:0)

这是我使用的,具有更多功能,例如使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的外观更改为蓝色下划线。