标签值在运行时不显示 [英] Value of label does not display at run time

查看:76
本文介绍了标签值在运行时不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在aspx页面的页面加载上执行asyncallback.

从EndInvoke的回调返回的值分配给标签字段.但是问题是我在运行应用程序时没有获得价值.

我也用断点检查了这一点.我无法确定问题的原因.
我已经尝试过使用AJAX updatepanel作为标签-仍然无法正常工作.

这是代码:

ASPX

I''m trying to do an asyncallback on page load of an aspx page.

The value returned from the callback of EndInvoke is assigned to a label field. But the problem is I''m not getting the value when running my application.

I''ve checked this with a break point too. I am unable to determine the cause of the problem.

I''ve tried with an AJAX updatepanel for the label - still not working.

Here is the code:

ASPX

<asp:UpdatePanel ID="updLbl" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                You are in
                <asp:Label ID="lblPos" runat="server"></asp:Label><sup>th</sup> Position
            </ContentTemplate>
        </asp:UpdatePanel>



C#



C#

public partial class _Default : System.Web.UI.Page 
{
    int pos;
    public delegate int  dlgtcaller(out int pos);
    protected void Page_Load(object sender, EventArgs e)
    {
        Async objCls = new Async();
        dlgtcaller call = new dlgtcaller(objCls.AsyncMethod);
        IAsyncResult result = call.BeginInvoke(out pos, new AsyncCallback(cbMethod), null);
    }
    public void cbMethod(System.IAsyncResult ar)
    {
        AsyncResult result = (AsyncResult)ar;
        dlgtcaller caller = (dlgtcaller)result.AsyncDelegate;
        int returnValue = caller.EndInvoke(out pos, result);
        lblPos.Text = pos.ToString();
    }
}

public class Async
{
     public int AsyncMethod(out int pos)
    {
        string SQL = "SELECT count(id) FROM test";
        string ConnectionString = "server=DEV-PC4\\SQLEXPRESS;Initial Catalog=Pradeep;User ID=******;Password=******;";
        SqlConnection con = new SqlConnection(ConnectionString);
        SqlCommand cmd = new SqlCommand(SQL, con);
        con.Open();
        pos = Convert.ToInt32(cmd.ExecuteScalar());
        return pos;
    }
}





但是我所需要的只是将值放在 lblPos.text 中,该值应该由回调方法返回.有什么办法..:confused:



预先感谢..





But all i need is to put the value in lblPos.text that should be returned by the callback method. Is there any way..:confused:



Thanks in advance..

推荐答案

对不起,但是您已经完全倒退了.

在asp.net中,必须来自客户端的请求,然后由服务器处理,然后返回结果.

如果在服务器上使用BeginInvoke,则在调用回调方法时,该请求很可能已经完成.这就是为什么设置lblPos.Text不执行任何操作的原因.结果已发送到客户端.

另一方面,UpdatePanel是一种更新页面的部分区域的方法.通过向服务器发送请求并处理发送回的结果,它也可以工作.

如果您编辑问题并确切说出您要实现的目标,我们也许可以提供替代解决方案.

尼克
Sorry, but you''ve got this completely backwards.

In asp.net, a request must come from the client which is then handled by the server and the result is returned.

If you use BeginInvoke on the server, the request will most probably already have been completed by the time the callback method is called. That is why setting lblPos.Text doesn''t do anything. The result has already been sent to the client.

An UpdatePanel, on the other hand, is a way to update a partial area of your page. It too works by sending a request to the server and handling the result that is sent back.

If you edit your question and say exactly what you''re trying to achieve, we may be able to offer an alternative solution.

Nick


谢谢尼克..

但是我所需要的只是将值放在 lblPos.text 中,该值应该由回调方法返回.有什么办法..:confused:
Thankyou Nick..

But all i need is to put the value in lblPos.text that should be returned by the callback method. Is there any way..:confused:


为什么要尝试异步运行SQL?

这基本上是行不通的.

尼克
Why are you trying to run the SQL asynchronously?

This basically won''t work.

Nick


这篇关于标签值在运行时不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