2012-01-28 159 views
0

基本上我製作了一個網頁表單,您可以在其中填寫所有文本框,然後從下拉菜單中選擇一個類別並點擊提交。根據你選擇的類別應該規定來自文本框的數據存儲在哪個字符串中。當涉及到C#和ASP.NET時,我處於新手級別,並且我的if語句有些問題,但我無法弄清楚如何正確地做到這一點。下面存儲一個基於字符串的下拉列表選擇

代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

public partial class _Default : System.Web.UI.Page 
{ 
string non_fiction; 
string fiction; 
string self_help; 



protected void Page_Load(object sender, EventArgs e) 
{ 


} 


protected void Submit_btn_Click(object sender, EventArgs e) 
{ 
    if (Cat_DropDownList.SelectedIndex = 0) 
    { 
     fiction = "Title: " + Titletxt.Text + " | " + "Description: " + Descriptiontxt.Text + " | " + "Price: " + Pricetxt.Text + " | " + "Quantity: " + Quantitytxt.Text; 
    } 

    if (Cat_DropDownList.SelectedIndex = 1) 
    { 
     non_fiction = "Title: " + Titletxt.Text + " | " + "Description: " + Descriptiontxt.Text + " | " + "Price: " + Pricetxt.Text + " | " + "Quantity: " + Quantitytxt.Text; 

    } 

    if (Cat_DropDownList.SelectedIndex = 2) 
    { 
     self_help = "Title: " + Titletxt.Text + " | " + "Description: " + Descriptiontxt.Text + " | " + "Price: " + Pricetxt.Text + " | " + "Quantity: " + Quantitytxt.Text; 
    } 
} 
} 

也才能拯救他人文章,我需要找出辦法把這些存儲的,所以我可以稱之爲「全面」的字符串,並將其添加到另一個頁面上的其他一個。

+0

採用' ='而不是'=='在'if'條件下是一個語法錯誤。你沒注意到嗎? – Lion 2012-01-28 19:13:21

回答

1

我將宣佈

StringBuilder non_fiction = new StringBuilder(); 
StringBuilder fiction = new StringBuilder(); 
StringBuilder self_help = new StringBuilder(); 

StringBuilder[] strings = null; 

,並把它們作爲

protected void Page_Load(object sender, EventArgs e) 
{ 
    strings = new StringBuilder[] { fiction, non_fiction, self_help }; 
} 

protected void Submit_btn_Click(object sender, EventArgs e) 
{ 
    strings[Cat_DropDownList.SelectedIndex].Append("Title: " + Titletxt.Text + " | " + "Description: " + Descriptiontxt.Text + " | " + "Price: " + Pricetxt.Text + " | " + "Quantity: " + Quantitytxt.Text); 
} 

沒有if S和switch ES

0
if (Cat_DropDownList.SelectedIndex = 0) 
{ 
    fiction = "Title: " + Titletxt.Text + " | " + "Description: " + Descriptiontxt.Text + " | " + "Price: " + Pricetxt.Text + " | " + "Quantity: " + Quantitytxt.Text; 
} 

=是分配 - 你想與==的比較,而不是 - if語句同樣適用於其他。還使用string.Format()將使這種說法更可讀(IMO):

if (Cat_DropDownList.SelectedIndex == 0) 
{ 
    fiction = string.Format("Title: {0} | Description: {1} | Price: {2} | Quantity: {3}", Titletxt.Text, Descriptiontxt.Text, Pricetxt.Text, Quantitytxt.Text); 
} 
1

首先你缺少==操作符在if條件。你需要使用==操作符comaparision

if (Cat_DropDownList.SelectedIndex == 0) 
    { 
     fiction = "Title: " + Titletxt.Text + " | " + "Description: " + Descriptiontxt.Text + " | " + "Price: " + Pricetxt.Text + " | " + "Quantity: " + Quantitytxt.Text; 
    } 



if (Cat_DropDownList.SelectedIndex == 1) 
    { 
     non_fiction = "Title: " + Titletxt.Text + " | " + "Description: " + Descriptiontxt.Text + " | " + "Price: " + Pricetxt.Text + " | " + "Quantity: " + Quantitytxt.Text; 

    } 

    if (Cat_DropDownList.SelectedIndex == 2) 
    { 
     self_help = "Title: " + Titletxt.Text + " | " + "Description: " + Descriptiontxt.Text + " | " + "Price: " + Pricetxt.Text + " | " + "Quantity: " + Quantitytxt.Text; 
    } 
相關問題