2012-07-19 101 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 -->