为什么asp.net onclick事件在单击按钮时不会更改按钮文本?

时间:2010-09-10 15:57:42

标签: asp.net

显然我是一个总菜鸟,这对你们中的一些人来说很简单,但我无法弄清楚为什么其余的子工作,但 button1.Text =“正在上传,请等待......” 似乎完全被忽略了。

按钮应该在单击时更改文本,但我尝试使用的方法不适用于我的页面。

有什么想法吗?这是我的简单上传表单页面:

<%@ 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" />&nbsp;<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.FileName, 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>

1 个答案:

答案 0 :(得分:2)

按钮单击事件方法正在服务器上执行,而不是在浏览器中执行 要获得您正在寻找的效果,您需要在javascript中编写客户端函数,理想情况下使用javascript等javascript框架,因此在事件被触发回服务器进行处理之前,浏览器中会发生此按钮文本更改。