string str1 = :"class1=7&class2=52&info[adn]=TextBox1.Text&info[abst]=TextBox2.Text&info[touch]=TextBox3.Text&info[region][01]=01&agree=1";string str2 = str1.replace("TextBox","\" + TextBox1.Text").replace("Text&","Text + \"&");或者用正则表达式来替换

解决方案 »

  1.   

    String Replace(String str){
    Regex r = new Regex("(TextBox)[0-9]+");
    MatchCollection mc = r.Matches(str);
    for (int i = 0; i < mc.Count; i++) {
    Match m = mc[i];
    if (m.Success) {
    String val = m.Value;
    TextBox tb = (TextBox) FindControl(val);
    if (tb != null) {
    str = str.Replace(val + ".Text", tb.Text);
    }
    }
    }
    return str;
    }
      

  2.   

    然后调用:
    <%Response.Write(Replace("class1=7&class2=52&info[adn]=TextBox1.Text&info[abst]=TextBox2.Text&info[touch]=TextBox3.Text&info[region][01]=01&agree=1"));%>
      

  3.   

    问题是最后得到的还是字符串呀,我是想获取TextBox1.Text这里面的值,然后加上其它的字符串
      

  4.   

    是取到TextBox1.Text这里面的值了呀,因为这个属性的类型是字符串,所以最后的结果还是字符串。而且我测试了,只要你在这个页面定义了这些TextBox,这种方法肯定可以取到这些文本框的文本。绝对不会不错出错。除非你的本意并不是取文本框的文本,而是别的。
      

  5.   

    只有一个问题...  TextBox1.Text 这些名字不是规律性的,而是如234.Text ddd.Text 都是不规律的
      

  6.   

    还有,我这个代码是在winForm里用的,FindControl()不行
      

  7.   

    用Reflection来解决:
    要引用System.Text.RegularExpressions,System.ReflectionString Replace(String str){
    Type t = GetType();
    PropertyInfo text = typeof(TextBox).GetProperty("Text");
    Regex r = new Regex("[=]\w+[.](Text)");
    MatchCollection mc = r.Matches(str);
    for (int i = 0; i < mc.Count; i++) {
    Match m = mc[i];
    if (m.Success) {
    String val = m.Value;
    if (val.Length > 6) {
    String fieldname = val.Substring(1, val.Length - 6);
    FieldInfo fi = t.GetField(fieldname, BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.Public|BindingFlags.Static);
    if (fi != null) {
    String pvalue = text.GetValue(fi.GetValue(this), null);
    if (pvalue != null) {
    str = str.Replace(str, "=" + pvalue);
    }
    }
    }
    }
    }
      

  8.   

    有些小错误:
    String Replace(String str)
    {
    Type t = GetType();
    PropertyInfo text = typeof(TextBox).GetProperty("Text");
    Regex r = new Regex("[=]\\w+[.](Text)");
    MatchCollection mc = r.Matches(str);
    for (int i = 0; i < mc.Count; i++) 
    {
    Match m = mc[i];
    if (!m.Success) continue;
    String val = m.Value;
    if (val.Length <= 6) continue;
    String fieldname = val.Substring(1, val.Length - 6);
    FieldInfo fi = t.GetField(fieldname, BindingFlags.NonPublic
    |BindingFlags.Instance|BindingFlags.Public
    |BindingFlags.Static|BindingFlags.IgnoreCase);
    if (fi == null) continue;
    String pvalue = (String) text.GetValue(fi.GetValue(this), null);
    if (pvalue != null) 
    {
    str = str.Replace(val, "=" + pvalue);
    }
    }
    return str;
    }
    经过测试完全可以。
      

  9.   

    如果除了TextBox还有别的类型,那就用:
    PropertyInfo text = typeof(Control).GetProperty("Text");
      

  10.   

    PropertyInfo text = typeof(Control).GetProperty("Text");
    这句有问题,提示:“Control”是不明确的引用
    换成TextBox 也不行。