How to make string accessible to a button? - c#

I need to save the sting i get from selecting a row to be used in a button protected void.
protected void Button2_Click(object sender, EventArgs e)
{
string = id <= how to get the string here?
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
string id = row.Cells[3].Text;
}

You can move the code that gets the id to a property, e.g.:
protected void Button2_Click(object sender, EventArgs e)
{
string id = SelectedId;
}
private string SelectedId
{
get
{
GridViewRow row = GridView1.SelectedRow;
return row?.Cells[3].Text; // Please note the check against null (row?)
}
}

The simplest would be:
Set Button2 to be disabled at start and...
protected void Button2_Click(object sender, EventArgs e)
{
string id = GridView1.SelectedRow.Cells[3].Text;
}
// only allow the button to be enabled if there is a selected row
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (GridView1.SelectedRow != null){
Button2.Enabled = true;
} else {
Button2.Enabled = false;
}
}

Instead of initializing String inside the function, Use session, it'll store your value and you can access it anywhere in the code.
protected void Button2_Click(object sender, EventArgs e)
{
lblanything.Text = Session["id"].ToString();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
Session["id"] = row.Cells[3].Text;
}
Hope it'll resolve your issue.

You can save the string value in a Viewstate and then try to access the value.
//Setting the ViewState
protected void MyGridView_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = MyGridView.SelectedRow;
ViewState["id"] = row.Cells[3].Text;
}
//Assigning the ViewState
protected void MyButton_Click(object sender, EventArgs e)
{
lable1.Text = ViewState["id"].ToString();
}

Related

global variable giving incorrect output

I have declared a variable globally and it is getting its value from page load function but the variable is not returning the value correctly in the button clicked function.
string id = "1";
protected void Page_Load(object sender, EventArgs e)
{
id = "3";
}
protected void Button1_Click(object sender, EventArgs e)
{
label.text = id;
}
Output
label.text = "1"
why is it so?
well when you click postback runs and assigns value to 3, so you will get only 3 unless you do the page load assignment when it is not postback
string id = "1";
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
id = "3";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
label.text = id;
}

if statements in C# for a textbox to change back to normal when i press a button for the second time

This is the code that i used to change the text in the text box from "Livre" to "Ocupado"
What code should i use to change it from "Ocupado" to "Livre"
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text="Ocupado";
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "Livre")
{
textBox1.Text = "Ocupado";
}
else
{
textBox1.Text = "Livre";
}
}
you can add a variable to your class
e.g.:
bool livre = true;
private void button1_Click(object sender, EventArgs e)
{
if (livre)
{
textBox1.Text="Ocupado";
}
else
{
textBox1.Text="Livre";
}
livre = !livre;
}

c# display the value to listview if 3 button is clicked

can someone help me on my problem I'm making an ordering system for our thesis and I don't know what to do next if 3 button is clicked
Things I want to happen:
user need to pick an item first then he/she will pick a quantity for the item(I have 10 buttons for quantity: 1,2,3,4,5,6,7,8,9,0)
so I add a button0 so the user need to pick item and then click button1 then button0 to have a quantity of 10 that will display in my listview
1-9 button is working now but my problem is what if the user wants the item for a quantity of 10 or more
so here I have my code for an item button1
private void button1_Click(object sender, EventArgs e)
{
ms = 1;
}
and the quantity button
private void button1_Click(object sender, EventArgs e)
{
if(ms == 1)
{
ListViewItem item = new ListViewItem(btnms1.Text);
item.SubItems.Add("1");
item.SubItems.Add("118");
listView1.Items.Add(item);
ms = 0;
}
}
private void button2_Click(object sender, EventArgs e)
{
//working
}
private void button3_Click(object sender, EventArgs e)
{
//working
}
private void button4_Click(object sender, EventArgs e)
{
//working
}
private void button5_Click(object sender, EventArgs e)
{
//working
}
private void button6_Click(object sender, EventArgs e)
{
//working
}
private void button7_Click(object sender, EventArgs e)
{
//working
}
private void button8_Click(object sender, EventArgs e)
{
//working
}
private void button9_Click(object sender, EventArgs e)
{
//working
}
here is the 0 button
private void button0_Click(object sender, EventArgs e)
{
// magic please
}
I'm not really sure what you want to achieve but it might be something like this.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text += "1";
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text += "2";
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text += "3";
}
private void button4_Click(object sender, EventArgs e)
{
textBox1.Text += "4";
}
private void button5_Click(object sender, EventArgs e)
{
textBox1.Text += "5";
}
private void button6_Click(object sender, EventArgs e)
{
textBox1.Text += "6";
}
private void button7_Click(object sender, EventArgs e)
{
textBox1.Text += "7";
}
private void button8_Click(object sender, EventArgs e)
{
textBox1.Text += "8";
}
private void button9_Click(object sender, EventArgs e)
{
textBox1.Text += "9";
}
private void button10_Click(object sender, EventArgs e)
{
textBox1.Text += "0";
}
private void button11_Click(object sender, EventArgs e)
{
int quantity = 0;
bool quantityParse = int.TryParse(textBox1.Text, out quantity);
string productName = "Product Name"; // Sample Name
double productPrice = 300; // Sample Price
if (quantityParse)
{
ListViewItem lvi = new ListViewItem(productName); // Name
lvi.SubItems.Add(quantity.ToString()); // Quantity
lvi.SubItems.Add(productPrice.ToString()); // Price
lvi.SubItems.Add((productPrice * quantity).ToString()); // Subtotal
listView1.Items.Add(lvi);
}
}
}
Firstly, please start picking more meaningful names for variables as they help with code comprehension, especially when your code gets too complex for you or when you are sharing with others.
Secondly, how you're doing this at the moment is not the most intuitive way to go about things, always aim for the least amount of code as possible.
What I would be doing is calling the same event handler for each of the quantity buttons, but parsing the text in the buttons as a integer and adding that to a total string.
Ie:
private string QuantityText = string.Empty;
private void QtyButton_Click(object sender, EventArgs e)
{
if (sender is Button)
{
Button theButton = (Button)sender;
string qtyText = theButton.Content.ToString();
QuantityText += qtyText;
}
}
Then call something like this method when you want to process the quantity string:
Private Void ProcessQuantity()
{
int qtyAmount = -1;
int.TryParse(QuantityText, out qtyAmount)
if (qtyAmount > -1)
{
//Do Processing here
}
else
{
throw new InvalidArgumentException("Quantity is invalid");
}
}

