How to add tooltip for Checkboxlist for each item in asp.net

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

For each item tooltip is different, for admin tooltip is creates user and for users tooltip is creates message. How can I add tooltip for each item inside the check box

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

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

Method 2

Use the ToolTip property:

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

Is this what you are asking?

If you want to update the ToolTip for each item then you need to treat them separately:

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

Method 3

You can use PreRender event– loop over the items (should be ListItems), and you can set an html attribute for title based on the values of the checkbox.

In cases where I want to have alot of control over the checkboxes, I might favor putting a checkbox in a repeater– but that might not be necessary here.

Method 4

You can write the following snippet of code on the page load method:
chkbox.Items[0].Attributes.Add(“Title”, “Admin”);
chkbox.ToolTip = “Admin”;

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

Method 5

This is what I use, with more features, like making the ListItem look like a linkbutton.

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

So in effect I am placing a ToolTip that’s unique based on the data from the DatSource and I change the appearance of the ListItem to blue underline.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x