【问题标题】:Progress bar not displaying when button is clicked单击按钮时不显示进度条
【发布时间】:2013-08-29 16:50:52
【问题描述】:

我有一个Fileupload 控件,当我单击上传按钮时,我只想显示一个图像,即进度条或加载 gif 图像。我通过 javascript 进行了尝试,但无法显示该图像。 我希望在上传文件之前显示该图像。

这是我的 aspx 代码:

<form id="form1" runat="server">
    <div class="transbox" id="mainbk" runat="server" style="position:absolute; top:0px; left:0px; width: 100%; height: 100%;" >
            <fieldset style="width:51%; margin-left:300px;font-family:'Palatino Linotype';font-size:large">
            <legend style="color:white;font-family:'Palatino Linotype'">Upload Video Files</legend>
                <asp:FileUpload EnableViewState="true" runat="server" ID="UploadImages" style="background-color:white; position:relative; font-family:'Palatino Linotype'; font-size:medium" Width="500px" AllowMultiple="false"/>
                <asp:RequiredFieldValidator ID="reqFile" runat="server" ControlToValidate="UploadImages" ErrorMessage="Select a File" style="color:red;position:absolute"></asp:RequiredFieldValidator><br />
                <asp:Label Text="Description:" runat="server" ID="lbldes" style="font-family:'Palatino Linotype';position:absolute; margin-top:10px; font-size:large;color:white" ></asp:Label>
                <asp:TextBox id="txtdes" runat="server" TextMode="MultiLine" Height="60px" style="margin-top:10px;margin-left:195px;" Width="300px"></asp:TextBox>
                <asp:RequiredFieldValidator ID="txtreq" runat="server" ControlToValidate="txtdes" ErrorMessage="Description Required" style="color:red;position:absolute;margin-top:20px;" ></asp:RequiredFieldValidator><br />

                <asp:Button runat="server" ID="uploadedFile" style="position:relative; font-family:'Palatino Linotype'; font-size:medium; width: 112px; height: 29px;" Text="Upload" UseSubmitBehavior="true" OnClick="uploadedFile_Click"/>
                <asp:image id="loading_img" runat="server" style="Display:none;position:absolute;margin-top:-20px;margin-left:200px;" src="../Images/Other Images/waiting.gif" />
            </fieldset>
     </div>
    </form>

这是我的javascript:

 <script type="text/javascript">
        $('#uploadedFile').click(function () {
            $('#loading_img').show(); // Show image
            return true; // Proceed with postback
        });
    </script>

这是我在 aspx.cs 页面中 uploadedFile 按钮的点击事件。

protected void uploadedFile_Click(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
             string fileExt = Path.GetExtension(UploadImages.FileName).ToLower();
             if (fileExt == ".flv" || fileExt == ".avi" || fileExt == ".mp4" || fileExt == ".3gp" || fileExt == ".mov" || fileExt == ".wmv" || fileExt == ".mpg" || fileExt == ".asf" || fileExt == ".swf")
             {
                try
                {

                    filepath = Server.MapPath("~/Videos/" + UploadImages.FileName);
                    UploadImages.PostedFile.SaveAs(filepath);
                    vurl=UploadImages.FileName.ToString();
                    newpath = "Images//Video_Thumbs//" + createvidImage(filepath);
                    al = txtdes.Text.ToString();
                    id += 1;
                    string Insert = "Insert into video (vid,videoname,videourl,vidthumb) values (@id,@alter,@vrl,@IMAGE_PATH)";
                    SqlCommand cmd = new SqlCommand(Insert, con);
                    cmd.Parameters.AddWithValue("@IMAGE_PATH", newpath.ToString());
                    cmd.Parameters.AddWithValue("@id", id);
                    cmd.Parameters.AddWithValue("@alter", al);
                    cmd.Parameters.AddWithValue("@vrl", vurl);
                    try
                    {
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                        Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('Video Uploaded!!');", true);

                        txtdes.Text = "";
                    }
                    catch (Exception e1)
                    {
                        Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('" + e1.Message.ToString() + "!!');", true);
                    }
                }
                catch (Exception ex)
                {
                    Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('" + ex.Message.ToString() + "!!');", true);
                }
            }
        }
    }

请帮助我在 javascript 或 C# 中执行此操作。

【问题讨论】:

    标签: c# javascript jquery asp.net html


    【解决方案1】:

    这里的问题是&lt;asp:Button/&gt; 标签(以及所有的 asp 标签)在页面中的呈现方式。如果您右键单击并检查该元素,您会看到它的 ID 实际上不是“uploadedFile”,而可能类似于“ctl00_uploadedFile”,因此,您的 jQuery 选择器找不到元素。我会推荐以下两件事之一:

    1) 检查页面并查看实际呈现的元素的名称,然后相应地更新您的 jQuery。

    2) 使用嵌入式 C# sn-p 提取 ID。例如,您可以将$('#uploadedFile') 替换为$('#' + '&lt;%=uploadedFile.ClientID%&gt;')。其中的 &lt;%%&gt; 部分将评估为 C# 并将 ClientID 属性(即在页面上呈现的 id 值)插入到 jQuery 选择器中。

    编辑: 显然我对 &lt;asp:Button/&gt; 标记有误,它们使用未更​​改的 name 属性而不是 id 属性呈现。 UploadFile 按钮的选择器是$('input[name="uploadedFile"]')

    【讨论】:

    • @Alex... 我检查了页面,发现它在按钮标签的位置。 这实际上是什么意思,我也收到一个错误,说未捕获的引用错误,$ 未定义..
    • 嗯。显然我的回答不适用于
    • @Alex.. 很抱歉,但由于我真的对 jquery 了解不多,所以我问这个 wid .. 我们需要编写什么样的 jQuery 源文件.. 现在我是不是我只是在 head 标签之前添加了那个脚本块......但结果仍然是一样的...... :(
    • 您需要在页面中包含一个 jQuery 源文件 which you can get here。根据您打算支持的浏览器,您可能需要 1.10.2(更具兼容性)或 2.0.3(仅限现代浏览器)。下载您选择的任何产品的生产版本,将其放在站点层次结构中的某个位置,然后将其包含在页面头部:&lt;script src="[path-to-jquery-file].js"&gt;&lt;/script&gt;
    • 如果它正在生成&lt;img id="loading_img",那么答案就更简单了——只要不理会jQuery的那一部分。 $('#loading_img').show(); 应该可以使用,如果由于某种原因无法使用,请尝试使用$('#loading_img').css('display', 'block');
    猜你喜欢
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多