ASP.net Gridview Paging doesin't work inside UpdatePanel - asp.net

Although, questions somehow similar to this have been asked for a number of times, but the question is still unsolved. Here is the question:
I have a GridView which is contained in a tab container AJAX control which itself is inside an UpdatePanel. Gridview works excellent and its corresponding methods are fired accurately, but when I enable paging(e.g.) after I click on page 2, the GridView hides itself. here is my PageIndexChanging() method:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
UpdatePanel2.Update();
}
Why paging causes GridView to stop working correctly? What can I do?

The solution is that you should refill the dataset which is used to populate the gridview, each time your page index is changed. By this way, you could ensure that in each seperate postback which has been triggered by the gridview page number, results will be populated.

I just tried that above code. I had same issue and now it is working just fine.
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
// UpdatePanel2.Update(); <-- Remove this line from your code.
}
I have GridView inside update panel. Did you write your event PageIndexChanging in your .aspx file also?
Hope this helps.

Further research:
http://msdn.microsoft.com/en-us/library/cc295545.aspx
Controls that are not compatible with UpdatePanel controls
The following ASP.NET controls are not compatible with partial-page updates, and are therefore not designed to work inside an UpdatePanel control:
GridView and DetailsView controls when their EnableSortingAndPagingCallbacks property is set to true. The default is false.

I had the same issue , changing the updatepanel property UpdateMode="Conditional" to UpdateMode="Always" and setting the property ChildrenAsTriggers="true" solved the problem for me.

To do it, you have to re set the datasource in the page index change event. The performance will be lower but that's the way you can make it works.
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.DataSource = ;//Set again the datasource
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
UpdatePanel2.Update();
}

Related

Hide databound RadGrid rows

I have a telerik radgrid in my .aspx page name rgLowRise, and I have an ObjectDataSource as the DataSource set like rgLowRise.DataSourceID = odsLowRise. This works fine, but I want it not to show any records at first load, how can I do that? And I have to use DataSourceID and not DataSource due to some reasons.
Thanks
You can remove the DataSourceID from the markup or your code behind (Page_Load) and set at a later stage, e.g. Button_Click or some other event
protected void Button_Click(object sender, EventArgs e)
{
rgLowRise.DataSourceID = odsLowRise; //until you click this button, the grid would display nothing
}

error when canel row in gridview

this error apear when I try to cancel row in grid view
The GridView 'GridView1' fired event RowCancelingEdit which wasn't handled
In the mark up, add row cancelling edit event for the gridview
RowCancelingEdit="GridView1_RowCancelingEdit"
In the code behind add,
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
//switch back to default mode
GridView1.EditIndex = -1;
//Bind the grid
Gridview1.Datasource=yourDatasource;
GridView1.DataBind();
}
If this is ASP.NET then you may have specified the name of the handler in the ASP.Net page and not implmented it in the Code Behind page.
Can you post the code where it is defined?
You will have
<asp:GridView ID="GridView1" runat="server" RowCancelingEdit="MyFunction">
Just remove the RowCancelingEdit section
Try checkng the value of "EditItemIndex" in page prerender, see if it get chaged after the first postback.
With Gridview after setting the EditItemIndex, you have to do a rebind usually by just calling your GridView1.DataBind()

How to use the FindControl function to find a dynamically generated control?

