2011-08-26 44 views
1

我試圖創建一個頁面,其中包含一些帶有和編輯按鈕的數據綁定標籤中的信息。當單擊編輯按鈕時,信息被替換爲綁定到相同數據的文本框。然後可以修改數據,保存回數據庫,並使用更新後的標籤替換文本框。UpdatePanel中的文本框導致PageRequestManagerServerErrorException

一開始,並讓事情變得簡單,我只有一個UpdatePanelDataList和兩個按鈕:EditButtonCancelButtonCancelButton默認是隱藏的)。 DataListItemTemplate有兩個面板:ViewPanelEditPanelEditPanel默認爲隱藏)。當點擊EditButton時,我隱藏EditButtonDataListItems'ViewPanel,並且顯示CancelButtonDataListItems'EditPanel
沒問題。但是,一旦完成,CancelButton按鈕將不起作用,拋出PageRequestManagerServerErrorException

通過一些擺弄,我發現了這種情況,因爲EditPanel上有數據綁定文本框。如果我沒有將數據綁定到文本框,那麼一切正常。爲什麼這不起作用?

這裏是我的代碼:

UpdatePanelTest.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UpdatePanelTest.aspx.cs" Inherits="WebLetterViewer.UpdatePanelTest" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true"></asp:ScriptManager> 
    <asp:SqlDataSource ID="AllLettersDataSource" runat="server" 
     ConnectionString="<%$ ConnectionStrings:ORMSTestConnectionString %>" 
     SelectCommand="SELECT * FROM [Letters] WHERE ([id] = @id)"> 
     <SelectParameters> 
      <asp:ControlParameter ControlID="HiddenLetterID" DefaultValue="1" Name="id" PropertyName="Value" Type="Int32" /> 
     </SelectParameters> 
    </asp:SqlDataSource> 
    <asp:HiddenField ID="HiddenLetterID" runat="server" Value="1" /> 
    <div> 
     <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false"> 
      <ContentTemplate> 
       <asp:DataList ID="LettersDataList" runat="server" DataSourceID="AllLettersDataSource"> 
        <ItemTemplate> 
         <asp:Panel ID="ViewPanel" runat="server"> 
          <h2>Data1:</h2> 
          <asp:Label ID="data1Label" runat="server" BorderStyle="Solid" BorderWidth="1px" Height="100px" Text='<%# Eval("data1") %>' Width="500px" /> 
          <h2>data2:</h2> 
          <asp:Label ID="data2Label" runat="server" BorderStyle="Solid" BorderWidth="1px" Height="100px" Text='<%# Eval("data2") %>' Width="500px" /> 
          <h2>data3:</h2> 
          <asp:Label ID="data3Label" runat="server" BorderStyle="Solid" BorderWidth="1px" Height="100px" Text='<%# Eval("data3") %>' Width="500px" /> 
         </asp:Panel> 
         <asp:Panel ID="EditPanel" runat="server" Visible="False"> 
          <h2>data1:</h2> 
          <asp:TextBox ID="data1TextBox" runat="server" Height="100px" Text='<%# Eval("data1", "{0}") %>' TextMode="MultiLine" Width="500px"></asp:TextBox> 
          <h2>data2:</h2> 
          <asp:TextBox ID="data2TextBox" runat="server" Height="100px" Text='<%# Eval("data2", "{0}") %>' TextMode="MultiLine" Width="500px"></asp:TextBox> 
          <h2>data3:</h2> 
          <asp:TextBox ID="data3TextBox" runat="server" Height="100px" Text='<%# Eval("data3", "{0}") %>' TextMode="MultiLine" Width="500px"></asp:TextBox> 
         </asp:Panel> 
        </ItemTemplate> 
       </asp:DataList> 
       <asp:Button ID="EditButton" runat="server" onclick="EditButton_Click" Text="Edit" /> 
       <asp:Button ID="CancelButton" runat="server" onclick="CancelButton_Click" Text="Cancel" Visible="False" /> 
      </ContentTemplate> 
     </asp:UpdatePanel> 
    </div> 
    </form> 
</body> 
</html> 

UpdatePanelTest.aspx.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace WebLetterViewer{ 
    public partial class UpdatePanelTest : System.Web.UI.Page{ 
     protected void Page_Load(object sender, EventArgs e){ 
     } 

     protected void EditButton_Click(object sender, EventArgs e){ 
      foreach (DataListItem item in LettersDataList.Items){ 
       item.FindControl("ViewPanel").Visible = false; 
       item.FindControl("EditPanel").Visible = true; 
      } 
      EditButton.Visible = false; 
      CancelButton.Visible = true; 
      UpdatePanel1.Update(); 
     } 

     protected void CancelButton_Click(object sender, EventArgs e){ 
      foreach (DataListItem item in LettersDataList.Items){ 
       item.FindControl("ViewPanel").Visible = true; 
       item.FindControl("EditPanel").Visible = false; 
      } 
      EditButton.Visible = true; 
      CancelButton.Visible = false; 
      UpdatePanel1.Update(); 
     } 
    } 
} 
+0

是否異常已包括的任何消息?也許有些細節還是一個innerException? –

回答

1

把你EditPanel在EditItemTemplate裏,並使用Commands,你是不是用這個控制它被設計爲使用的方式:

How to: Allow Users to Edit Items in DataList Web Server Controls

標記:

<asp:SqlDataSource ID="AllLettersDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ORMSTestConnectionString %>" 
     SelectCommand="SELECT * FROM [Letters] WHERE ([id] = @id)"> 
     <SelectParameters> 
      <asp:ControlParameter ControlID="HiddenLetterID" DefaultValue="1" Name="id" PropertyName="Value" 
       Type="Int32" /> 
     </SelectParameters> 
     <!--change this --> 
     UpdateCommand="UPDATE [Categories] SET [CategoryName] = @CategoryName, [Description] 
     = @Description WHERE [CategoryID] = @original_CategoryID"> 
     <UpdateParameters> 
      <asp:Parameter Name="CategoryName" Type="String" /> 
      <asp:Parameter Name="Description" Type="String" /> 
      <asp:Parameter Name="original_CategoryID" Type="Int32" /> 
     </UpdateParameters> 
    </asp:SqlDataSource> 
    <asp:DataList ID="LettersDataList" runat="server" DataSourceID="AllLettersDataSource" 
     OnEditCommand="LettersDataList_EditCommand" OnCancelCommand="LettersDataList_CancelCommand" 
     OnUpdateCommand="LettersDataList_UpdateCommand"> 
     <ItemTemplate> 
      <asp:Panel ID="ViewPanel" runat="server"> 
       <h2> 
        Data1:</h2> 
       <asp:Label ID="data1Label" runat="server" BorderStyle="Solid" BorderWidth="1px" Height="100px" 
        Text='<%# Eval("data1") %>' Width="500px" /> 
       <h2> 
        data2:</h2> 
       <asp:Label ID="data2Label" runat="server" BorderStyle="Solid" BorderWidth="1px" Height="100px" 
        Text='<%# Eval("data2") %>' Width="500px" /> 
       <h2> 
        data3:</h2> 
       <asp:Label ID="data3Label" runat="server" BorderStyle="Solid" BorderWidth="1px" Height="100px" 
        Text='<%# Eval("data3") %>' Width="500px" /> 
      </asp:Panel> 
      <asp:Button ID="EditButton" runat="server" CommandName="edit" Text="Edit" /> 
     </ItemTemplate> 
     <EditItemTemplate> 
      <asp:Panel ID="EditPanel" runat="server"> 
       <h2> 
        data1:</h2> 
       <asp:TextBox ID="data1TextBox" runat="server" Height="100px" Text='<%# Eval("data1", "{0}") %>' 
        TextMode="MultiLine" Width="500px"></asp:TextBox> 
       <h2> 
        data2:</h2> 
       <asp:TextBox ID="data2TextBox" runat="server" Height="100px" Text='<%# Eval("data2", "{0}") %>' 
        TextMode="MultiLine" Width="500px"></asp:TextBox> 
       <h2> 
        data3:</h2> 
       <asp:TextBox ID="data3TextBox" runat="server" Height="100px" Text='<%# Eval("data3", "{0}") %>' 
        TextMode="MultiLine" Width="500px"></asp:TextBox> 
      </asp:Panel> 
      <asp:LinkButton ID="LinkButton1" runat="server" CommandName="update"> 
        Save 
      </asp:LinkButton> 
      &nbsp; 
      <asp:Button ID="CancelButton" runat="server" CommandName="cancel" Text="Cancel" Visible="False" /> 
     </EditItemTemplate> 
    </asp:DataList> 

代碼隱藏:

protected void LettersDataList_EditCommand(object source, DataListCommandEventArgs e) 
    { 
     LettersDataList.EditItemIndex = e.Item.ItemIndex; 
     LettersDataList.DataBind(); 
    } 

    protected void LettersDataList_CancelCommand(object source, 
     DataListCommandEventArgs e) 
    { 
     LettersDataList.EditItemIndex = -1; 
     LettersDataList.DataBind(); 
    } 

    protected void LettersDataList_UpdateCommand(object source, 
     DataListCommandEventArgs e) 
    { 
     //change this to your database needs 

     //String categoryID = 
     //  LettersDataList.DataKeys[e.Item.ItemIndex].ToString(); 
     //String categoryName = 
     //  ((TextBox)e.Item.FindControl("textCategoryName")).Text; 
     //String description = 
     //  ((TextBox)e.Item.FindControl("textDescription")).Text; 

     //SqlDataSource1.UpdateParameters["original_CategoryID"].DefaultValue 
     // = categoryID; 
     //SqlDataSource1.UpdateParameters["categoryName"].DefaultValue 
     // = categoryName; 
     //SqlDataSource1.UpdateParameters["Description"].DefaultValue 
     // = description; 
     //SqlDataSource1.Update(); 

     LettersDataList.EditItemIndex = -1; 
     LettersDataList.DataBind(); 
    } 
+0

謝謝你,我以爲我做錯了什麼,你的答案幫了很大忙。我還發現我的文本框中有HTML,並且驗證是停止提交表單。事後看來,這是問題,所以在技術上這不是我的問題的答案,但正如我所說的那樣,它幫助了很多,並且向我揭示了真正的問題。 –

+0

很酷,很高興幫助,我從來沒有見過任何人使用它,我學到了一些新東西。 –

相關問題