C# 存储基于下拉列表选择的字符串
基本上,我正在制作一个网络表单,你将填写所有的文本框,然后从下拉列表中选择一个类别并点击提交。根据您选择的类别,您应该指定文本框中的数据存储在哪个字符串中。当涉及到C和ASP.NET时,我是一个新手,我的if语句有些不对劲,但我不知道如何正确地执行它们 代码如下:C# 存储基于下拉列表选择的字符串,c#,asp.net,if-statement,store,C#,Asp.net,If Statement,Store,基本上,我正在制作一个网络表单,你将填写所有的文本框,然后从下拉列表中选择一个类别并点击提交。根据您选择的类别,您应该指定文本框中的数据存储在哪个字符串中。当涉及到C和ASP.NET时,我是一个新手,我的if语句有些不对劲,但我不知道如何正确地执行它们 代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using Syste
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;
}
}
}
另外,为了保存另一篇文章,我需要找到一种存储这些内容的方法,这样我就可以调用完整的字符串并将它们添加到另一页上的另一个字符串中
if (Cat_DropDownList.SelectedIndex = 0)
{
fiction = "Title: " + Titletxt.Text + " | " + "Description: " + Descriptiontxt.Text + " | " + "Price: " + Pricetxt.Text + " | " + "Quantity: " + Quantitytxt.Text;
}
=是赋值-您希望与==进行比较-这同样适用于其他if语句。同时使用string.Format将使此语句更具可读性:
if (Cat_DropDownList.SelectedIndex == 0)
{
fiction = string.Format("Title: {0} | Description: {1} | Price: {2} | Quantity: {3}", Titletxt.Text, Descriptiontxt.Text, Pricetxt.Text, Quantitytxt.Text);
}
首先,在if条件中缺少==运算符。您需要使用==运算符进行比较
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;
}
我要宣布
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);
}
如果没有ifs和开关,则在if条件中使用=而不是==是一个语法错误。你没注意到吗?