How to store record using ViewState - c#

Now there are 2 buttons, let's call them "Button1" and "Button2" respectively.
If I click "Button1", store "1" in ViewState["Record"]; also, if I click "Button2",
store "2" in ViewState["Record"], etc.
When I click Button1⟶Button1⟶Button2⟶Button2, my expected result is: in ViewState["Record"], there are a list of records, like
[1,1,2,2].
Here is my click event code:
protected void Button1_Click(object sender, EventArgs e)
{
ViewState.Add("Record", "1");
}
protected void Button2_Click(object sender, EventArgs e)
{
ViewState.Add("Record", "2");
}
//show the viewstate result
protected void ShowResult(object sender, EventArgs e)
{
for (int i = 1; i <= 4; i++)
{
Label.Text += ViewState["Record"];
}
}
It shows "2222" instead of "1122".
How can I code to resolve it?

When you add Record key a value actually it not adds, just puts a value Record key. You always see last added value on your loop.
You should add a List to ViewState and add value to list.
Add this property
protected List<long> ViewStateList
{
get{ if(ViewState["Record"] == null) ViewState["Record"] = new List<long>();
return (List<long>)ViewState["Record"] }
}
And use like
protected void Button1_Click(object sender, EventArgs e)
{
ViewStateList.Add(1);
}
Loop
protected void ShowResult(object sender, EventArgs e)
{
foreach(long item in ViewStateList)
{
Label.Text += item.ToString();
}
}

Related

How to update label box from selection of listbox using math

So I have 2 list boxes within my form. Listbox1 contains different types of items that have a price and Listbox2 contains how much of that item you want to purchase. How do I update my price label so when I select both options from each list box it updates the label and gives me a price. Here's an example to help you better understand.
I select the $1.50 Chocolate Chip Cookie item in my ListBox1 and in ListBox2 I select the 1 Dozen Cookie item. So I would want my priceLabel to update to $18.00. How would I do this?
As of now I have tried creating some code in the listBox1_SelectedIndexChanged method but I am returned these 3 following values... $0.00...$2.00...$4.00
Here's my code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
double index = listBox1.SelectedIndex;
double index2 = listBox2.SelectedIndex;
double total = index * index2;
label9.Text = total.ToString("C");
}
private void label5_Click(object sender, EventArgs e)
{
}
private void label9_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
{
const int ESTIMATED_ARRIVAL = 3;
label10.Text = monthCalendar1.SelectionStart.AddDays(ESTIMATED_ARRIVAL).ToShortDateString();
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
The problem in your code is that the ".SelectedIndex" property only returns a 0-index position of the currently selected item in the listbox. That's why the calculation goes wrong.
I would like to suggest the use of the ".SelectedItem" property. Because it returns the selected item itself instead of it's postition. So if your items contain a double value, then the calculation will succeed.

A way to add int value to textbox with a button click and then subtract from the int value with another button

I'm trying to have a button click event add an int value to a textbox and then have another button click event subtract from the same textbox that I just added the int value to. The problem I'm having is that the subtract button enters a negative(-1) value to the textbox and if I click on the add button again it goes right back to the int value I had before clicking the subtract button. I just want the add button to add and the subtract button to subtract the in value. I'm very new to c# and I've tried a everything I've found online and I'm losing faith.
I've tried if statements, and some other methods I found online and nothing works.
This is the code I have now. The add button works fine, but the subtract button doesn't do what I want.
private int a = 0;
private void btnAdd_Click(object sender, EventArgs e)
{
a++;
txtSummary.Text = a.ToString();
}
private void comboBoxValues_SelectedIndexChanged(object sender, EventArgs e)
{
}
private int b = -0;
private void btnSubtract_Click(object sender, EventArgs e)
{
b--;
txtSummary.Text = b.ToString();
}
This will convert whatever is in your TextBox to an Integer, then add or subtract 1 to that value:
private void Form1_Load(object sender, EventArgs e)
{
txtSummary.Text = "0";
}
private void btnAdd_Click(object sender, EventArgs e)
{
AddToTextBox(1);
}
private void btnSubtract_Click(object sender, EventArgs e)
{
AddToTextBox(-1);
}
private void AddToTextBox(int changeBy)
{
int value;
if(int.TryParse(txtSummary.Text, out value))
{
value = value + changeBy;
txtSummary.Text = value.ToString();
}
else
{
MessageBox.Show("Invalid Integer in TextBox!");
}
}

Load WinForm with previous ComboBox Value

I have three ComboBoxs get there value from folder and subfolder. when I close the WinForm
and run it again I have to set ComboBoxs value again..
What I need to do is Save the previous selection of ComboBoxs
private void Form1_Load(object sender, EventArgs e)
{
if (Directory.Exists(rootDirectory))
{
comboBox1.DataSource = Directory.GetDirectories(rootDirectory).Select(Path.GetFileName).ToList();
comboBox1.SelectedIndexChanged += comboBox1_SelectedValueChanged;
comboBox2.SelectedIndexChanged += comboBox2_SelectedIndexChanged;
comboBox3.SelectedIndexChanged += comboBox3_SelectedIndexChanged;
comboBox1.Enter += comboBox1_Enter;
}
else
{
MessageBox.Show("Cannot find folder!!! ");
}
}
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
var parentDir = Path.Combine(rootDirectory, comboBox1.SelectedItem.ToString());
comboBox2.DataSource = Directory.GetDirectories(parentDir).Select(Path.GetFileName).ToList();
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
var parentDir = Path.Combine(rootDirectory, comboBox1.SelectedItem.ToString(), comboBox2.SelectedItem.ToString());
comboBox3.DataSource = Directory.GetDirectories(parentDir).Select(Path.GetFileName).ToList();
}
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
var parentDir = Path.Combine(rootDirectory, comboBox1.SelectedItem.ToString(), comboBox2.SelectedItem.ToString(), comboBox3.SelectedItem.ToString());
}
1 - select the combobox
2 - go to Properties > Data > (ApplicationSettings)
3 - add application settings binding to Text property
4 - on FormClosed event save application settings
Saving settings:
private void Form_FormClosed(object sender, FormClosedEventArgs e)
{
Settings.Default.Save();
}
Credits here!
First this fiction is incorrect for listing directories, sub directories and files. Bad idea to use dropdown(s).
Use "TreeView" component in win form.
Example : https://www.c-sharpcorner.com/article/display-sub-directories-and-files-in-treeview/
So, you can save/set only the selected value in treeview.
Note : You can also review the recursive functions.

