Asp.net Response.Redirect Error - asp.net

My team and I are working on a portal applicaiton. When a user requests a page, we get a page object (contianing permissions, actual file to use and what not). We then do a Response.Redirect to "~/Default.aspx".
The crazy thing is that when the code to validate access and what not is called from a button click event from within an ajax update panel, response.redirect is pasting a "&f2" or a "/" into the url. So rather than http://localhost/Default.aspx, the webbrowser is being redirected to http://localhost/%f2Default.aspx, and is subsequently returning a 404 error.
HttpContext.Current.Response.Redirect("~/Default.aspx", false);
Anyone have an idea of why this would occur? And it only happens when the click event fires inside an update panel.

It sounds like it is escaping the URL. Can you call a method on the code that is generating the URL to decode it before output?

The solution is to set up the update panel like this:
<asp:UpdatePanel ChildrenAsTriggers="false" UpdateMode="Conditional" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlNewAddressCountry" EventName="SelectedIndexChanged" />
<asp:AsyncPostBackTrigger ControlID="ddlAddressState" EventName="SelectedIndexChanged" />
<asp:AsyncPostBackTrigger ControlID="ddlNewAddressCity" EventName="SelectedIndexChanged" />
<asp:AsyncPostBackTrigger ControlID="ddlNewAddressPostalCode" EventName="SelectedIndexChanged" />
<asp:PostBackTrigger ControlID="btnCustomerAddressEditCancel" />
</Triggers>
...
<td colspan="2">
<asp:Button ID="btnCustomerAddressEditSave" runat="server" OnClick="CustomerAddressEditSave_Click"
Text="Save" />
<asp:Button ID="btnCustomerAddressEditCancel" runat="server" CausesValidation="false" OnClick="CustomerAddressEditCancel_Click"
Text="Cancel" />
<asp:Button ID="btnCustomerAddressEditDelete" runat="server" OnClick="CustomerAddressEditDelete_Click" OnClientClick="return confirm('Are you sure you want to delete this record?');"
Text="Delete" />
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>

Related

Make any button on the page as a trigger for an asp.net UpdatePanel other than its sibling or child postback controls

Consider the following code fragment:
<div>
<asp:Button runat="server" ID="trickyUPTrigger" Text="Tricky Update" />
<div>
<asp:Button runat="server" ID="normalUPTrigger" OnClick="normalUPTrigger_Click" Text="Normal Update" />
<asp:UpdatePanel runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="normalUPTrigger" />
</Triggers>
<ContentTemplate>
<asp:Label runat="server" ID="changeableLabel" Text="Change me"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</div>
Now make the button with ID of trickyUPTrigger as the trigger of the UpdatePanel. Or, devise a mechanism (probably... using javascript?) so that when this button is clicked UpdatePanel updates without full page postback.
If you want to update the UpdatePanel when clicking on trickyUPTrigger, you can add that button to the triggers list:
<asp:UpdatePanel runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="normalUPTrigger" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="trickyUPTrigger" EventName="Click" />
</Triggers>
<ContentTemplate>
...
</ContentTemplate>
</asp:UpdatePanel>
UPDATE
You asked some code examples showing cases with naming containers, a concept that comes into play for databound controls with item templates, like the GridView and the ListView, and for user controls. In the examples below, I use a ListView, where each item is a separate naming container.
If you wanted to trigger an update of your panel from a button in a ListView item template, the trigger would not be found at runtime, and an exception would occur:
<asp:ListView ID="lstView" runat="server">
<ItemTemplate>
<asp:Button ID="anotherTrigger" runat="server" Text="This trigger cannot be found!" OnClick="anotherTrigger_Click" />
</ItemTemplate>
</asp:ListView>
<asp:UpdatePanel runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="anotherTrigger" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
The reverse case (the UpdatePanel in the ListView item template, the trigger button outside of the ListView) does work, according to my tests, which seems to contradict the note that you mention in your comment:
<asp:ListView ID="lstView" runat="server">
<ItemTemplate>
<asp:UpdatePanel runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="anotherTrigger" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</ItemTemplate>
</asp:ListView>
<asp:Button ID="anotherTrigger" runat="server" Text="This trigger works!" OnClick="anotherTrigger_Click" />
Finally, the case where the UpdatePanel and the trigger button are both in the ListView item template also works:
<asp:ListView ID="lstView" runat="server">
<ItemTemplate>
<asp:Button ID="anotherTrigger" runat="server" Text="This trigger works!" OnClick="anotherTrigger_Click" />
<asp:UpdatePanel runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="anotherTrigger" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</ItemTemplate>
</asp:ListView>
You can notice that I set UpdateMode="Conditional" for the panel in this last example. With this setting, the panel is updated only by its own triggers. If the attribute is set to UpdateMode="Always", the panel is updated not only by his own triggers but also by the triggers of the other UpdatePanels in the page. The default value is UpdateMode="Always" (as in your code sample).

