2012-07-19 112 views
0

我目前在我的一個項目中使用asp:formview,它是這樣使用的;asp:FormView ItemUpdating event not firing

<asp:FormView ID="formViewGalleryEdit" runat="server" DefaultMode="Edit" 
      RenderOuterTable="False" DataKeyNames="GalleryID" 
      onitemupdating="formViewGalleryEdit_ItemUpdating"> 
      <EditItemTemplate> 
       <div class="field-group"> 
        <label for="textBoxVendorName">Gallery Heading:</label> 
        <div class="field"> 
         <asp:TextBox runat="server" ID="textBoxGalleryHeading" Width="90%" 
          Text='<%# Eval("GalleryHeading").ToString() %>' /> 
        </div> 
       </div> 
       <div class="field-group">  
        <label for="textBoxDescription">Gallery Description:</label> 
        <div class="field"> 
         <asp:TextBox runat="server" ID="textBoxDescription" Text='<%# Eval("GalleryDescription") %>' 
          Width="90%" Columns="50" 
          Rows="7" TextMode="MultiLine" /> 
        </div>  
       </div> 
       <div class="field-group inlineField"> 
        <label for="myfile">Gallery Image:</label> 
        <div class="field"> 
         <asp:FileUpload ID="imageFileUpload" runat="server" /> 
        </div> 
       </div> 
       <br /> 
       <div class="field-group inlineField"> 
        <div class="field"> 
         <img src='/images/gallery/<%# Eval("GalleryImage") %>' alt='<%# Eval("GalleryImage") %>' /> 
        </div> 
       </div> 
       <div class="field-group">  
        <label for="textBoxDescription">Gallery Button Text:</label> 
        <div class="field"> 
         <asp:TextBox runat="server" ID="textBoxButtonText" Text='<%# Eval("GalleryButtonText") %>' 
          Width="90%" /> 
        </div>  
       </div> 
       <div class="field-group">  
        <label for="textBoxDescription">Gallery Button URL:</label> 
        <div class="field"> 
         <asp:TextBox runat="server" ID="textBoxGalleryUrl" Text='<%# Eval("GalleryButtonUrl") %>' Width="90%" /> 
        </div>  
       </div> 
       <br /> 
       <div class="field-group">  
        <div class="actions"> 
         <asp:Button ID="buttonUpdate" runat="server" CausesValidation="True" CommandArgument='<%# Eval("GalleryId") %>' 
          Width="60" CommandName="Update" Text="Update" /> 
         <asp:Button ID="buttonCancel" runat="server" CausesValidation="False" Width="60" 
          CommandName="Cancel" Text="Cancel" /> 
        </div> <!-- .actions --> 
       </div> 
      </EditItemTemplate> 
     </asp:FormView> 

現在,在這個文件的代碼背後,我正在處理它,像這樣;

protected void formViewGalleryEdit_ItemUpdating(object sender, FormViewUpdateEventArgs e) 
{ 
    try 
    { 
     string galleryId = e.CommandArgument.ToString(); 
     string galleryHeading = Server.HtmlEncode(((TextBox)formViewGalleryEdit.FindControl("textBoxGalleryHeading")).Text); 
     string galleryDescription = ((TextBox)formViewGalleryEdit.FindControl("textBoxDescription")).Text; 

     FileUpload control = (FileUpload)formViewGalleryEdit.FindControl("imageFileUpload"); 
     string galleryImage = control.FileName; 
     string location = Server.MapPath("~/images/gallery/") + galleryImage; 
     control.SaveAs(location); 

     string galleryButtonText = ((TextBox)formViewGalleryEdit.FindControl("textBoxButtonText")).Text; 
     string galleryUrl = ((TextBox)formViewGalleryEdit.FindControl("textBoxGalleryUrl")).Text; 

     bool status = GalleryManager.UpdateGallery(galleryId, galleryHeading, galleryDescription, galleryImage, 
                galleryButtonText, galleryUrl); 
     if (status) 
     { 
      literalSucess.Text = "Item Updated Successfully"; 
      panelScucess.Visible = true; 
     } 
    } 
    catch (Exception ex) 
    { 
     literalError.Text = ex.Message; 
     panelError.Visible = true; 
    } 

} 

我也插入了一個斷點,但它沒有觸發事件。我做錯了什麼? 謝謝,並感謝所有回答

回答

0

我知道這聽起來很愚蠢,但我通過使用下面的代碼解決了問題;

if(!Page.IsPostBack()) 
{ 
    // code goes here 
} 
+0

向下投票,因爲你沒有說你把它放在哪裏。不是一個可用的答案。我在這裏遇到同樣的問題,我無法用你的答案來解決我的問題。謝謝。 – toddmo 2016-04-13 17:19:57

2

你已經得到了99%的權利。您只需將Button控件更改爲LinkButton控件即可,如the MSDN example所示,您將可以輕鬆前往。

<div class="actions"> 
    <asp:LinkButton ID="buttonUpdate" runat="server" CausesValidation="True" CommandArgument='<%# Eval("GalleryId") %>' 
         Width="60" CommandName="Update" Text="Update" /> 
    <asp:LinkButton ID="buttonCancel" runat="server" CausesValidation="False" Width="60" 
         CommandName="Cancel" Text="Cancel" /> 
</div> 
+0

鏈接按鈕和命令按鈕都工作 – 2012-07-20 10:03:32

+0

當我試圖代碼,相同的代碼與'LinkBut​​ton'但不與合作正常的'按鈕'。你有沒有得到你的解決方案使用普通的'Button'?如果不是,那就是你所需要的,我可以稍微調整一下,試着去獲得它。 – 2012-07-20 14:04:53

0

如果您需要觸發按鈕上的事件ItemUpdating,則需要捕獲事件點擊按鈕。類似這樣的:

此代碼在VB中。

Protected Sub buttonUpdate_Click(sender As Object, e As EventArgs) 

    FormView1.UpdateItem(True) 

End Sub 

和頁面看起來你收到,只有一個變化

<div class="actions"> 
     <asp:Button ID="buttonUpdate" runat="server" CausesValidation="True" **OnClick="buttonUpdate_Click"** CommandArgument='<%# Eval("GalleryId") %>' Width="60" CommandName="Update" Text="Update" /> 
     <asp:Button ID="buttonCancel" runat="server" CausesValidation="False" Width="60" CommandName="Cancel" Text="Cancel" /> 
</div> <!-- .actions -->