Hello

I am attempting something like this:

Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
        If FileUpload1.HasFile Then
            Try
                FileUpload1.SaveAs("C:\Uploads\" & _
                    FileUpload1.FileName)
                Label1.Text = "File name: " & _
                    FileUpload1.PostedFile.FileName & "<br>" & _
                    "File Size: " & _
                    FileUpload1.PostedFile.ContentLength & "<br>" & _
                    "Content type: " & _
                    FileUpload1.PostedFile.ContentType & "<br>" & _
                    "Location Saved: C:\Uploads\" & _
                    FileUpload1.FileName
            Catch ex As Exception
                Label1.Text = "ERROR: " & ex.Message.ToString()
            End Try
        Else
            Label1.Text = "You have not specified a file."
        End If

        LblDate.Text = ReturnDate()
    End Sub

If a file is uploaded successfully, let's call it mrGates.txt, it generates something like the following:

File name: mrGates.txt
File Size: 162
Content type: text/plain
Location Saved: C:\Uploads\mrGates.txt

Is it possible to stylise these attributes using CSS because until they are generated, the script does not make them available, does it?

Thank you.

Recommended Answers

All 11 Replies

You can use jQuery on the client side to watch the label and when its text changes look to see if thw value contains "error". Based on the result, alter the css class of the label

$(document).ready(function() {
    $('#lblresult').change(function() {
        if($('#lblResult').val().indexOf("Error") >= 0) {
            $('#lblResult').addClass('warning');
        }
    });
});

You'd need to create the warning CSS class of course.
I haven't tested that code but it should pretty close to what you need.

Hello Hericles

Thank you for your message.

But how would I show:

File name: mrGates.txt
 File Size: 162
 Content type: text/plain
 Location Saved: C:\Uploads\mrGates.txt

Not an error.

Have the default CSS class set as you like it to be and remove that class when you the error class
$('#lblResult').removeClass('default');
That class will be used unless the label contains the word error.

Hello Hericles

Oh, right.
I will give it a go - it sounds promising.

Thank you!

For you to reference that label control using the reference "lblResult", you are going to have to add the following attribute on the control (in the aspx page).

 ClientIDMode="Static"

The reason is because if you look at your html source from the browser, you will note that asp.net will generate longer, more specific ids for the controls.

using ClientIDMode will allow you to control this behavior.

I think that might all be a bit beyond me. I am going to put it to one side for the moment in order to focus on a couple of errors I am getting with the upload.aspx file (but I am grateful to you both for your replies).

I am getting an error (green underlined 'Label1') in my upload.aspx file:

<asp:Label1 ID="LblDate" runat="server" class="labelStyle" Text="Label"></asp:Label1>

When I debug the file, I get:

Element 'Label1' is not a known element. This can occur if there is a compilation error in the Web site, or the web.config file is missing.
Error: Unknown server tag 'asp:Label1'.

In my aspx.vb file I have:

Dim Label1 As Object

    Function ReturnDate() As String

        Dim theDate As String = ""

        Dim NumericDayofMonth As Integer = Date.Now.Day
        Dim OrdinalSuffix As String = ""

        Select Case NumericDayofMonth
            Case 1, 21, 31
                OrdinalSuffix = "<sup>st</sup>"
            Case 2, 22
                OrdinalSuffix = "<sup>nd</sup>"
            Case 3, 23
                OrdinalSuffix = "<sup>rd</sup>"
            Case Else
                OrdinalSuffix = "<sup>th</sup>"
        End Select

        Dim NumericMonthofYear As Integer = Date.Now.Month
        Dim MonthofYear As String = ""

        Select Case NumericMonthofYear
            Case 1
                MonthofYear = "January"
            Case 2
                MonthofYear = "February"
            Case 3
                MonthofYear = "March"
            Case 4
                MonthofYear = "April"
            Case 5
                MonthofYear = "May"
            Case 6
                MonthofYear = "June"
            Case 7
                MonthofYear = "July"
            Case 8
                MonthofYear = "August"
            Case 9
                MonthofYear = "September"
            Case 10
                MonthofYear = "October"
            Case 11
                MonthofYear = "November"
            Case 12
                MonthofYear = "December"
        End Select

        theDate &= DateTime.Now.Day.ToString() '1, 2, 3, 4 etc
        theDate &= OrdinalSuffix & " " 'st, nd, rd, th
        theDate &= MonthofYear & " " 'Jan, Feb etc
        theDate &= DateTime.Now.Year.ToString() '2013, 2014 etc

        Return theDate
    End Function

    Sub Page_load(sender As Object, e As EventArgs) Handles Me.Load

        Session.Clear()
        Session.Abandon()
        Response.Redirect("index.aspx")

        'LblDate.Text = ReturnDate()
        Label1.Text = ReturnDate()

    End Sub