how to get value of a selected Item in drop down list?

I need to select values from selected item in a drop down list in asp.net. in this code:
protected void EducationFeildsList_SelectedIndexChanged(object sender, EventArgs e)
{
int index = Convert.ToInt32(EducationFeildsList.SelectedIndex);
Label1.Text = index.ToString(CultureInfo.InvariantCulture);
}
But it seems that the value could not be read and so label1.text wasn't changed. how I could get the correct value of a selected item in this situation?
protected void EducationFeildsList_SelectedIndexChanged(object sender, EventArgs e)
{
If (!IsPostback)
{
Label1.Text = Dropdownlist1.Selectedvalue;
}
}
Set AutoPosback prperty of DDL to TRUE
Use the Parse
protected void EducationFeildsList_SelectedIndexChanged(object sender, EventArgs e)
{
int index = int.Parse(EducationFeildsList.SelectedIndex);
Label1.Text = index.ToString(CultureInfo.InvariantCulture);
}

dropdown value selecting only first value

In the below C# code even though I am choosing my own year but still the value is passed as 1920 only. I can see all the values being displayed in the dropdown box but when I choose a value and submit it only 1920 is being passed to the database.
protected void Page_Load(object sender, EventArgs e)
{
DropDownList3.Items.Clear();
for (int i = 1920; i <= 2000; i++)
{
DropDownList3.Items.Add(i.ToString());
}
}
protected void Button1_Click(object sender, EventArgs e)
{
sbtc.dex(DropDownList3.SelectedItem);
}
Can anyone tell me where did I do mistake?
Because when you click on the button it is again executing the code in your Page_Load event. Solution is to set the dropdown values only once. You can use Page.IsPostBack property to check whether this is the initial load or a postback.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DropDownList3.Items.Clear();
for (int i = 1920; i <= 2000; i++)
{
DropDownList3.Items.Add(i.ToString());
}
}
}
Try like this;
if(!IsPostBack)
[
for (int i = 1920; i <= 2000; i++)
{
DropDownList3.Items.Add(i.ToString());
}
}
Try also setting a value for the selected items like this, make sure to check for postbacks
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DropDownList3.Items.Clear();
for (int i = 1920; i <= 2000; i++)
{
DropDownList3.Items.Add( new ListItem(i.ToString(), i.ToString()));
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
sbtc.dex(DropDownList3.SelectedItem);
}

Categories