ASP.NET Page_PreRender method - asp.net

I'm trying to set a htmltablecell.innerHTML from code behind on Page_PreRender.
It works fine the first time it's set.
Hereafter I need to set the reference to a new value from a string (I have checked that the string value is changed), but it keeps displaying the old value in the HTML output.
When I debug the htmltablecell.innerHTML, I can also see that the value has changed.
The reason why the value is set from Page_PreRender method, is that I have a User Control which updates the string after the Page_load has fired.

What you desribe can occur, if the post back is asynchronous (fired from a control in an UpdatePanel) when the control being accessed, (the HtmlTableCell in this case) is outside the UpdatePanel.
Disregard if this is not the case.
Edit
You have two options.
You can setup the control within the UpdatePanel which is initiating the async postback to perform a standard postback. To do this, define a PostBackTrigger within the UpdatePanel, setting the ControlID to the ID of the control you wish to perform the postback.
(Because this will be performing a full postback, the HtmlTableCell will then be accessible to modify)
<asp:UpdatePanel ... >
...
<Triggers>
<asp:PostBackTrigger ControlID="ControlIDToPostBack" />
</Triggers>
</asp:UpdatePanel>
Or you could add the HtmlTableCell (and parent rows, table etc) you wish to update to another UpdatePanel setting both of the UpdatePanel's UpdateMode properties to Always.
This will ensure that the content within both UpdatePanel controls is updated for all postbacks that originate from the page. Which includes asynchronous postbacks.
<asp:UpdatePanel ... UpdateMode="Always">
Hope this helps.

Related

ASP.NET webform Fileupload inside an ascx which is inside an updatepanel in the aspx

here is some code in my page :
<asp:UpdatePanel ID="UpdatePanelEQSelector" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<uc12:EQSelector ID="custEQSelector" OnEqChange="custEQSelector_OnEqChange" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
Inside my user Control, I'm asked to add a fileuploader.
After coding it simply, I tested and my fileuploader is always empty.
I've searched a while and discovered that it was a normal behavior.
The solution to make it work is to add a PostBackTrigger for the updatePanel.
When I tested it in my aspx page, I achieved to do it and my fileUploader had the file.
Then I tried to add it dynamically (to finally do it in my control), it worked with that :
PostBackTrigger trigger = new PostBackTrigger();
trigger.ControlID = this.btnTest.ID;
this._UpdatePanelEQSelector.Triggers.Add(trigger);
But I can't manage to make this code work in my control (I passed my updatePanel as a parameter to my control set in Load, the fileUpload is always empty)
Do you see a solution ?
Thanks
Does it postback, but leaves the control empty? or is it not posting back at all. If it's not posting back at all I'd add this
ScriptManager.GetCurrent(this).RegisterPostBackControl(btnTest);
to the page_load of the user control.
If it is posting back, but the control is empty, then I'd wager there is probably some dodgy html somewhere on the page and the values are getting lost.

How do I force asp.net page to postback when I'm in code behind that called by UpdatePanel

When an UpdatePanel Calls a method in server and for example this method change
textBox1.Text = "12312"
and this textBox1 was out of updatePanle scope
it dosn't change it text till a postback happend to the page
so I need after that calculation and chaging the Textbox's text in the server, I need to forcepage to do postback
plz can any one help ?
If you want to force a refresh you could try: Response.Redirect(Request.Url.AbsoluteUri)
This should force a redirect to the current page.
Hope this helps
If you wish for a control within the UpdatePanel to perform a standard postback, define a PostBackTrigger within the UpdatePanel, setting the ControlID to the ID of the control you wish to perform the postback.
<asp:UpdatePanel ...
...
<Triggers>
<asp:PostBackTrigger ControlID="ControlIDToPostBack" />
</Triggers>
</asp:UpdatePanel>
Or you could add the TextBox control you wish to update to another UpdatePanel setting both of the UpdatePanel's UpdateMode properties to Always.
This will ensure that the content within both UpdatePanel controls is updated for all postbacks that originate from the page. Which includes asynchronous postbacks.
<asp:UpdatePanel ... UpdateMode="Always"

gridview delete command within updatepanel, and separate __doPostback() functionality

