global variable giving incorrect output - c#

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;
}

Related

How to make string accessible to a button?

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();
}

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.

Increment the int value in the text box

I have a disabled text box thats' value only increments by one once a button is clicked, the problem is that it goes from 1 to 2 and thats it. I want it to increment everytime i push the button.
namespace StudentSurveySystem
{
public partial class AddQuestions : System.Web.UI.Page
{
int num = 1;
protected void Page_Load(object sender, EventArgs e)
{
QnoTextBox.Text = num.ToString();
}
protected void ASPxButton1_Click(object sender, EventArgs e)
{
num += 1;
QnoTextBox.Text = num.ToString();
}
}
}
Postback intializes the variable num to 1 again and you do not get the expected increamented result, you better you textbox value and store the value in ViewState.
protected void ASPxButton1_Click(object sender, EventArgs e)
{
num = int.Parse(QnoTextBox.Text);
num++;
QnoTextBox.Text = num.ToString();
}
Using ViewState
public partial class AddQuestions : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
ViewState["Num"] = "1";
}
protected void ASPxButton1_Click(object sender, EventArgs e)
{
QnoTextBox.Text = ViewState["Num"].ToString();
int num = int.Parse(ViewState["Num"].ToString());
ViewState["Num"] = num++;
}
}

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.

how to get variable from form load to action?

private void showCategory_Load(object sender, EventArgs e)
{
string categoryId = "100"
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = categoryId;
}
how to pass string categoryId from form load to button click?
Thank you.
If it's Windows Forms application than using the variable on the class level, not in the function
public partial class showCategory : Form
{
string categoryId = null;
private void showCategory_Load(object sender, EventArgs e)
{
this.categoryId = "100";
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = this.categoryId;
}
}
If it's Web Application - store the value the some state callection: ViewState, Session, HiddenField and so on..

Categories

Resources