访问gridView页脚中的文件上载

时间:2012-01-26 01:48:21

标签: asp.net vb.net

这是我对网格视图代码的一部分

   <asp:GridView ID="gridViewCourse" 
            runat="server" 
            AutoGenerateColumns="False" 
            onrowcancelingedit="gridViewCourse_RowCancelingEdit" 
            onrowdeleting="gridViewCourse_RowDeleting" onrowediting="gridViewCourse_RowEditing" 
            onrowupdating="gridViewCourse_RowUpdating" 
            onrowcommand="gridViewCourse_RowCommand" 
            datakeynames="CourseId"

            ShowFooter="True">
         .....

    <asp:TemplateField HeaderText="Fichier">
                    <EditItemTemplate>
                        <asp:FileUpload ID="FileUploadFichier" CssClass="upload" runat="server" Text='<%#Eval("Fichier") %>'/>  
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblFichier" runat="server" Text='<%#Eval("Fichier") %>'/>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:FileUpload ID="FileUploadFichier" CssClass="upload" runat="server"  />

                    <asp:RequiredFieldValidator ID="rfvFichier" runat="server" ControlToValidate="FileUploadFichier" Text="*" ValidationGroup="validaiton"/>
                    </FooterTemplate>

        ....

这个我的方法“onrowcommand =”gridViewCourse_RowCommand“”

        Protected Sub gridViewCourse_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
    If e.CommandName.Equals("AddNew") Then
        Dim txtId As TextBox = DirectCast(gridViewCourse.FooterRow.FindControl("txtftrId"), TextBox)
        Dim txtCours As TextBox = DirectCast(gridViewCourse.FooterRow.FindControl("txtftrCours"), TextBox)
        Dim txtPrix As TextBox = DirectCast(gridViewCourse.FooterRow.FindControl("txtftrPrix"), TextBox)
        Dim txtTutor As TextBox = DirectCast(gridViewCourse.FooterRow.FindControl("txtftrTuteur"), TextBox)
        con.Open()
        Dim cmd As New SqlCommand(((("insert into Courses(CourseID,CourseName,Price,Tutor) values('" + txtId.Text & "','") + txtCours.Text & "','") + txtPrix.Text & "','") + txtTutor.Text & "')", con)
        Dim result As Integer = cmd.ExecuteNonQuery()
        con.Close()
        If result = 1 Then
            BindCoursesDetails()
            lblresult.ForeColor = Color.Green
            lblresult.Text = " Details inserted successfully"
        Else
            lblresult.ForeColor = Color.Red
            lblresult.Text = " Details not inserted"


        End If
    End If

    Dim upload As FileUpload = DirectCast(gridViewCourse.FindControl("FileUploadFichier"), FileUpload)

    upload.SaveAs((Server.MapPath(Request.ApplicationPath & "/CoursesFiles/" & Path.GetFileName(FileUpload1.PostedFile.FileName))))
End Sub

问题是上传的演员表(As FileUpload)之后什么都没有... 其他的都很好而有效......

如何访问FileUpload1并保存?

谢谢弗兰克

2 个答案:

答案 0 :(得分:0)

您正在使用两个具有相同ID的文件上载控件,尝试使用不同的ID

并替换此行

     Dim upload As FileUpload = DirectCast(gridViewCourse.FindControl("FileUploadFichier"), FileUpload)

这一个

  Dim upload As FileUpload = DirectCast(gridViewCourse.FooterRow.FindControl("FileUploadFichier"), FileUpload)

答案 1 :(得分:0)

         Protected Sub gridViewCourse_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
If e.CommandName.Equals("AddNew") Then
    Dim txtId As TextBox = DirectCast(gridViewCourse.FooterRow.FindControl("txtftrId"), TextBox)
    Dim txtCours As TextBox = DirectCast(gridViewCourse.FooterRow.FindControl("txtftrCours"), TextBox)
    Dim txtPrix As TextBox = DirectCast(gridViewCourse.FooterRow.FindControl("txtftrPrix"), TextBox)
    Dim txtTutor As TextBox = DirectCast(gridViewCourse.FooterRow.FindControl("txtftrTuteur"), TextBox)

Dim upload As FileUpload = DirectCast(gridViewCourse.FindControl("FileUploadFichier"), FileUpload)

upload.SaveAs((Server.MapPath(Request.ApplicationPath & "/CoursesFiles/" & Path.GetFileName(FileUpload1.PostedFile.FileName))))
    con.Open()
    Dim cmd As New SqlCommand(((("insert into Courses(CourseID,CourseName,Price,Tutor) values('" + txtId.Text & "','") + txtCours.Text & "','") + txtPrix.Text & "','") + txtTutor.Text & "')", con)
    Dim result As Integer = cmd.ExecuteNonQuery()
    con.Close()
    If result = 1 Then
        BindCoursesDetails()
        lblresult.ForeColor = Color.Green
        lblresult.Text = " Details inserted successfully"
    Else
        lblresult.ForeColor = Color.Red
        lblresult.Text = " Details not inserted"


    End If
End If

End Sub

我必须在BindCoursesDetails()

之前放置演员表