I have a PlaceHolder control inside of a ListView that I am using to render controls from my code behind. The code below adds the controls:
TextBox tb = new TextBox();
tb.Text = quest.Value;
tb.ID = quest.ShortName.Replace(" ", "");
((PlaceHolder)e.Item.FindControl("ph_QuestionInput")).Controls.Add(tb);
I am using the following code to retrieve the values that have been entered into the TextBox:
foreach (ListViewDataItem di in lv_Questions.Items)
{
int QuestionId = Convert.ToInt32(((HiddenField)di.FindControl("hf_QuestionId")).Value);
Question quest = dc.Questions.Single(q => q.QuestionId == QuestionId);
TextBox tb = ((TextBox)di.FindControl(quest.ShortName.Replace(" ","")));
//tb is always null!
}
But it never finds the control. I've looked at the source code for the page and the control i want has the id:
ctl00_cphContentMiddle_lv_Questions_ctrl0_Numberofacres
For some reason when I look at the controls in the ListViewDataItem it has the ClientID:
ctl00_cphContentMiddle_lv_Questions_ctrl0_ctl00
Why would it be changing Numberofacres to ctl00? Is there any way to work around this?
UPDATE:
Just to clarify, I am databinding my ListView in the Page_Init event. I then create the controls in the ItemBound event for my ListView. But based on what #Womp and MSDN are saying the controls won't actually be created until after the Load event (which is after the Page_Init event) and therefore are not in ViewState? Does this sound correct?
If so am I just SOL when it comes to retrieving the values in my dynamic controls from my OnClick event?
UPDATE 2:
So i changed the code i had in my Page_Init event from:
protected void Page_Init(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//databind lv_Questions
}
}
to:
protected void Page_Init(object sender, EventArgs e)
{
//databind lv_Questions
}
And it fixed my problem. Still a little confused as to why I want to databind regardless of whether it's a postback or not but the issue is resolved.
It looks like you're adding your textbox to a Placeholder control... but then you're searching a ListViewDataItem container for it later.
Seems to me that you need to search for the Placeholder first, and then search it for the textbox.

Can't see next page in gridview

when I use " AllowPaging="True" " property in gridview and upload my webSite ,when I click next page , I can't see that and I see same record that I see already . point : I bind a dataBase on gridview,and I see that in gridview.above problem when I uplaod my website,happen . what is problem that I can't see next page of gridview ?
I am having a bit of trouble completing understanding your question. I think your are basically trying to get paging to work on a gridview.
In addition to setting "AllowPaging=True", you will need to hook into your paging event like so:
void GridView_PageIndexChanging(Object sender, GridViewPageEventArgs e)
{
e.NewPageIndex = e.NewPageIndex + 1;
DataBind();
}
Check it out on MSDN
you need to set the current page index property in Page index chaged event. then call the bind method in next line. then only you able to see the next page and it will work.
example
protected void SearchGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//set the current page index for grid
searchResultsGrid.PageIndex = e.NewPageIndex;
//Bind the result sets
BindSearchResults();
}
Custom control would help to avoid writing on page level event handlers

DotNetNuke ObjectDataSource.SelectMethod not being saved in viewstate

I'm using DotNetNuke 4.9.2 and am running into an odd issue.
I have a MultiView in the module that I'm developing, and in one of the views have a GridView that is bound to an ObjectDataSource.
In a separate view, i have several buttons that will switch the SelectMethod of the ObjectDataSource in the 2nd view and then set that view active. That all works fine, until the grid is sorted on the 2nd view - which causes a postback and the ODS somehow picks up its original SelectMethod. The SelectParameters that are assigned at the same time in the code-behind stick though.
Seems to me that the ObjectDataSource should be remembering the SelectMethod in viewstate, shouldn't it?
<asp:ObjectDataSource runat="server" ID="MyObjectDataSource" SelectMethod="MyFirstSelectMethod" TypeName="Whatever"></asp:ObjectDataSource>
protected void Button1_Click(object sender, EventArgs e)
{
MyObjectDataSource.SelectMethod = "MyNewMethod";
// more code here to change the parameters as well...
MyMultiView.SetActiveView(MyView2);
}
When I run that button click, the grid displays as expected. When I click on one of the column headers for the GridView and break in the page load to inspect the SelectMethod, it has reverted to the one declared in the markup.
Any suggestions as to what my problem could be here?
I'm guessing you've made sure that you're not resetting .SelectMethod when the page reloads?
I ended up working around the issue by just using a page property to hold the selectmethod, and then resetting it on each postback...
protected string MySelectMethod
{
get
{
return (string)ViewState["MySelectMethod"] ?? MySearchResultsDataSource.SelectMethod;
}
set
{
ViewState["MySelectMethod"] = value;
MySearchResultsDataSource.SelectMethod = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
MySearchResultsDataSource.SelectMethod = MySelectMethod;
}
}
protected void MyButton_Click(object sender, EventArgs e)
{
MySelectMethod = "MyNewMethod";
}
Still not sure why that SelectMethod prop doesn't stick on a postback in nuke. I'm sure this has worked fine for me in straight asp.net projects in the past...

Resources