如何为asp.net中的每个项目添加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(); } 

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

 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 } 

使用ToolTip属性:

   

这是你要问的吗?

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

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

您可以使用PreRender事件 – 循环项(应该是ListItems),并且可以根据复选框的值为title设置html属性。

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

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

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

这是我使用的,具有更多function,例如使ListItem看起来像一个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;"); } } } } } } } } 

所以实际上我根据DatSource的数据放置了一个唯一的ToolTip,我将ListItem的外观更改为蓝色下划线。