日期:2014-05-17  浏览次数:20840 次

dropdownlist怎样根据text来选定某项
如题...
我的dropdownlist是绑定xml的..
所选择的结果保存到数据库里面..

现在我想根据保存在数据库里面的数据是什么..dropdownlist就做为默认值..

例如...dropdownlist有广东,南海,山西这几项...我数据库的保存数据是"南海"..那dropdownlist就要把"南海"做为默认值显示出来..
请问怎么才能实现这样的功能呢~!?


------解决方案--------------------
C# code

<asp:Dropdownlist ID="Dropdownlist1" runat="server" DataSourceID="XmlDataSource2"   DataTextField="Name" DataValueField="ID">
</asp:Dropdownlist>

<asp:XmlDataSource ID="XmlDataSource2" runat="server" DataFile="~/XML/Zodiac.xml"></asp:XmlDataSource>

protected void Page_Load(object sender, EventArgs e)
{
        if (!IsPostBack)
        {
              int memberId = StringHelper.GetIntParam(this.Request, "memberId", 0);
              if (memberId != 0)
              {
                    Member member = Common.ObjectFactory.GetObject<Member>(memberId.ToString());
                    this.GetMembers(member);
              }
         }
}

private void GetMembers(Member member)
{
        if (member != null)
        {
             string s = member.Zodiac.Value.ToString().Trim();
             //输出s看看值多少?
             Response.Write(s);
             Dropdownlist1.Items.FindByText(s).Selected = true; 
        }
    }