I have code like this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Do something
}
}
When I click on paging in datapager, first the IsPostBack condition is skipped since it is a postback and then control moves to listItems_PagePropertiesChanging() event.
After executing this event, the control goes to page_Load again and gets inside the if(!IsPostBack) condition.
This is happening only in Mozilla 3.0.14, but not in IE 7.0.
Anyone please give me a solution ASAP.
Thanks in advance
Related
I've got an ASP.NET page on which I've got a GridView which is part of a form. I've enabled editing on the GridView, so it causes a postback to the page.
I've also got a form on the page, which causes a postback when it's submitted.
Can someone tell me how I can distinguish between the two postbacks? When the form is submitted I need to handle that, but not for example when someone hits edit on the GridView.
Thanks.
Have you got some code which looks like
<asp:Button OnClick="ButtonEventHandler" />
<asp:GridView OnRowEditing="GridViewEditEventHandler" />
you could do what ever it was you need to do in the eventhander specific to the event you were interested in handling at that point.
protected void ButtonEventHandler (object sender, EventArgs e)
{
// do stuff from the button click event
}
protected void GridViewEditEventHandler(object sender, GridViewEditEventArgs e)
{
// do something else from the gridview event
}
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();
}
I'm using a Sharepoint WebPart to load a UserControl which has a button that does some processing on PostBack. I got a problem: when I click the button for the first time, the data loaded on ! IsPosback gets lost, but this does not occur when I click the button again. I think my problem is explained here: Sharepoint Lifecycle, but I haven't been able to find a workaround.
Any help would be really appreciated.
Additional Info:
I'm using EnsureChildControls on the WebPart's OnLoad event, and loading the UserControl on CreateChildControls.
I was able to fix this by programatically specifying an ID to the User Control.
E.g.:
protected void Page_Load(object sender, EventArgs e)
{
this.ID = "MyUserControlID";
}
More info here: http://bytes.com/topic/asp-net/answers/314816-dynamically-loaded-control-event-only-reached-2nd-postback
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (ViewState["MyStuff"] == null)
LoadMyStuffAndSaveToViewState();
else
DoSomethingWith(ViewState["MyStuff"]);
}
I have a user control, which is added to another user control. The nested user control is built up of a GridView, an image button and a link button. The nested user control is added to the outer control as a collection object based upon the results bound to the GridView.
The problem that I have is that my link button doesn't work. I click on it and the event doesn't fire. Even adding a break point was not reached. As the nested user control is added a number of times, I have set image button to have unique ids and also the link button. Whilst image button works correctly with its JavaScript. The link button needs to fire an event in the code behind, but despite all my efforts, I can't make it work. I am adding the link button to the control dynamically. Below is the relevant code that I am using:
public partial class ucCustomerDetails : System.Web.UI.UserControl
{
public event EventHandler ViewAllClicked;
protected override void CreateChildControls( )
{
base.CreateChildControls( );
string strUniqueID = lnkShowAllCust.UniqueID;
strUniqueID = strUniqueID.Replace('$','_');
this.lnkShowAllCust.ID = strUniqueID;
this.lnkShowAllCust.Click += new EventHandler(this.lnkShowAllCust_Click);
this.Controls.Add(lnkShowAllCust);
}
protected override void OnInit (EventArgs e)
{
CreateChildControls( );
base.OnInit(e);
}
protected override void OnLoad(EventArgs e)
{
base.EnsureChildControls( );
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
CreateChildControls( );
}
}
protected void lnkShowAllCust_Click(object sender, EventArgs e)
{
this.OnCustShowAllClicked(new EventArgs ( ));
}
protected virtual void OnCustShowAllClicked(EventArgs args)
{
if (this.ViewAllClicked != null)
{
this.ViewAllClicked(this, args);
}
}
}
I have been stuggling with this problem for the last 3 days and have had no success with it, and I really do need some help.
Can anyone please help me?
My LinkButton wasn't firing it's Click event, and the reason was I had its CausesValidation property set to True. If you don't want the link to validate the form, be sure to set this to False.
Try adding your click event to the linkbutton tag:
<asp:LinkButton runat="server" OnClick="linkShowAllCust_Click" />
Or adding it to your Page_Load:
Page_Load(object sender, EventArgs e)
{
this.lnkShowAllCust.Click += new EventHandler(this.lnkShowAllCust_Click);
}
Is the usercontrol within the gridview? If so register the event handler on the gridview's onrowcreated event.
It appears that you have a viewstate issue. Because the control isn't there when the viewstate is loaded the application doesn't know how to hook up the event to be fired. Here is how to work around this.
You can actually make your app work like normal by loading the control tree right after the loadviewstateevent is fired. if you override the loadviewstate event, call mybase.loadviewstate and then put your own code to regenerate the controls right after it, the values for those controls will be available on page load. In one of my apps I use a viewstate field to hold the ID or the array info that can be used to recreate those controls.
Protected Overrides Sub LoadViewState(ByVal savedState As Object)
MyBase.LoadViewState(savedState)
If IsPostBack Then
CreateMyControls()
End If
End Sub
I had the same issue. I had viewstate="false" on the page I was adding the control to. (on the aspx page)
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...