Session variable can't be updated

Whenever i try to update a session variable that has been previously entered it won't update.
Heres an example on what i'm talking about:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Test"] != null)
{
TextBox1.Text = Session["Test"].ToString();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Session["Test"] = TextBox1.Text;
}
So when i click the button the first time, the textbox will be updated. But when i edit the text and click the button again, the textbox just reverts to what it was the first time i.e doesn't update. Anyone have any ideas?
So when i click the button the first time, the textbox will be
updated. But when i edit the text and click the button again, the
textbox just reverts to what it was the first time
I believe it's because you are doing just exactly as that:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Test"] != null)
{
TextBox1.Text = Session["Test"].ToString();
}
}
In that code you have you should be checking if the page load is caused by a post back (click of a button). So you should be doing this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack && Session["Test"] != null)
{
TextBox1.Text = Session["Test"].ToString();
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["Test"] != null && Session["Test"].ToString().Length > 0)
{
TextBox1.Text = Session["Test"].ToString();
}
}
Session["Test"] = string.Empty;
}
protected void Button1_Click(object sender, EventArgs e)
{
Session["Test"] = TextBox1.Text;
}
This is tested code.
Do it like this
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Session["Test"] = "";
}
if (Session["Test"] != null)
{
Session["Test"] = ASPxTextBox1.Text;
}
}
protected void ASPxButton1_Click(object sender, EventArgs e)
{
ASPxTextBox1.Text = Session["Test"].ToString();
}
You page is posted back thats why it is taking previous value as you have written
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Test"] != null)
{
TextBox1.Text = Session["Test"].ToString();
}
}
int this code the text box text will be restored with the previous value which you entered before,
so your code should be
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
if (Session["Test"] != null)
{
TextBox1.Text = Session["Test"].ToString();
}
}
}
This should work
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["Test"] != null)
{
TextBox1.Text = Session["Test"].ToString();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Session["Test"] = TextBox1.Text;
}
You are getting the Page_Load event before the Button Click, so your Page_Load is overwriting the value of TextBox1.Text with the previous value in the Session. That is why it never changes after the first time it is set.
Check that you are not responding to a post on Page_Load like this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox1.Text = (Session["Test"] ?? "").ToString();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Session["Test"] = TextBox1.Text;
}
With that said, you probably want to avoid using the Session altogether if it can be helped.

How to access the value of a listbox on another page

I have a website in c#, in the first page I have some listbox, I need that
the last user's choice goes to a label in other page, how can I do that?
In my code if
the user chooses a value a button is visible, and in the click event of that button
redirect to to another page, but I need that value in a label in the page2
if (ddlFunciones.SelectedValue.Equals("15"))
{
lblAgregarNuevoServicio.Visible = true;
//lblIdFuncion.Visible = true;
lblDescripcion.Visible = true;
//txtId_funcion.Visible = true;
txtDescripcionFuncion.Visible = true;
btnAgregarNuevaFuncion.Visible = true;
}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void btnVerCargos_Click(object sender, EventArgs e)
{
if (btnVerCargos.Enabled)
{
ListBoxCargo.Visible = true;
}
else
{
ListBoxCargo.Visible = false;
}
}
protected void ListBoxCargo_SelectedIndexChanged(object sender, EventArgs e)
{
}
If this is in Asp.Net you can pass information between pages in a number of ways.
You can use the Session object
protected void ListBoxCargo_SelectedIndexChanged(object sender, EventArgs e)
{
Session["MyVar"] = ListBoxCargo.SelectedValue;
}
and in your other page
object value;
if (Session["MyVar"] != null)
{
value = Session["MyVar"]
}
OR
By passing them in the QueryString see Passing-variables-between-pages-using-QueryString
And using Request.QueryString["MyVar"]
and of course there are more, please explain what exactly are you trying to do...
Edit: Based on OPs comments:
In page1:
protected void Button1_Click(object sender, EventArgs e)
{
Session["Page1Value"] = ListBox3.SelectedItem.Text;
//Response.Redirect("~/Page2.aspx");
}
In Page2:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Page1Value"] != null)
{
Label1.Text = Session["Page1Value"].ToString();
}
}
Before you redirect the user to another page, store the selected value in the user's session.
protected void Button1_Click(object sender, EventArgs e)
{
Session["userSelectedValue"] = ListBox1.SelectedValue;
Response.Redirect("OtherPage.aspx");
}
On the other page just extract the selected value from the session.
For example:
protected void Page_Load(object sender, EventArgs e)
{
var selectedValue = Session["userSelectedValue"];
}
More than enough examples of working with session variables available on the interwebs.
I suggest you read up on ASP.NET Session State.

Categories

Resources