我在这个gridview中有三个TemplateFields。第一个模板字段将包含文件的路径,第二个是“上传”按钮模板字段,最后一个是“发布”按钮模板字段,在单击时,它将在同一网格视图的边界字段中生成今天的日期。条件是用户需要上传pdf文件,然后才能启用模板字段中的“release”按钮,因为默认为禁用。
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中的代码无法启用释放按钮。有解决方案吗?感谢。
答案 0 :(得分:0)
按钮btnUpload
正在发布表单。因此,GridView的RowDatabound
不适合启用或禁用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
答案 1 :(得分: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
希望这会奏效。