Why does asp.net onclick event not change the button text when the button is clicked?
Obviously I am a total noob and this is simple to some of you, but I can not figure out why the rest of the sub works, but the button1.Text="Uploading, Please Wait..." seems to be completely ignored.
The button is supposed to change text when clicked but no method I have tried works with my page.
Any ideas? Here is my simple upload form page:
<%@ Page aspcompat=true %>
<% ' import all relevant namespaces %>
<%@ import namespace="System" %>
<%@ import namespace="System.Drawing" %>
<%@ import namespace="System.Drawing.Imaging" %>
<%@ import namespace="System.IO" %>
<html>
<head>
<title>Photo 1 Upload</title>
</head>
<body bgcolor="#000000">
<p>
<table align="center" bgcolor="white" cellpadding="5" cellspacing="2" border="2" width="">
<tr>
<td align="center" valign="middle" nowrap bgcolor="554aa1">
<font face="arial" color="white" size="5">
<b>Click the "Browse..." button to select your
<br>photo then click "Upload"</b>
<p>
</font>
<font face="arial" color="white" size="4">
Note: Image must be .jpg format and less than 4MB
</font>
</td>
</tr>
<tr>
<td align="center" valign="middle" bgcolor="#cccccc">
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" /><br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Upload Photo" /> <br />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label></div>
</form>
<p>
</td>
</tr>
</table>
</body>
</html>
<script runat="server">
Protected Sub Button1_Click(ByVal sender As Object,ByVal e As System.EventArgs)
button1.Text="Uploading, Please Wait..."
If FileUpload1.HasFile Then
Try
Dim Ext
Ext = Lcase(Right(FileUpload1.PostedFile.F开发者_StackOverflow社区ileName, 3))
If Ext = "jpg" or Ext = "peg" Then
FileUpload1.SaveAs(Server.MapPath("..\" & Session("user_name") & "\photos\photo1_raw.jpg"))
response.redirect("done.asp?action=done&photo=1#photos")
Label1.Text = "File name: " & _
FileUpload1.PostedFile.FileName & "<br>" & _
"File Size: " & _
FileUpload1.PostedFile.ContentLength & " kb<br>" & _
"Content type: " & _
FileUpload1.PostedFile.ContentType
Else
Label1.Text = "ERROR: Nothing Saved - Photo must be a .jpg or .jpeg format."
end if
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
</script>
The button click event method is executing on the server, not in the browser.
To get the effect you are looking for you will need to write a client side function in javascript, ideally using a javascript framework such as jquery, so this button text change occurs in the browser before the event is fired back to the server for processing.
精彩评论