2016-05-12 54 views
0

我有一个有两个Web窗体的asp.net项目。我想将listbox3(这是主页)的所有项目传递给统计页面中的文本框。我尝试下面的代码,但没有工作。如何将列表框的所有项目传递到另一个页面中的文本框? ASP.NET

主页:

protected void Button5_Click(object sender, EventArgs e) 
     { 
      Response.Redirect("Statistics.aspx?ListBox3"); 
     } 

统计数据页

public partial class WebForm1 : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      TextBox3.Text = ListBox3.Items; 

     } 
    } 
+0

我想你想枚举的项目,像'foreach(var ListBox3.Items中的项目)TextBox3.Text = TextBox3 ==“”? TextBox3.Text + item.ToString():TextBox3.Text +“,”+ item.ToString();' –

回答

0

您不能引用这是不符合其ID当前页面的所有控件。 因此,它可能需要保留的项目从ListBox在Session或将其穿过QueryString

首页

protected void Button5_Click(object sender, EventArgs e) 
{ 
    string Items = string.Empty; 
    foreach(var item in ListBox3.Items) 
    { 
     Items += item + ","; 
    } 
    //Session["ListBox3"] = Items; 
    Response.Redirect("Statistics.aspx?ListBox3=" + Items); 
} 

统计数据页

protected void Page_Load(object sender, EventArgs e) 
{ 
    TextBox3.Text = Request.QueryString["ListBox3"].ToString(); 
    //TextBox3.Text = Session["ListBox3"].ToString(); 
} 
+0

感谢您的回答,我收到此错误“重定向URI不能包含换行符。对于这一行“Response.Redirect(”Statistics.aspx?ListBox3 =“+ Items);” – dpointttt

+0

我其实不知道什么都是你的列表框中的项目 –

+0

更好的你去'Session'内容,因为它不是很好的做法,通过长文本中'QueryString'并没有为'QueryString'内容 –

0
protected void Button5_Click(object sender, EventArgs e) 
{ 
    string allItems; 
    for(int i = 0; i < ListBox3.Items.Count; i++) 
     allItems += ListBox3.Items[i].ToString() + "--"; 
    Response.Redirect("Statistics.aspx?ListBox3=" + allItems.SubString(0, allItems.Length - 2)); 
} 

访问它使用

public partial class WebForm1 : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     TextBox3.Text = Request.QueryString["ListBox3"]; 
    } 
} 
+0

和限制统计页面应该像这样使用protected void Page_Load(object sender,EventArgs e) TextBox3.Text = allItems.Text;在第一 } – dpointttt

+0

@dpointttt更新回答 – Imad

+0

没有工作然后我已经改变,像这样INT米= allItems.Length串行 - 2; string mmm = allItems.Substring(0,m); Response.Redirect(“Statistics.aspx?ListBox3 =”+ mmm);和现在的工作,我怎么过刚开列表框的第一要素不是全部 – dpointttt

相关问题