I have a gridview within an updatepanel, the rows have a delete button which removes that row.
Elsewhere, I run code to insert a row. Following this insert I run __doPostback() with the ID of the updatepanel, then in the updatepanel's load() event I call databind() on the gridview.
As soon as I implement the __doPostback() and databind, the inbuilt gridview delete stops working! :( The actual refresh/databind when adding the row works well.
How can I overcome this? I guess something may be awry in that when clicking on the delete button, the databind is conflicting with the inbuild delete/refresh functionality?
Thanks!
EDIT: Apologies if the question isn't described well...
Essentially, I wish to have a gridview with built-in delete functionality through the datasource and command column etc. inside of an updatepanel. I also want to update this panel seperately, but when I put in this separate update code (gridview.databind in the updatepanel.load) it breaks the standard delete functionality. Hope that is clear :)
You've tried put the UpdatePanelMode as Conditional and use UpdatePanel.Update() besides the ClientScript.RegisterStartupScript during your insertion block ?
I believe the problem is that the event is inside of the GridView and you can't access them as easily as you can with something like a Button. To register the GridView to make the Async events you need to attach it to the ScriptManager.
To do this you use the RegisterAsyncpostBackControl method.
Here is a general idea of how to do it.
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server">
<%-- your fields, etc --%>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
In your code behind you do
protected void Page_Load()
{
ScriptManager1.RegisterAsyncPostBackControl(GridView1);
}
It's been a while since I've done this but I believe this will allow the GridView to function as you'd expect, except you don't need the extra DataBind() I don't believe in this case.
You can also set the UpdatePanel to Conditional and fire an UpdatePanel1.Update() on top of this as Jeison suggested.
You can find some added details at http://msdn.microsoft.com/en-us/library/bb386452.aspx
If you still have trouble, let us know what happened.
It seems like you calling DataBind() of GridView every time the UpdatePanel load after the insert button click, it reload the data before delete reaches the DataSource.
EDIT
If so, you can add boolean eventArgument in __doPostBack(updatePanelId, "true"). And using this you could add a condition in your updatepanel load event like
if(this.updatepanel1.Page.Request.Params["__EVENTARGUMENT"] == "true"]
this.gridview.databind()
Hope this will resolve the issue.

Updating a control outside the UpdatePanel

I have the following ASPX structure:
<UpdatePanel id="OutsidePanel" UpdateMode="Conditional">
<div runat="server" id="myDiv">
<UpdatePanel id="InsidePanel" UpdateMode="Conditional">
<asp:ImageButton that causes a postback.. />
</UpdatePanel>
</div>
</UpdatePanel>
When the imageButton is clicked, on the server side, I change the class of myDiv. It's not getting updated. I assume this is because the div is outside of the Inside UpdatePanel. How would I force it to update?
Could you not just call the Update method of the "OutsidePanel" UpdatePanel on the server when the Imagebutton in "InsidePanel" causes a postback? Or alternatively, set up ImageButton click event as a trigger for "OutsidePanel"
According to MSDN
If the UpdateMode property is set to
Conditional, the UpdatePanel control's
content is updated in the following
circumstances:
When you call the Update method of the UpdatePanel control explicitly.
When the UpdatePanel control is nested inside another UpdatePanel
control, and the parent panel is
updated.
When a postback is caused by a control that is defined as a trigger
by using the Triggers property of the
UpdatePanel control. In this scenario,
the control explicitly triggers an
update of the panel content. The
control can be either inside or
outside the UpdatePanel control that
the trigger is associated with.
When the ChildrenAsTriggers property is set to true and a child
control of the UpdatePanel control
causes a postback. Child controls of
nested UpdatePanel controls do not
cause an update to the outer
UpdatePanel control unless they are
explicitly defined as triggers.
If you are updating the class server-side, then the answer is yes. The inner update panel is being rerendered and passed back to the client, but the outer update panel is not being replaced. Anything outside the actual panel being updated won't be replaced when the AJAX callback returns. You might want to consider adding some javascript that updates the div when the result is returned as a simple solution to changing the class.

Trigger an update of the UpdatePanel by a control that is in different ContentPlaceHolder

I have a page with two ContentPlaceHolders. One has a DropDown and another UpdatePanel with content.
How can I trigger update to the UpdatePanel by the DropDown's selectedItemChanged event when they are in different ContentPlaceholders?
The following would not work since UpdatePanel1 doesn't know about DropDown1:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
<ContentTemplate>
Some content that needs to be updated here...
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DropDown1" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
One way is to make an ajax page method that would be called by javascript on the page when DropDown's item is selected. Then in code behind, inside that page method, call UpdatePanel1.Update().
Is there an easier alternative?
From http://msdn.microsoft.com/en-us/library/system.web.ui.asyncpostbacktrigger.aspx
The control that the
AsyncPostBackTrigger references must
be in the same naming container as
the update panel for which it is a
trigger. Triggers that are based on
controls in other naming containers
are not supported.
The workaround is to use the UniqueID of the control that the
trigger is referencing. Unfortunately the UniqueID isn't qualified
until the control has been added to its parent (and its parent
has been added to its parent, all the way up the control tree).
In your code behind, try:
UpdatePanel1.Triggers.Add(new AsyncPostBackTrigger()
{
ControlID = DropDown1.UniqueID,
EventName = "SelectedIndexChanged", // this may be optional
});
In the code-behind file, you should be able to do:
ScriptManager.RegisterAsyncPostBackControl(dropdown1);
You can enforce update any of page UpdatePanels by call updatePanel1.Update() method on server side.
For example during update updatePanel1 on button1.Click call updatePanel2.Update() and both panels will be updated.

Resources