0

我目前在我的一个项目中使用 asp:formview,它是这样使用的;

<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;
    }

}

我还插入了一个断点,但它没有触发事件。我做错了什么?感谢并感谢所有回答

4

3 回答 3

2

你把所有事情都做对了 99%。您需要做的就是将您的Button控件更改为LinkButton控件,如MSDN 示例中所示,您就可以开始使用了。

<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>
于 2012-07-19T18:35:41.047 回答
0

我知道这听起来很傻,但我使用以下代码解决了这个问题;

if(!Page.IsPostBack())
{
    // code goes here
}
于 2012-07-21T09:40:31.110 回答
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 -->
于 2014-09-22T20:34:05.293 回答