2012年3月26日月曜日

Response.Redirect and UpdatePanel Problem Again

I can't figure out if I'm doing something wrong, it's just how it is, or it's a bug. If I create an UpdatePanel and place a button in it and then put Response.Redirect in the button's click event, everything works as I would expect and I'm redirected to the page I specified in the event. Now, if I drop a DataList inside the UpdatePanel and then put LinkButtons that have OnCommand and CommandArguement properties inside the DataList's ItemTemplate, response.redirect won't work for those LinkButtons. It just clears the datagrid and remains on the same page.

I've read everything I can find about people having the same problems but everything I read doesn't address buttons inside a DataList. Normal buttons in the UpdatePanel work for me. I'm using Ajax 1.0 RC.

Strange. Could you share your code?

Here's what I tried, and it works fine:

<%@. Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"> protected void item_command(object sender, DataListCommandEventArgs e) { Response.Redirect("http://ajax.asp.net"); } protected void Page_Load(object sender, EventArgs e) { list.DataSource = new string [] { "one", "two", "three" }; list.DataBind(); }</script><html xmlns="http://www.w3.org/1999/xhtml" ><head ID="Head1" runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <asp:ScriptManager ID="sm1" runat="server" /> <asp:UpdatePanel ID="up1" runat="server" UpdateMode="Conditional"> <ContentTemplate> <asp:DataList ID="list" runat="server" OnItemCommand="item_command"> <ItemTemplate> <asp:LinkButton ID="button" runat="server" Text="<%# Container.DataItem%>" CommandName="redirect" /> </ItemTemplate> </asp:DataList> </ContentTemplate> </asp:UpdatePanel> </form></body></html>

I need to get the CommandArgument from the LinkButton, set it to a session variable and then redirect. Here is an example of my code.

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:datalist id="MyList" runat="server" EnableViewState="False" RepeatColumns="4">
<ItemTemplate>
<asp:LinkButton ID="MyLinkButton" runat="server" EnableViewState="FALSE" OnCommand="My_Command" CommandArgument='<%# Eval("MyID") %>'><%# Eval("MyName") %>
</asp:LinkButton>
</ItemTemplate>
</asp:datalist>
</ContentTemplate>
</asp:UpdatePanel>
Public Sub My_Command(ByVal senderAs Object,ByVal eAs System.Web.UI.WebControls.CommandEventArgs) Session("MyID") = e.CommandArgument Response.Redirect("MyDetail.aspx",False)End Sub
 
Thanks!

I haven't tried this yet, but I notice you're passing False as the second argument to Response.Redirect()... does it make a difference if you change that to true? (Or simply drop it as in my example?)

I've tried it all three ways with the same result.


I tried the following code and it seems to work for me too. Have you used Fiddler on any other trace tool to chaeck what the response is from the server?

<%@. Page Language="VB" AutoEventWireup="true" %><script runat="server"> Protected Sub LinkButton1_Command(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs) Session("MyID") = e.CommandArgument Response.Redirect("Default2.aspx") End Sub</script><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server" />   <asp:UpdatePanel ID="UpdatePanel1" runat="server" EnableViewState="False"> <ContentTemplate> <asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" EnableViewState="False" RepeatColumns="4"> <ItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Eval("AddressID")%>' OnCommand="LinkButton1_Command" Text='<%# Eval("AddressLine1") + Eval("AddressLine2")%>'></asp:LinkButton> </ItemTemplate> </asp:DataList> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString%>" SelectCommand="SELECT TOP 100 Person.Address.* FROM Person.Address"></asp:SqlDataSource> </ContentTemplate> </asp:UpdatePanel>   </form></body></html>

I finally found the problem. In my codefile I have a sub that gets called that fills the datalist in the Page_Load procedure.

As an example:

If Not Page.IsPostBackThen
 'Do some stuff if this isn't a postback UpdateMyDataListEnd If
This worked for everything *except* clicking on the LinkButtons in the Datalist. So what I did was this and it now works for the LinkButtons as well.
 
If Not Page.IsPostBackThen
 'Do some stuff if this isn't a postback
Else UpdateMyDataListEnd If

0 件のコメント:

コメントを投稿