I have a web application consisting of an aspx-file.
On page load two textboxes are filled with data (a "username" and a "password"). This works.
On a button click it should save the textboxes' text. But for some reason the text of the textboxes isn't updated if I have changed it manually meanwhile (by typing in some letters with my keyboard).
Why is that? And how can I tell my program to regard my changes?
My code is:
protected void Page_Load(object sender, EventArgs e)
{
CredentialsManager cm = new CredentialsManager();
TextBox_Benutzername.Text = cm.Username;
TextBox_Passwort.Text = cm.Password;
}
protected void Button_Speichern_Click(object sender, EventArgs e)
{
CredentialsManager cm = new CredentialsManager();
cm.setCredentials(TextBox_Benutzername.Text, TextBox_Passwort.Text);
}
EDIT:
It works with this improvement:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
CredentialsManager cm = new CredentialsManager();
TextBox_Benutzername.Text = cm.Username;
TextBox_Passwort.Text = cm.Password;
}
}
For further information, see answers below. Thanks everyone!
Try checking for a postback -
private void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CredentialsManager cm = new CredentialsManager();
TextBox_Benutzername.Text = cm.Username;
TextBox_Passwort.Text = cm.Password;
}
}
Your Page_Load code will currently run after every button click (or postback), and overwrite the values you have manually added.
Try this,
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack){
CredentialsManager cm = new CredentialsManager();
TextBox_Benutzername.Text = cm.Username;
TextBox_Passwort.Text = cm.Password;
}
}
You are assiging the value to the textboxes on every page load instead of firt page load.
Change the Page_Load method to :
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
CredentialsManager cm = new CredentialsManager();
TextBox_Benutzername.Text = cm.Username;
TextBox_Passwort.Text = cm.Password;
}
}
I think the problem is that you are creating a new CredentialsManager each and every time that the page is loaded (I assume that a new CredentialsManager has an empty Username and Password fields). You should only do that on new page loads, and not when the page is refreshed because of a button click. That is determined with the Page.IsPostBack property, so you moght need to do:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CredentialsManager cm = new CredentialsManager();
TextBox_Benutzername.Text = cm.Username;
TextBox_Passwort.Text = cm.Password;
}
}
Related
I have a Repeater which has it's data set up on Page_Load
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
myRepeater.DataSource = data;
myRepeater.DataBind();
}
}
When I come to save my data on postback, myRepeater.Items contains zero elements when I know there are several.
protected void btnSave_Click(object sender, EventArgs e)
{
....
// why does this contain zero elements>
foreach (RepeaterItem item in myRepeater.Items)
{
Any suggestions as to what may be the issue?
In this case, binding the repeater even on postback fixed the issue
protected void Page_Load(object sender, EventArgs e)
{
myRepeater.DataSource = data;
myRepeater.DataBind();
}
I set up a session variable on the Booking form.aspx as so:
protected void confirmImageButton_Click(object sender, ImageClickEventArgs e)
{
Session["confirmBooking"] = "confirm";
Session["beachBach"] = beachBachRadioButtonList.SelectedItem.Text;
}
and I transfer to my other page as so:
protected void Page_Load(object sender, EventArgs e)
{
{
if (Session["beachBach"] != null)
{
numberOfBeachBookingInteger += 1;
beachBachLabel.Text = numberOfBeachBookingInteger.ToString();
}
I'm trying to add 1 to the beachBach session variable whenever user press the confirm button....however, when i start to debug it, instead of adding 1, it add 2 to the label.
Can someone please help me out.. thanks
try this
protected void Page_Load(object sender, EventArgs e) {
{
if(!IsPostBack)
{
if (Session["beachBach"] != null)
{
numberOfBeachBookingInteger += 1;
beachBachLabel.Text = numberOfBeachBookingInteger.ToString();
}
}
protected void Page_Load(object sender, EventArgs e)
{
{
if (Session["beachBach"] != null)
{
numberOfBeachBookingInteger += 1;
beachBachLabel.Text = numberOfBeachBookingInteger.ToString();
}
Well what happens after we come to this page ?? Do we have any event that causes postback in this page??
If there is than definitely it will add 1 again to your session .
Try using !IsPostBack property .
On clicking Go button the name given in textbox should be displayed as a link label and this should increment dynamically.
Here my code:
private void buttongo_Click(object sender, EventArgs e)
{
linkLabelName.Text = textBoxName.Text;
}
private void btnGo_Click(object sender, EventArgs e)
{
LinkLabel link = new LinkLabel();
link.Text = txtText.Text;
panTable.Controls.Add(link);
}
I have a asp.net page with a datalist with a textbox and a button on it, on page load the textbox gets text in it, if I change the text and press the button the text doesn't get updated.
What am I doing wrong?
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable table = CategoryAccess.GetProducts();
ProductList.DataSource = table;
ProductList.DataBind();
}
}
protected void btn_Click(object sender, EventArgs e)
{
string Name = textbox.Text;
CategoryAccess.UpdateProducts(Name);
}
}
I had same problem. I found that I put textbox.text = "xxx" in Page_Load() but outside if(!ispostback).
Try to add EnableViewState property in your textBox control and set the value to true.
e.g.
<asp:TextBox ID="textBox1"
EnableViewState="true"
MaxLength="25"
runat="server"/>
or you can do it programatically:
protected void Page_Load(object sender, EventArgs e)
{
textBox1.EnableViewState = true;
}
You need to bing again the new data...
protected void btn_Click(object sender, EventArgs e)
{
string Name = textbox.Text;
// you update with the new parametre
CategoryAccess.UpdateProducts(Name);
// you get the new data
DataTable table = CategoryAccess.GetProducts();
// and show it
ProductList.DataSource = table;
ProductList.DataBind();
}
Help me ..my page index is not working in visual studio.
my page load is as follows:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
CustomerView.DataSource = Customer.GetAll();
CustomerView.DataBind();
}
}
protected void CustomerView_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
{
int newPageNumber = e.NewPageIndex + 1;
CustomerView.DataSource = Customer.GetAll();
CustomerView.DataBind();
}
what am i doing wrong my page index in not working.
Try this. I think you have to set the GridView's PageIndex property manually.
protected void CustomerView_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
{
CustomerView.PageIndex = e.NewPageIndex;
CustomerView.DataSource = Customer.GetAll();
CustomerView.DataBind();
}