0

我在这个 gridview 中有三个 TemplateFields。第一个模板字段将包含文件的路径,第二个是“上传”按钮模板字段,最后一个是“发布”按钮模板字段,当单击时,它将在同一网格视图的绑定字段中生成今天的日期。条件是用户需要在模板字段中的“释放”按钮启用之前上传一个pdf文件,因为默认是禁用的。

HTML 代码是这样的:

<asp:TemplateField HeaderText="CFVGL" SortExpression="cfvgl_path" ControlStyle-ForeColor="Blue" ItemStyle-Width="">
                <ItemTemplate>
                    <asp:HyperLink ID="link1" runat="server" Text='<%# Eval("cfvgl_path")%>' 
                        NavigateUrl='<%# "~/"+"CFVGL/" + Eval("cfvgl_path") %>' Target="_blank"  ></asp:HyperLink>
                </ItemTemplate>
                <ControlStyle ForeColor="Blue"></ControlStyle>
                <HeaderStyle CssClass="tblheader2" HorizontalAlign="Center" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Upload File" HeaderStyle-HorizontalAlign ="center"  >
                <ItemTemplate> 
                    <table id="colWith">
                    <tr>
                        <td><asp:FileUpload ID="FileUpload4" runat="server" Width="180px"></asp:FileUpload></td>
                        <td><asp:Button ID="btnUpload" runat="server" EnableViewState="False" Text="Upload" CommandName="Upload"  
                          CommandArgument='<%# Container.DataItemIndex %>'
                          OnClick="saveTheFile"></asp:Button ></td>
                    </tr>
                    </table></ItemTemplate>
                <HeaderStyle CssClass ="tblheader2" />
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate> 
                    <asp:Button ID="release" runat="server" EnableViewState="False" Text="Release" CommandName="Upload"  
                          CommandArgument='<%# Container.DataItemIndex %>'
                          OnClick="dateReleased_Click" Enabled="False"></asp:Button >
                </ItemTemplate>
                <HeaderStyle CssClass ="tblheader2" />
            </asp:TemplateField>

GridView1_RowDataBound的VB代码:

   Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim status As Integer = e.Row.Cells(2).Text
        Select Case status
            Case 0
                e.Row.Cells(2).Text = "for submission of requirements"
            Case 1
                e.Row.Cells(2).Text = "for compliance of lacking requirements"
            Case 2
                e.Row.Cells(2).Text = "for evaluation"
            Case 3
                e.Row.Cells(2).Text = "for signature"
            Case 4
                e.Row.Cells(2).Text = "approved and ready for release"
            Case 7
                e.Row.Cells(2).Text = "released"
        End Select
    End If
    If e.Row.RowType = DataControlRowType.DataRow Then

        Dim release As Button = CType(e.Row.FindControl("release"), Button)
        If release IsNot Nothing AndAlso Uploaded = True Then
            release.Attributes.Remove("enable")
            release.Attributes.Add("disable", "disable")
        Else
            release.Attributes.Remove("disable")
            release.Attributes.Add("enable", "enable")
        End If
    End If
End Sub

这是 saveTheFile 的 VB 代码

