2016-05-12 33 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

相關問題