让DropDownList添加ToolTip(title)属性。为下拉式菜单(DropDownList)添加第一个选择。

刚才一律各网友在SKYPE问及,如何让asp.net的DropDownList控件添加ToolTip(title)属性。
Insus.NET回复他说,同这样的问题,在google或baidu应该多多。他可回说,一不思量使用Javascript,二真正的是思念看你的解决方式。

森计可呢耶下拉式菜单(DropDownList)添加第一只选择,下面是Insus.NET小毕了几个方式,仅供参考:

下是Insus.NET实现之力量:

Html code:

betway必威登录 1

betway必威登录 2betway必威登录 3View Code

 

<body>
    <form id=”form1″ runat=”server”>
    <div>
        <asp:DropDownList ID=”DropDownList1″ runat=”server”>            
        </asp:DropDownList>
    </div>
    </form>
</body>

贯彻过程,只是啊DropDownList控件写OnDataBound事件,

 

betway必威登录 4betway必威登录 5DropDownList1_DataBound

数据源与绑定:

 protected void DropDownList1_DataBound(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;

betway必威登录 6betway必威登录 7View Code

        for (int i = 0; i < ddl.Items.Count; i++)
        {
            //当然你可于此吧每个item写上其他的显得的始末。
            ddl.Items[i].Attributes.Add(“title”, ddl.Items[i].Text);  
        }
    }

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Data_Binding();
        }
    }

 

    private void Data_Binding()
    {
        this.DropDownList1.DataSource = Site();
        this.DropDownList1.DataTextField = “key”;
        this.DropDownList1.DataValueField = “value”;
        this.DropDownList1.DataBind();
    }

整的程序,xxx.aspx:

    private Dictionary<string, string> Site()
    {
        Dictionary<string, string> site = new Dictionary<string, string>();
        site.Add(“Insus.NET cnblogs”, “http://insus.cnblogs.com”);
        site.Add(“Microsoft”, “http://www.microsoft.com”);
        site.Add(“Google”, “http://www.google.com”);
        return site;
    }

betway必威登录 8betway必威登录 9View Code

 

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default.aspx.cs” Inherits=”_Default” %>

以下有所办法,均为上述html或code作变动。

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

 

<html xmlns=”http://www.w3.org/1999/xhtml"&gt;
<head runat=”server”>
    <title></title>
</head>
<body>
    <form id=”form1″ runat=”server”>
    <div>
       <asp:DropDownList ID=”DropDownList1″ runat=”server” OnDataBound=”DropDownList1_DataBound”>
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>

首先种,修改Html
Code,把DropDownList属性AppendDataBoundItems的值设为true,然后直接抬高一个item:<asp:ListItem Text=”–选择–”
Value=””></asp:ListItem> 在DropDownList内。

 

betway必威登录 10betway必威登录 11View Code

xxx.aspx.cs:

 <asp:DropDownList ID=”DropDownList1″ runat=”server” AppendDataBoundItems=”true”>    
        <asp:ListItem Text=”–选择–” Value=””></asp:ListItem>        
        </asp:DropDownList>

betway必威登录 12betway必威登录 13View Code

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

老二栽办法,Html Code无需更改,

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Data_Binding();
        }
    }

betway必威登录 14betway必威登录 15View Code

    private void Data_Binding()
    {
        DropDownList1.DataSource = Datas();
        DropDownList1.DataTextField = “value”;
        DropDownList1.DataValueField = “key”;
        DropDownList1.DataBind();
    }   

 <asp:DropDownList ID=”DropDownList1″ runat=”server” >                  
        </asp:DropDownList>

    protected void DropDownList1_DataBound(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;

 

        for (int i = 0; i < ddl.Items.Count; i++)
        {
            //当然你得以此地呢每个item写上实际的展示的情节。
            ddl.Items[i].Attributes.Add(“title”, ddl.Items[i].Text);  
        }
    }

修改Data_Binding()方法,如下: 

    private Dictionary<int, string> Datas()
    {
        Dictionary<int, string> Dd = new Dictionary<int, string>();
        Dd.Add(1, “米饭”);
        Dd.Add(3, “紫菜汤”);
        Dd.Add(4, “菜心”);
        Dd.Add(5, “肉片”);
        return Dd;
    }
}

betway必威登录 16betway必威登录 17View Code

 

private void Data_Binding()
    {
        this.DropDownList1.DataSource = Site();
        this.DropDownList1.DataTextField = “key”;
        this.DropDownList1.DataValueField = “value”;
        this.DropDownList1.DataBind();

        //添加底下代码:
        DropDownList1.Items.Insert(0, new ListItem( “–选择–“,””));
         
    }

 

其三栽方式:

Html改吗如下,设置AppendDataBoundItems属性与同贯彻OnDataBound事件:

betway必威登录 18betway必威登录 19View Code

<asp:DropDownList ID=”DropDownList1″ runat=”server” AppendDataBoundItems=”true” OnDataBound=”DropDownList1_DataBound” >                  
        </asp:DropDownList>

 

cs code:

betway必威登录 20betway必威登录 21View Code

 protected void DropDownList1_DataBound(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;
        ddl.Items.Insert(0, new ListItem(“–选择–“, “0”));
    }