假如我有一组数据
string str="1_小说|2_军事|3_人文|4_地里";
将str里的值绑定到DropDownList得到的结果是这样的,最好用linq 其它的方法也可以<select name="dltypeid" id="dltypeid">
<option value="1">小说</option>
<option value="2">军事</option>
<option value="3">人文</option>
<option value="4">地里</option>
</select>

解决方案 »

  1.   

    字符串分解吗,以 | 分隔,然后再取出 _ 前面数字作为value
      

  2.   

    string str1 = 字段名.Remove(0, 字段名.LastIndexOf(@"_"));楼主在加个循环 添加到dropdownlist就好了!
      

  3.   

    string[] result=str.split('|');
    foreach(var info in result)
    {
    string[] v=info.split('_'); 
    items.Add(v[0],v[1]);
    }
      

  4.   


    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:DropDownList ID="DropDownList1" runat="server">
            </asp:DropDownList>
        </div>
        </form>
    </body>
    </html>
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;
    using System.Collections;public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string str = "1_小说|2_军事|3_人文|4_地里";
            Hashtable ht = GetDataSource(str);
            this.DropDownList1.DataSource = ht;
            this.DropDownList1.DataTextField = "value";
            this.DropDownList1.DataValueField = "key";
            this.DropDownList1.DataBind();
        }
        public Hashtable GetDataSource(string str)
        {
            Hashtable ht = new Hashtable();
            string[] s = str.Split('|');
            foreach (string ss in s)
            {
                ht.Add(int.Parse(ss.Split('_')[0].ToString()), (ss.Split('_')[1]).ToString());
            }
            return ht;
        }
    }
      

  5.   

    String.split("|");
    以|分割 获得一组数组  既1_小说 2_军事 3_人文 4_地里";
    然后你在赋值给DropDownList
    String.split("_");得到的前面的值付给value后面的值付给text 
      

  6.   

    String.split()方法,返回是一个数组
    1、如果用“.”作为分隔的话,必须是如下写法:String.split("\\."),这样才能正确的分隔开,不能用String.split(".");
    2、如果用“|”作为分隔的话,必须是如下写法:String.split("\\|"),这样才能正确的分隔开,不能用String.split("|");
    “.”和“|”都是转义字符,必须得加"\\";
    3、如果在一个字符串中有多个分隔符,可以用“|”作为连字符,比如:“acount=? and uu =? or n=?”,把三个都分隔出来,可以用String.split("and|or");
      

  7.   

    参数regex是一个 regular-expression的匹配模式而不是一个简单的String,他对一些特殊的字符可能会出现你预想不到的结果,比如测试下面的代码:用竖线 | 分隔字符串,你将得不到预期的结果    String[] aa = "aaa|bbb|ccc".split("|");
        //String[] aa = "aaa|bbb|ccc".split("\\|"); 这样才能得到正确的结果    for (int i = 0 ; i <aa.length ; i++ ) {
          System.out.println("--"+aa[i]); 
        } 用竖 * 分隔字符串运行将抛出java.util.regex.PatternSyntaxException异常,用加号 + 也是如此。    String[] aa = "aaa*bbb*ccc".split("*");
        //String[] aa = "aaa|bbb|ccc".split("\\*"); 这样才能得到正确的结果        for (int i = 0 ; i <aa.length ; i++ ) {
          System.out.println("--"+aa[i]); 
        } 显然,+ * 不是有效的模式匹配规则表达式,用"\\*" "\\+"转义后即可得到正确的结果。"|" 分隔串时虽然能够执行,但是却不是预期的目的,"\\|"转义后即可得到正确的结果。还有如果想在串中使用"\"字符,则也需要转义.首先要表达"aaaa\bbbb"这个串就应该用"aaaa\\bbbb",如果要分隔就应该这样才能得到正确结果:String[] aa = "aaa\\bbb\\bccc".split("\\\\");