按钮在DetailsView中单击

本文关键字:单击 DetailsView 按钮 | 更新日期: 2023-09-27 17:54:19

如何在DetailsView中使用button onclick ?到目前为止,我有这个。

 <asp:DetailsView ID="DetailsView1" 
                DefaultMode= "Edit"  AutoGenerateEditButton="False" AutoGenerateInsertButton="True" 
                AutoGenerateDeleteButton="True" DataSourceID="SqlDataSource2" runat="server" 
                AutoGenerateRows="False" DataKeyNames="fileID" >
                <Fields>
                    <asp:BoundField DataField="fileID" HeaderText="fileID" InsertVisible="False" ReadOnly="True" SortExpression="fileID" />
                    <asp:BoundField DataField="filenameName" HeaderText="filenameName" SortExpression="filenameName" />
                    <asp:TemplateField HeaderText="filePath" SortExpression="filePath">
                        <EditItemTemplate>                                
                            <asp:FileUpload ID="FileEditUpload1" Width="300px" runat="server" /> <br />
                            <asp:Button ID="Button1" runat="server" Text="Upload file" onclick="Button1_Click"/>                
                        </EditItemTemplate>
                        <InsertItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("filePath") %>'></asp:TextBox>
                        </InsertItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("filePath") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>                        
                    <asp:BoundField DataField="Createdby" HeaderText="Createdby" SortExpression="Createdby" Visible="False" />
                    <asp:BoundField DataField="CreatedDt" HeaderText="CreatedDt" SortExpression="CreatedDt" Visible="False" />
                    <asp:BoundField DataField="Updatedby" HeaderText="Updatedby" SortExpression="Updatedby" Visible="False" />
                    <asp:BoundField DataField="UpdatedDt" HeaderText="UpdatedDt" SortExpression="UpdatedDt" Visible="False" />
                    <asp:BoundField DataField="Active" HeaderText="Active" SortExpression="Active" Visible="False" />
                </Fields>
                </asp:DetailsView>

后台代码

 protected void Button1_Click(object sender, EventArgs e)
    {
        //Get path from web.config file to upload
        string FilePath = ConfigurationManager.AppSettings["FilePath"].ToString();
        bool blSucces = false;
        string filename = string.Empty;
        string pathname = string.Empty;
        string filePathName = string.Empty;
        if (FileEditUpload1.HasFile)
        { 
        }

按钮在DetailsView中单击

你可以像这样从DetailsView访问fileUpload控件:

protected void Button1_Click(object sender, EventArgs e)
    {
        //Get path from web.config file to upload
        string FilePath = ConfigurationManager.AppSettings["FilePath"].ToString();
        bool blSucces = false;
        string filename = string.Empty;
        string pathname = string.Empty;
        string filePathName = string.Empty;
        //To access the file upload control
        //First get the clicked button
        Button btn = (Button)sender;
        //Then get the detailsview row
        DetailsViewRow drv = (DetailsViewRow)btn.Parent.Parent;
        //Now you can access the FileUpload control
        FileUpload FileEditUpload1 = (FileUpload)drv.FindControl("FileEditUpload1");
        if (FileEditUpload1.HasFile)
        {
            //Do the rest of your code
        }
    }

试一试,如果遇到任何问题请告诉我