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.
Related
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;
}
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;
}
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();
}
I'm working on a Web browser in Visual Studio 2010, but I can't update the tab's name to the website's name. For example, when you visit a website like CNN.Com, I want the tab to also say, "cnn.com". The project isn't using the default WebBrowser form, by the way. Please explain it in the simplest way possible since I'm new to C#(Just moved from C++ and Java) so I'm not familiar with working with Windows forms. Thanks. Any help is appreciated.
Here's an image of the problem: http://postimage.org/image/5ym4yx0pt/
....
public Form1()
{
InitializeComponent();
}
int i = 1;
private void Form1_Load(object sender, EventArgs e)
{
WebBrowser Browse = new WebBrowser();
//Load a tab when loading form
tabControl1.TabPages.Add("Tab");//problem
tabControl1.SelectTab(i - 1);
Browse.Name = "Lithium Browser";
Browse.Dock = DockStyle.Fill;
tabControl1.SelectedTab.Controls.Add(Browse);
i++;
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("www.google.com");
}
private void button1_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate(textBox1.Text);
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
WebBrowser Browse = new WebBrowser();
tabControl1.TabPages.Add("Tab"); //problem
tabControl1.SelectTab(i - 1);
Browse.Name = "Lithium Browser";
Browse.Dock = DockStyle.Fill;
tabControl1.SelectedTab.Controls.Add(Browse);
i++;
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
tabControl1.TabPages.RemoveAt(tabControl1.SelectedIndex);
tabControl1.SelectTab(tabControl1.TabPages.Count - 1);
i = i- 1;
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoBack();
}
private void toolStripButton4_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoForward();
}
private void toolStripButton5_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoHome();
}
private void toolStripButton6_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Refresh();
}
private void toolStripButton7_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Stop();
}
private void yahooSearchToolStripMenuItem_Click(object sender, EventArgs e)
{
toolStripDropDownButton1.Text = yahooSearchToolStripMenuItem.Text;
}
private void youtubeSearchToolStripMenuItem_Click(object sender, EventArgs e)
{
toolStripDropDownButton1.Text = youtubeSearchToolStripMenuItem.Text;
}
private void googleSearchToolStripMenuItem_Click(object sender, EventArgs e)
{
toolStripDropDownButton1.Text = googleSearchToolStripMenuItem.Text;
}
private void toolStripButton8_Click(object sender, EventArgs e)
{
if (toolStripDropDownButton1.Text == googleSearchToolStripMenuItem.Text)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://www.google.com/search?q=" + toolStripTextBox1.Text);
}
if (toolStripDropDownButton1.Text == yahooSearchToolStripMenuItem.Text)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://search.yahoo.com/search?p=" + toolStripTextBox1.Text);
}
if (toolStripDropDownButton1.Text == youtubeSearchToolStripMenuItem.Text)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://www.youtube.com/results?search_query=" + toolStripTextBox1.Text);
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
//add KeyUp event for detecting 'Enter' key
//navigate to specified URL withoud pressing the 'Go' button
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate(textBox1.Text);
}
}
private void toolStripTextBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (toolStripDropDownButton1.Text == googleSearchToolStripMenuItem.Text)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://www.google.com/search?q=" + toolStripTextBox1.Text);
}
if (toolStripDropDownButton1.Text == yahooSearchToolStripMenuItem.Text)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://search.yahoo.com/search?p=" + toolStripTextBox1.Text);
}
if (toolStripDropDownButton1.Text == youtubeSearchToolStripMenuItem.Text)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://www.youtube.com/results?search_query=" + toolStripTextBox1.Text);
}
if (toolStripDropDownButton1.Text == youtubeSearchToolStripMenuItem.Text)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://en.wikipedia.org/wiki/" + toolStripTextBox1.Text);
}
if (toolStripDropDownButton1.Text == youtubeSearchToolStripMenuItem.Text)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://en.wikipedia.org/wiki/" + toolStripTextBox1.Text);
}
}
}
private void newTabToolStripMenuItem_Click(object sender, EventArgs e)
{
WebBrowser Browse = new WebBrowser();
tabControl1.TabPages.Add("Tab");
tabControl1.SelectTab(i - 1);
Browse.Name = "Lithium Browser";
Browse.Dock = DockStyle.Fill;
tabControl1.SelectedTab.Controls.Add(Browse);
i++;
}
private void closeTabToolStripMenuItem_Click(object sender, EventArgs e)
{
tabControl1.TabPages.RemoveAt(tabControl1.SelectedIndex);
tabControl1.SelectTab(tabControl1.TabPages.Count - 1);
i = i - 1;
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
}
private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
PrintDialog printDialog = new PrintDialog();
printDialog.ShowDialog();
}
private void printPreviewDialog1_Load(object sender, EventArgs e)
{
}
private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
{
//Associate PrintPreviewDialog with PrintDocument.
printPreviewDialog1.Document = printDocument1;
// Show PrintPreview Dialog
printPreviewDialog1.ShowDialog();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Exit?", "Exit", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
Application.Exit();
}
}
// Bring up 'Print Dialog'
private void pageSetupToolStripMenuItem_Click(object sender, EventArgs e)
{
PageSetupDialog pageSetup = new PageSetupDialog();
pageSetup.PrinterSettings = new System.Drawing.Printing.PrinterSettings();
pageSetup.PageSettings = new System.Drawing.Printing.PageSettings();
pageSetup.EnableMetric = false;
pageSetup.ShowDialog();
}
private void stopToolStripMenuItem_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Stop();
}
private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Refresh();
}
private void homeToolStripMenuItem_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoHome();
}
private void previousPageToolStripMenuItem_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoBack();
}
private void nextPageToolStripMenuItem_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoForward();
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 about = new Form2();
about.Show();
}
private void calenderToolStripMenuItem_Click(object sender, EventArgs e)
{
calenForm cal = new calenForm();
cal.Show();
}
}
}
...........
Assuming WebBrowser is the built-in WebBrowser, you can fire the OnDocumentTitleChanged event to change the tab text every time the WebBrowser document title is changed.
to do this, in the form load event, after declaring browse, start typing browse.DocumentTitleChanged += and a tooltip should come up saying 'tab to insert this code' or something along those lines. Just tab twice and Visual Studio will insert a new method for you, with a throw new NotImplementedException(); line. Delete that line and replace it with the code changing your tab's text to the browser's DocumentTitle.
If you need any more information, I suggest you check the documentation:
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.aspx
Though I am confident that using events is the best solution. Events are designed to execute upon certain significant programming 'events' happening, and changing a webpage is one example of such an event. (Events are roughly C#'s equivalent of C++'s function pointers if that helps your understanding at all. Though they are more akin to a std::vector of function pointers.)
Set the HTML title tag for the page text contained within the two tags will show up in the tab that the web page is displayed in.
See the following for more about setting the title in the code behind
How to use Eval in codebehind to set Page.Title
And this link as well
http://www.asprobot.com/ASP.NET/ASPNET-Title-Tag-and-Meta-Tags
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.