Initial selection for a DropDownList overriding user selection - asp.net

I'm trying to set an initial selection for a DropDownList by calling: drop.SelectedIndex = 5 in Page_Load.
This works, but then if I change the selection manually and want to save the form, I'm still getting the initial selection instead of the new selection when calling drop.SelectedValue. What's wrong?

You have forgotten you check if(!IsPostback). Otherwise you will select the 6th item again on postbacks before the SelectedIndexChanged event is triggered (or a button-click event):
protected void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack) // do this only on the initial load and not on postbacks
dropDwonList1.SelectedIndex = 5;
}

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//set up data here
}
}
if (Page.IsPostBack)
{
//do page reload logic in here
}
protected void foo(object sender, EventArgs e)
{
//get your selected value here
}
Try this code

You should be using if(!IsPostback) in the Page_Load function.
protected void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack)
{
drop.SelectedIndex = 5;
//yourcode
}
}
Through this your problem will be solved

Related

asp.net: Create Handles clause for control class

Is there a way that I force all TextBoxes (as an example) .. on an asp.net form, to go through the same routine, during a "postback" event or onchange event?
Here is example code.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
textBox1.TextChanged += new EventHandler(textBox_TextChanged);
textBox2.TextChanged += new EventHandler(textBox_TextChanged);
}
}
protected void textBox_TextChanged(object sender, EventArgs e)
{
}

GridView | drop down lists gets unpopulate

The Drop Down Lists in the FooterTemplate gets unpopulate when clicking "update" on some row
this is the page load event when they gets populate:
protected void Page_Load(object sender, EventArgs e)
{
DropDownList ddlImages_new = ((DropDownList)gvAdminArticleAdd.FooterRow.FindControl("ddlImages_new"));
ddlImages_new.DataSource = GetPdfs();
ddlImages_new.DataBind();
DropDownList ddl_invNamesNew = ((DropDownList)gvAdminArticleAdd.FooterRow.FindControl("ddl_invNamesNew"));
ddl_invNamesNew.DataSource = GetInvestigatorNames();
ddl_invNamesNew.DataBind();
}
If I click the update linkButton on some row the data in the drop down lists are disappear
Even when try to call the page load on cancel event it didn't work.
protected void gvAdminArticleAdd_CancelEditEventHandler(object sender, GridViewCancelEditEventArgs e)
{
Page_Load(sender, e);
}
Bind your controls only when the page is not post back:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList ddlImages_new = ((DropDownList)gvAdminArticleAdd.FooterRow.FindControl("ddlImages_new"));
ddlImages_new.DataSource = GetPdfs();
ddlImages_new.DataBind();
DropDownList ddl_invNamesNew = ((DropDownList)gvAdminArticleAdd.FooterRow.FindControl("ddl_invNamesNew"));
ddl_invNamesNew.DataSource = GetInvestigatorNames();
ddl_invNamesNew.DataBind();
}
}

Update a label in server side in ASP.Net after Button Click

I want to update my label content when the user submits a form but it doesn't get updated. Although I have put it in if (!IsPostBack) condition in form load It doesn't show the changes. The only solution that I came up with was defining a counter and increase it in button_click event and check it before label update in !IsPostBack condition. Which is working fine with that.
Is there any other way to update the label text?
Here is my solution:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (count > 0)
lblSuccessMsg.Text ="A Message!";
count = 0;
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
Save();
count = count + 1;
}
please update your label code outside of !IsPostBack evnt and inside a pageload.
Hard to tell but by the sounds of it you have a submit button and the onclick needs to update this label, something like this should work. I'm using viewstate but session would work here, as would a redirect to the same page with a querystring parameter. Not sure if I've understood your question correctly though.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
if(Viewstate["updateLabel"] == "true")
{
lblYourLabel.Text = "I'm updated now!";
Viewstate["updateLabel"] = "";
}
}
}
protected void btnYourButton_Click(Object sender, Eventargs e)
{
ViewState["updateLabel"] = "true";
//Do other stuff here if you want
}

Drop down selected index changed on page load

I have a drop down box that populates textbox values based on its value. It fires when it is changed but on pageload it doesnt fire. How do I get it to fire on page load?
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox3.Text = DropDownList1.SelectedValue;
TextBox12.Text = DropDownList1.SelectedValue;
TextBox21.Text = DropDownList1.SelectedValue;
//etc
Tim Schmelter's comment is right on the money.
// Wire up to the page load event
protected void Page_Load(object sender, System.EventArgs e)
{
updateTextBoxes();
}
// Wire up to the select index-changed event
protected void DropDownList1_SelectIndexChanged( object sender, EventArgs e )
{
updateTextBoxes();
}
// your workhorse method
protected void updateTextBoxes()
{
TextBox3.Text = DropDownList1.SelectedValue;
TextBox12.Text = DropDownList1.SelectedValue;
TextBox21.Text = DropDownList1.SelectedValue;
// etc.
}
It won't be called automatically at page load, you have to call it "manually":
void Page_Load(object sender, System.EventArgs e) {
// ....
DropDownList1_SelectedIndexChanged(DropDownList1, e);
}
SelectedIndexChanged fires in response to a user-driven change. Move the assignment logic to another method and call it manually from Page_Load and also from your event handler.

ASP.NET Profile

I have this:
protected void Page_Load(object sender, EventArgs e)
{
nome_Txt.Text = Profile.dados_pessoais.nome;
}
protected void save_Click(object sender, EventArgs e)
{
Profile.dados_pessoais.nome = nome_Txt.Text;
}
If Profile.dados_pessoais.nome is empty, nome_txt.Text is empty too. When I change nome_Txt.Text to teste for example, when I click on the button nome_Txt.Text is empty.
What am I doing wrong?
The Page_Load event run before the button click event so you always assign the text box to empty value.
To solve this, don't populate the textbox when you are in a Postback:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
nome_Txt.Text = Profile.dados_pessoais.nome;
}
}
As also stated in a comment, you probably have to save the profile after changing it otherwise it won't be saved when you next load the page:
protected void save_Click(object sender, EventArgs e)
{
Profile.dados_pessoais.nome = nome_Txt.Text;
Profile.Save()
}

Resources