Data not loading on pageload with postback - c#

I am having a problem. Data is not loading once the page loads. I have to choose an item on the dropdown list for it to load. I need it to load even before I choose any item on the drop down list.
This is my code behind.
protected void ddlPeriodStamp_SelectedIndexChanged(object sender, System.EventArgs e)
{
string selectedGroup = string.Empty;
DropDownList ddlItemGroup = (DropDownList)sender;
if (ddlItemGroup.SelectedValue != null)
TreatmentGroup = ddlItemGroup.SelectedValue;
ApplyGridFilter(ddlItemGroup.SelectedValue);
}
protected void ApplyGridFilter(string TreatmentGroup)
{
string selectedGroup = string.Empty;
DBDataSource1.State.BusinessObject.DataPump.FormFilters.Clear();
DBDataSource1.State.BusinessObject.DataPump.FormFilters.Add("TreatmentGroup", TreatmentGroup);
DBDataSource1.State.BusinessObject.Fill(null);
MedicalSchemeDetailGrid.DataBind();
}
protected void Page_LoadComplete(object sender, System.EventArgs e)
{
if (!this.IsPostBack)
ApplyGridFilter(string.Empty);
}

Call ApplyGridFilter(string.Empty); while ispostBack is false and call ApplyGridFilter(ddlItemGroup.SelectedValue); while ispostBack is true
protected void Page_LoadComplete(object sender, System.EventArgs e)
{
if (this.IsPostBack)
{
ApplyGridFilter(ddlItemGroup.SelectedValue);// it will hit on first time page load
}
else
{
ApplyGridFilter(string.Empty);// it will hit while you change the dropdown items, But you should set true for **IsAutoPostBack** property on dropsownlist.
}
}

You need to fill your data in Page_Load event not in Page_LoadComplete event. According to MSDN:
The LoadComplete event occurs after all postback data and view-state
data is loaded into the page and after the OnLoad method has been
called for all controls on the page.
protected void Page_Load(object sender, System.EventArgs e)
{
if (!this.IsPostBack)
ApplyGridFilter(string.Empty);
}

Related

How to show login saved in cookie in Page_load event?

I need save login of my Login page in cookie for show every time when page is loaded
I have create for save login in cookie, in Button click event. but, because in the Post Back, Page_Load before button, so when page_load put cookie information in the TextBox, my cookie is empty
What can I do for resolve this?
protected void Page_Load(object sender, EventArgs e)
{
// This time, Cookies["login"].Value is empty
tbLogin.Text = Response.Cookies["login"].Value;
}
protected void Button1_Click1(object sender, EventArgs e)
{
Response.Cookies.Add(new HttpCookie("login", tbLogin.Text));
}
Try using Page.IsPostBack property.
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
tbLogin.Text = Response.Cookies["login"].Value;
}
}
protected void Button1_Click1(object sender, EventArgs e)
{
Response.Cookies.Add(new HttpCookie("login", tbLogin.Text));
}

Why all dropdownlist_SelectedIndexChanged events trigger only after button click?

I have a simple ASP.NET Application. And On one of my pages all dropdownlist_SelectedIndexChanged events trigger only after I click save button.
This is part of my code behidn:
protected void ddlTimekeeperOffice_SelectedIndexChanged(object sender, EventArgs e)
{
CheckUniqueCombination();
}
protected void ddlTkprDepartment_SelectedIndexChanged(object sender, EventArgs e)
{
CheckUniqueCombination();
}
protected void ddlTkprSummaryTitle_SelectedIndexChanged(object sender, EventArgs e)
{
CheckUniqueCombination();
}
this is 3 dropdowns
protected void btnSave_Click(object sender, EventArgs e)
{
trTimekeeperRow.Visible = true;
}
and a save button.
When I select anything in dropdowns > Nothing happens
Then I click save button and before save_click it goes through every ddl_selectedindexchanged.
Why?
Make sure AutoPostBack property of the dropdown controls is true.

Buttonclick and postback issue

Below is a code wherein I want to display a message if there is a postback.
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
Response.Write("Hidden value :" + HiddenField1.Value);
}
}
protected void btn_Click(object sender, EventArgs e)
{
HiddenField1.Value = "test";
}
The issue is I get the message on clicking the button twice, that is clearly because Page_Load happens before btn_Click. Can anyone suggest me a way wherein I can get the message to be displayed in if (Page.IsPostBack) on page postback?
Kindly help
Yep, you're running into a page life cycle issue. Page_Load happens before any postback events.
If you're just doing Response.Write to the resposne stream, you could move this code to the overriden PreRender event on the page.
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (Page.IsPostBack)
{
Response.Write("Hidden value :" + HiddenField1.Value);
}
}

Databinding action cancels selectedindexchanging event

I'm using ListView server control to represent some data. When I fire a Select command, Page does postback properly; but if i bind data source in pageload, selectedindexchanging event is being cancelled. I toggled databinding to comment in pageload and it worked properly.
Here is my databinding method.
public void BindData()
{
lstSamples.DataSource = (Session["AnalyzeApp"] as AnalizBasvurusu).SampleInfos;
lstSamples.DataKeyNames = new string[] {"Key"};
lstSamples.DataBind();
}
And i call it that way
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostback)
BindData();
}
Thanks in advance.
Call BindData() after you handle your events(SelectedIndexChanging..etc):
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostback)
BindData();
}

Modify page_load method for radiobuttonlist

I have a page with radio buttons and a textarea that populates data dynamically based on your selection. The radio buttons act as a list of article titles and on selection you see the content of the article.
Within my pageload method, I want to allow users to be able to see a URL in their browser that points to value they've. That way they can link to the article within another source.
Currently, the method I have allows me to link to the button selection if I manually type in the following example URLs:
http://localhost/test/Articles_test.aspx?selected=1
http://localhost/test/Articles_test.aspx?selected=2
I'd like to modify this so that the URL appears in the browser when a radio button selection is made. Plus, on page load defaults to the "0" index if no value parameter was specified.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int selected;
if (int.TryParse(Request.QueryString["selected"], out selected))
RadioButtonList1.SelectedIndex = selected;
RadioButtonList1.DataBind();
}
}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
string strRedirect;
strRedirect = "frm_Articles.aspx?selected=" + RadioButtonList1.SelectedIndex;
Response.Redirect(strRedirect);
}
Set your radiobutton list to post back on change. Then, in the handler, do a redirect to the appropriate URL:
protected void Page_Load(object sender, EventArgs e)
{
int selected;
if (int.TryParse(Request.QueryString["selected"], out selected))
RadioButtonList1.SelectedIndex = selected;
RadioButtonList1.DataBind();
}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
string strRedirect;
strRedirect = "frm_Articles.aspx?selected=" + RadioButtonList1.SelectedIndex;
Response.Redirect(strRedirect);
}

Categories