2014-03-28 41 views
-1

如何在DetailsView中使用button onclick?到目前爲止,我有這個。DetailsView中的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) 
     { 

     } 
+0

你能解釋一下你想完成什麼嗎? –

+0

我正嘗試在詳細信息中上傳文件到服務器。 – JohnDoe4136

回答

1

您可以從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 
     } 
    } 

給它一個嘗試,如果你遇到任何問題,請通知我。