Prevent page refresh when button click without using UpdatePanel in asp.net

How i can prevent page refresh when button click without using UpdatePanel in asp.net ?
Thanks in advance,
use OnClientClick="return false;
<asp:button ID="btnAdd" runat="server" text="Button" OnClientClick="return false;" />
I know you said you don't want to use an UpdatePanel, but this might work with your tabs, though I'm not completely sure. From what I've seen, you can create a "dummy" UpdatePanel that just holds your AsyncPostBackTriggers, then match them to your control handlers. I just ran across something like this:
<asp:UpdatePanel ID="upnlAsync" runat="server" >
<ContentTemplate>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSave" EventName ="Click" />
<asp:AsyncPostBackTrigger ControlID="btnSave1" EventName ="Click" />
<asp:AsyncPostBackTrigger ControlID = "btnBackgroundTasks" EventName ="Click" />
<asp:AsyncPostBackTrigger ControlID = "btnReSubmit" EventName ="Click" />
<asp:AsyncPostBackTrigger ControlID = "btnSubmit" EventName ="Click" />
</Triggers>
</asp:UpdatePanel>
It is matched up with button controls, but they are not in an UpdatePanel themselves. When the Click handler is called in the code behind, the subroutine gets called, but the page doesn't refresh. I realize that this isn't really how the triggers are intended to be used, but it seems to work for my situation. Maybe it will work for yours.

Update panel can not find the button which trigger it

I have a button is inside a another table(s) inside the update panel.
<Update panel>
<ContentTemplate>
<table>
<table>
<Button>
<table>
<table>
</ContentTemplate>
</Update panel>
I would like to add a button to Update panel's trigger.
But am getting an err says "Update panel can not find the button which trigger it".
I am getting "Sys.Webforms.PageRequestmanagerParseErrorException: This message recieved from manager could not be parsed. Common cause for this error are when response is modified by response.write"
Please help!
PostBackTrigger example:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="btn1" runat="server" Text="Button 1-Partial Postback" />
<asp:Button ID="btn2" runat="server" Text="Button 2-Full Postback" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btn2" />
</Triggers>
</asp:UpdatePanel>

Refresh User Control without Refreshing the Page

I have a page and it has a button and a user control.
I want to refresh the user control without refreshing the page.
I know I cannot do it otherwise so what I did is wrapped my user control inside the Update Panel.
<asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />
<asp:Button ID="btnAdd" runat="server" Text="Add name to list" OnClick="btnAdd_Click" /><br /><br />
<asp:UpdatePanel ID="upShowNames" runat="server">
<ContentTemplate>
<uc1:ShowNames ID="ucShowNames" runat="server" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnAdd" />
</Triggers>
</asp:UpdatePanel>
But I still the control won't refresh.
I also tried calling the update panels. The Update() method by changing its UpdateMode to Conditional but that does not work either...
Does anyone know how can I do it?
Please change these 2 things
<asp:UpdatePanel ID="upShowNames" runat="server" UpdateMode="Conditional">
<asp:AsyncPostBackTrigger ControlID="btnAdd" EventName="Click"/>
You missed the EventName on the postback trigger, once you add that, it should work :-)

AsyncPostBackTrigger isn't working with simultaneous update calls

I have a page with some UpdatePanels, each one with its own button to update it. Since the update routines can take some time, I thought making them Asynchronous would help loading the page step by step.
But doing so, when I fire programatically the update routine of each panel, I get only the last UpdatePanel updated.
Here is an example of the code, with two UpdatePanels. There is the requirement that the update routine have to be fired on clientside pageLoad function.
Is it a bug or am I missing something in the code?
Thanks =)
<asp:UpdatePanel ID="Panel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox ID="Text1" runat="server" />
<asp:Button ID="Button1" runat="server" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<asp:UpdatePanel ID="Panel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox ID="Text2" runat="server" />
<asp:Button ID="Button2" runat="server" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button2" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
And the client-side code:
function pageLoad()
{
$('#Button1').click();
$('#Button2').click();
}
Here is why:
By default, when a page makes multiple
asynchronous postbacks at the same
time, the postback made most recently
takes precedence.
http://www.asp.net/ajax/documentation/live/tutorials/ExclusiveAsyncPostback.aspx
Here is a solution:
Handling Multiple Asynchronous Postbacks

Resources