使用ASP.NET在单击事件期间多次更新标签文本

3okqufwl  于 9个月前  发布在  .NET
关注(0)|答案(3)|浏览(134)

我有一个简单的应用程序,是用C#编写的使用VS2012,在Web表单应用程序。
有一个按钮开始处理,我想给予用户更新的进程,通过把一些文本在标签。我想我可以把标签控件在一个UpdatePanel,然后在按钮点击事件触发更改标签文本通过调用UpdatePanel Update()方法。然而,标签文本更改的唯一时间是在单击事件完成之后,并且标签文本显示为“处理完成”。
这是我的aspx文件:

<asp:UpdatePanel ID="ProgressUpdatePanel" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="False">
      <ContentTemplate>
      <div style="height: 81px">
           <asp:Label ID="lblCaption" runat="server" CssClass="label" Visible="true"></asp:Label> 
      </div>
      </ContentTemplate>
</asp:UpdatePanel>      
<asp:Button ID="btnCreateFiles" runat="server" Text="Create Labels Files" Width="206px" CssClass="label" Height="37px" OnClick="btnCreateFiles_Click" style="float: right"/>

字符串
这是一个简单的例子。

protected void btnCreateFiles_Click(object sender, EventArgs e)
        {
            //Do some stuff

            //Show the message that the application is processing
            lblCaption.Text = "Processing...updating label data";
            ProgressUpdatePanel.ContentTemplateContainer.Controls.Add(lblCaption);
            ProgressUpdatePanel.Update();      

            //Call to database to insert labels
            //Call to database to get the labels 

            lblCaption.Text = "Processing...creating label files.";
            ProgressUpdatePanel.ContentTemplateContainer.Controls.Add(lblCaption);
            ProgressUpdatePanel.Update();   

           //Create Text files from data

            lblCaption.Text = "Processing...updating label counts.";
            ProgressUpdatePanel.ContentTemplateContainer.Controls.Add(lblCaption);
            ProgressUpdatePanel.Update();   

            //Database call to insert count records
            //Create file of count records

            lblCaption.Text = "Processing...completed.";
            ProgressUpdatePanel.ContentTemplateContainer.Controls.Add(lblCaption);
            ProgressUpdatePanel.Update();   
        }


唯一显示的文本是最后一次更新.“处理中.完成。”
为什么其他更新不会触发标签文本的更改?

附加要求

我已经改变了我的要求一点点。而不是多次更新。我在进度模板中添加了一个标签,在处理开始时显示一条初始消息,然后在处理完成时使用lblCaption标签显示另一条消息。然而,当第二次单击按钮时,两条消息都显示:“请等待文件创建......”和“处理......完成”。
如何将进度模板中的文本重置为仅显示“请等待.”消息而不显示“正在处理.完成”消息?
这就是现在的aspx文件:

<asp:UpdatePanel ID="ProgressUpdatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:UpdateProgress ID="updProgress" runat="server"   AssociatedUpdatePanelID="ProgressUpdatePanel" DisplayAfter="1">
 <ProgressTemplate>                           
     <asp:Label ID="lblProgress" runat="server" CssClass="label" Text="Please wait while the label files are being created..."></asp:Label>   
</ProgressTemplate>
</asp:UpdateProgress>
<asp:Label ID="lblCaption" runat="server" CssClass="label" Visible="true"></asp:Label>
                        
<asp:Button ID="btnCreateFiles" runat="server" Text="Create Labels Files" Width="206px" CssClass="label" Height="37px" OnClick="btnCreateFiles_Click" style="float: right"/>                 
</ContentTemplate>
</asp:UpdatePanel>


现在按钮事件处理程序方法看起来像这样:

protected void btnCreateFiles_Click(object sender, EventArgs e)
            {
                //All of the processing is done here...
    
                //This works correctly the first time a user click the button
                //But the second time, this text remains and the 'Please wait...' text from the lblProgress label
                // is added above this text.
                ProgressUpdatePanel.ContentTemplateContainer.Controls.Add(lblCaption);
                ProgressUpdatePanel.Update();   
                lblCaption.Text = "Processing...completed.";
               
            }

UPDATE我已经尝试了以下方法来清除标签中的文本,lblCaption。我已经遍历了代码,并执行了此代码,因为标签文本未被清除。

我尝试在Page_Load()方法中重置标签,如下所示:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtBoxEnvironment.Text = CurrentEnvironment;
                DAL.setConnectionString(CurrentEnvironment);
            }
            ProgressUpdatePanel.ContentTemplateContainer.Controls.Add(lblCaption);
            ProgressUpdatePanel.Update();   
            lblCaption.Text = "";
        }


我已经删除了ProgressUpatePanel方法,并尝试只设置标签文本,它不是重置。
我错过了什么?

5kgi1eie

5kgi1eie1#

UpdatePanel上的Update方法不按您预期的方式工作。
如果必须执行服务器代码以确定是否应更新UpdatePanel控件,请调用Update方法。
如果你想模拟一个进程,你可以通过调用隐藏按钮来触发多个部分回发。每个按钮将执行另一个步骤,并通过JavaScript调用下一个按钮单击。这可能容易出错,效率低下。但这是一种方法。另一种方法是使用jQuery,(仍然进行几次调用)使用$. ausage,在成功回调中调用下一步。
以你的代码为例,这是你想要的一个基本的工作示例-我理解的方式:

加价

<asp:UpdatePanel ID="ProgressUpdatePanel" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Button ID="btnCreateFiles" runat="server" Text="Go" OnClick="btnCreateFiles_Click" />
        <asp:Label ID="lblCaption" runat="server" Text="" />
    </ContentTemplate>
</asp:UpdatePanel>

<script type="text/javascript">
    $(document).ready(function () {
        bindButton();
    });
    function bindButton() {
        $('#<%=btnCreateFiles.ClientID%>').on('click', function () {
            $('#<%=lblCaption.ClientID%>').html('Please Wait...');
    });
}
</script>

字符串

代码隐藏

protected void Page_Load(object sender, EventArgs e) {
        if (!IsPostBack) {
            //txtBoxEnvironment.Text = CurrentEnvironment;
            //DAL.setConnectionString(CurrentEnvironment);
        }
        ProgressUpdatePanel.ContentTemplateContainer.Controls.Add(lblCaption);
        ProgressUpdatePanel.Update();
        lblCaption.Text = "";

    }

    protected void btnCreateFiles_Click(object sender, EventArgs e) {
        //All of the processing is done here...

        //This works correctly the first time a user click the button
        //But the second time, this text remains and the 'Please wait...' text from the lblProgress label
        // is added above this text.
        ProgressUpdatePanel.ContentTemplateContainer.Controls.Add(lblCaption);
        ProgressUpdatePanel.Update();
        lblCaption.Text = "Processing...completed.";
        System.Threading.Thread.Sleep(1000);

        ScriptManager.RegisterClientScriptBlock(Page, typeof(string), "bindButton", "bindButton();", true);
    }

gmol1639

gmol16392#

我不认为你能在不写一些额外代码的情况下做你想做的事情。UpdatePanel不会提供对UI的多次更新。
这篇文章可能会帮助你实现这一目标:http://encosia.com/easy-incremental-status-updates-for-long-requests/
这篇文章有点旧,但仍然可能有用:http://msdn.microsoft.com/en-us/magazine/cc163393.aspx

xdnvmnnf

xdnvmnnf3#

我同意Rick S的观点,我们真的需要看到处理代码;你在这里的代码会更喜欢处理太快而看不到其他消息。这会导致你看到的问题,其中唯一显示的消息是最后一条消息。

相关问题