I am not sure why the date isn't showing. But then, just to make matters worse, I also have in aspx.vb (immediately after the above code):

Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click

        If FileUpload1.HasFile Then
            Try
                FileUpload1.SaveAs("C:\Uploads\" & _
                    FileUpload1.FileName)
                Label1.Text = "File name: " & _
                    FileUpload1.PostedFile.FileName & "<br>" & _
                    "File Size: " & _
                    FileUpload1.PostedFile.ContentLength & "<br>" & _
                    "Content type: " & _
                    FileUpload1.PostedFile.ContentType & "<br>" & _
                    "Location Saved: C:\Uploads\" & _
                    FileUpload1.FileName
            Catch ex As Exception
                Label1.Text = "ERROR: " & ex.Message.ToString()
            End Try
        Else
            Label1.Text = "You have not specified a file."

        End If
    End Sub

Please notice the three instances of Label1 there. As you can see, I am now hopelessly confused!

Basically, I seem to have assigned Label1 for the date and Label1 for text prod ucedafter the upload button is clicked.

There is no such control of type "Label1". If you want to use a label, then it should be "Label"

I have now removed all references to Label1 as you kindly advised and, after debugging, the upload.aspx file appears in my browser.

I have a 'Log out' link on that page and that works. The date does not work (I can see text that says 'Label' where the date should be)

The problem I am having with the date is that I do not know where to place this:

LblDate.Text = ReturnDate()

in my aspx.vb file.

The other error I get is when I try to upload a file (any file):

'Server Error in '/' Application - Object variable or With block variable not set' in my aspx.vb file:

Label.Text = "ERROR: " & ex.Message.ToString()

It is there where you can see ERROR:

 If FileUpload1.HasFile Then
            Try
                FileUpload1.SaveAs("C:\Uploads\" & _
                    FileUpload1.FileName)
                Label.Text = "File name: " & _
                    FileUpload1.PostedFile.FileName & "<br>" & _
                    "File Size: " & _
                    FileUpload1.PostedFile.ContentLength & "<br>" & _
                    "Content type: " & _
                    FileUpload1.PostedFile.ContentType & "<br>" & _
                    "Location Saved: C:\Uploads\" & _
                    FileUpload1.FileName
            Catch ex As Exception
                Label.Text = "ERROR: " & ex.Message.ToString()
            End Try
        Else
            Label.Text = "You have not specified a file."

        End If

In my aspx file, I have:

<div>
<asp:FileUpload ID="FileUpload1" runat="server" /><br />

<asp:Button ID="btnUpload" runat="server" Text="Upload" />

<asp:Label id="UploadStatusLabel" runat="server"></asp:Label>

</div>

If you could help me out with even just one of those two problems, I would be grateful!

I can help you but the truth is that it's very confusing to follow a thread with so many questions and issues. My recommendation is to revisit your first post/question. Once it's solved, let us know in a reply and mark it solved. If you have another question, start a new thread and do the same.

Hello Jorge

I have resolved the first part of my question:

Click Here

I'll open another thread about the date. Sorry to add to the confusion.

So this thread is resolved? that's good to hear. awaiting your next thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.