2012-01-28 102 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; 
    } 
相关问题