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();
}
Related
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);
}
I have a panel ViewStock where i am viewing stock in a gridview from database and DataBind() it via code. Allowed paging and created and event "OnPageIndexChanging" in gridview tag in html, Implemented the defined code above and paging in an event as follows:
HTML:
<asp:Panel ID="Panel_StockView" runat="server">
<asp:GridView ID="GridView_Stock" AllowPaging="true" OnPageIndexChanging="GridView_PageIndexChanging" runat="server"></asp:GridView>
</asp:Panel>
Code C#:
protected void LinkButton_Panel_ViewStock_Click(object sender, EventArgs e)
{
using(SqlConnection con = new SqlConnection(cs))
{
//Sql command here
/sql adapter and filled datatable
sdaStockView.Fill(dtStockView);
GridView_Stock.DataSource = dtStockView;
GridView_Stock.DataBind();
}
}
And now the Implemented Paging
protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView_Stock.DataBind();
GridView_Stock.PageIndex = e.NewPageIndex;
}
it does work but partially. It does the paging and does the data correctly. But, the issue is when i click the page '2' the panel blanks out just like in the picture i uploaded See this Image, then i click the link button that redirects me to the panel again and opens the page '2' of the gridview with valid data.
How to resolve this issue?
Extract the logic which binds the GridView to data into a new method.You can call it BindData() for example:
private void BindData()
{
using (SqlConnection con = new SqlConnection(cs))
{
sdastockview.fill(dtstockview);
gridview_stock.datasource = dtstockview;
gridview_stock.databind();
}
}
Call this method inside LinkButton_Panel_ViewStock_Click to populate the GridView:
protected void LinkButton_Panel_ViewStock_Click(object sender, EventArgs e)
{
this.BindData();
}
Lastly, call it again to re-populate the GridView during paging:
protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView_Stock.PageIndex = e.NewPageIndex;
this.BindData();
}
Just make those three little changes and it will work. I've tried this on my side and it's working just fine.
Save your DataSet somewhere like ViewState on LinkButton_Panel_ViewStock_Click after filling DataSet like this
ViewState["ds"] = dtStockView
In PageIndexChanging write like this
protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
panel_ViewStock.visible = true;
GridView_Stock.PageIndex = e.NewPageIndex;
GridView_Stock.DataSource = ViewState["ds"] as DataSet
GridView_Stock.DataBind();
}
Hope this will help you
You can try to use:
protected void GridView_RowCommand(object sender, GridViewCommandEventArgs e)
I have a Gridview and I want to set Pagination. So far I have:
<asp:GridView ID="grdActivity"
runat="server"
AutoGenerateColumns="False"
AllowPaging="True"
PageSize="30"
OnPageIndexChanging="gridView_PageIndexChanging">
C# code on the back is:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dsActivity myDataSet = new dsActivity();
myDataSet = clsDataLayer.GetActivity(Server.MapPath("DB.mdb"));
grdActivity.DataSource = myDataSet.Tables["tblActivity"];
grActivity.DataBind();
}
}
protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdActivity.PageIndex = e.NewPageIndex;
grdActivity.DataBind();
}
I get no errors but when I press any other page (2 and on), nothing shows, just a blank page and the GridView disappears. Am I missing something? I see hundreds of pages showing this exact code to do this.
You will need to re-assign your datasource property. After the postback the grActivity.DataSource is null, and calling DataBind on it will clear the grid.
protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//re-assign your DataSource
dsActivity myDataSet = new dsActivity();
myDataSet = clsDataLayer.GetActivity(Server.MapPath("DB.mdb"));
grdActivity.DataSource = myDataSet.Tables["tblActivity"];
//Set the new page index
grdActivity.PageIndex = e.NewPageIndex;
//apply your changes
grdActivity.DataBind();
}
Some notes:
you should consider re-factoring the code that brings you the data into a single method called on Page_Load and inside the gridview_PageIndexChanging (see #ekad's answer)
if you have a lot of data you should consider paging the data directly on the database. Right now if your table has 500 rows all will be loaded into the dataset even if just a small part of it (the page size) will be displayed in the grid
Hi you please keep the code in a separate method and call it in page load event and page index changed event, something like
protected void FillGrid()
{
dsActivity myDataSet = new dsActivity();
myDataSet = clsDataLayer.GetActivity(Server.MapPath("DB.mdb"));
grdActivity.DataSource = myDataSet.Tables["tblActivity"];
grActivity.DataBind();
}
and in the page index changing event
protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdActivity.PageIndex = e.NewPageIndex;
FillGrid();
}
and in page load in !post back event simply call the method, like
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FillGrid();
}
}
You have to set the data source of grdActivity before calling grdActivity.DataBind() in gridView_PageIndexChanging method. I would also suggest creating a separate method to avoid repeating the same code in Page_Load and gridView_PageIndexChanging
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
grdActivity.DataSource = GetDataSource();
grdActivity.DataBind();
}
}
private DataTable GetDataSource()
{
dsActivity myDataSet = new dsActivity();
myDataSet = clsDataLayer.GetActivity(Server.MapPath("DB.mdb"));
return myDataSet.Tables["tblActivity"];
}
protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdActivity.PageIndex = e.NewPageIndex;
grdActivity.DataSource = GetDataSource();
grdActivity.DataBind();
}
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.
I have a gridview using object datasource for data binding. Everything is working fine except, When i add some new records to data it is not displaying immediately, it requires a refresh. I am using L2S Business Object with Object Data Source. Same thing in update and delete events.
I think you miss EditIndex property, change it on every event, like :
protected void HlnkbInsert_Click(object sender, EventArgs e)
{
...
gv.EditIndex = -1;
DataBindGV();
}
protected void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
...
gv.EditIndex = -1;
DataBindGV();
}
protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
...
gv.EditIndex = -1;
DataBindGV();
}
protected void gv_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
...
gv.EditIndex = e.NewSelectedIndex;
DataBindGV();
}
Are you re-binding your GridView after making the changes to your data?