Сохранить строку на основе раскрывающегося списка выбора - PullRequest
0 голосов
/ 28 января 2012

В основном я делаю веб-форму, где вы заполните все текстовые поля, а затем выберите категорию в раскрывающемся списке и нажмите «Отправить». В зависимости от того, какую категорию вы выбираете, нужно определять, в какой строке хранятся данные из текстовых полей. Я нахожусь на уровне новичка, когда дело доходит до 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;
    }
}
}

Кроме того, чтобы сохранить еще одну запись, мне нужно найти способ ее сохранения, чтобы я мог вызывать «полные» строки и добавлять их в другую на другой странице.

Ответы [ 3 ]

1 голос
/ 28 января 2012

сначала вам не хватает оператора == в условии 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;
    }
1 голос
/ 28 января 2012

Я бы объявил

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 голосов
/ 28 января 2012
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);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...