Protected Sub saveTheFile(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim btn As Button = DirectCast(sender, Button)
    Dim gvr As GridViewRow = DirectCast(btn.NamingContainer, GridViewRow)
    'Get rowindex
    Dim rowindex As Integer = gvr.RowIndex
    '------------------------------------------------------------
    Dim fileUploadRowControl As FileUpload = DirectCast(gvr.FindControl("fileUpload4"), FileUpload)
    ' to view fileupload inside the gridview
    If fileUploadRowControl Is Nothing Then
        MsgBox("No file uploaded.")
    Else
        Dim savePath As String = Path.GetFileName(fileUploadRowControl.PostedFile.FileName)
        fileUploadRowControl.SaveAs(Server.MapPath("~\\CFVGL\\" & savePath))
        '
        Dim ext As String = Path.GetExtension(savePath)
        Dim contenttype As String = String.Empty
        Dim fileName As String = fileUploadRowControl.FileName
        If fileUploadRowControl.PostedFile Is Nothing OrElse String.IsNullOrEmpty(fileUploadRowControl.PostedFile.FileName) OrElse fileUploadRowControl.PostedFile.InputStream Is Nothing Then
            MsgBox("Unable to upload the file.")
        End If
        'Set the contenttype based on File Extension
        Select Case ext
            Case ".pdf"
                contenttype = "application/pdf"
                Exit Select
        End Select
        If contenttype <> String.Empty Then
            Dim fs As Stream = fileUploadRowControl.PostedFile.InputStream
            Dim br As New BinaryReader(fs)
            Dim bytes As Byte() = br.ReadBytes(fs.Length)
            'InsertItemPosition file to database
            Dim StrQuery As String = ("Update cfvgl set cfvgl_path= @Cfvgl_path where v_id =@id")
            Dim cmd As New SqlCommand(StrQuery)
            cmd.Parameters.Add("@Cfvgl_path", SqlDbType.VarChar).Value = savePath
            cmd.Parameters.Add("@id", SqlDbType.VarChar).Value = gvr.Cells(0).Text
            InsertUpdateData(cmd)
            MsgBox("File Uploaded Successfully!")
        Else
            MsgBox("File format not recognized." _
            & " Please Upload PDF formats")
        End If
    End If
    Dim row As GridViewRow = sender.NamingContainer
    Dim FileUpload4 As FileUpload = DirectCast(row.FindControl("FileUpload4"), FileUpload)
    Dim release As Button = DirectCast(row.FindControl("release"), Button)
    If FileUpload4.HasFile Then
        release.Enabled = True
    Else
        release.Enabled = False
    End If End Sub

我的问题是 vb 中的代码无法启用释放按钮。请问有什么解决办法吗?谢谢。

4

2 回答 2

0

该按钮btnUpload正在发布表单。因此,GridViewRowDatabound不是启用或禁用release按钮的正确位置。单击btnUpload将触发该saveTheFile方法,您必须在其中放置代码。您的代码应如下所示:

Protected Sub saveTheFile(sender As Object, e As EventArgs)
    Dim row As GridViewRow = sender.NamingContainer
    Dim FileUpload4 As FileUpload = DirectCast(row.FindControl("FileUpload4"), FileUpload)
    Dim release As Button = DirectCast(row.FindControl("release"), Button)

    If FileUpload4.HasFile Then
        release.Enabled = True
    Else
        release.Enabled = False
    End If
End Sub

希望能帮助到你!

编辑 :

在您的代码中更改此部分:

If fileUploadRowControl Is Nothing Then
        MsgBox("No file uploaded.")
Else
... ... ...

对此:

Dim release As Button = DirectCast(gvr.FindControl("release"), Button)

If Not fileUploadRowControl.HasFile Then
        'MsgBox("No file uploaded.")'Don't use MsgBox
        release.Enabled = False
Else
        release.Enabled = True
        'Do other task
于 2013-11-07T04:29:48.817 回答
0

在类文件中的全局声明中

    Dim Uploaded  As Boolean=False

然后

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound

 If e.Row.RowType = DataControlRowType.DataRow Then

         Dim release As button = CType(e.Row.FindControl("release"), Button)
            If release IsNot Nothing AndAlso  Uploaded=True Then
            realese.Attributes.remove('enabled')
            release.Attributes.Add("disabled","disabled")
        Else
             release.Attributes.remove('disabled')
             release.Attributes.Add("enabled","enabled")
        End If
   End if 
 End Sub

Uploaded 是一个全局变量,当您在保存文件方法中上传成功时,您可以将其设置为 true。

Protected Sub saveTheFile(sender As Object, e As EventArgs)
    Dim row As GridViewRow = sender.NamingContainer
    Dim FileUpload4 As FileUpload = DirectCast(row.FindControl("FileUpload4"), FileUpload)
    If FileUpload4.HasFile Then
        Uploaded = True
   End If
    GridView1.DataBind() 'so that row databound is fired
End Sub

希望这会奏效。

于 2013-11-07T06:35:34.310 回答