C#委托,标记文本不变

时间:2016-01-05 21:15:12

标签: c# asp.net delegates

我已创建此委托,以便每次单击按钮时标签文本中的文本都应更改,但由于某种原因,这不起作用且标签的文本不会更改。

这是我的aspx页面:

CREATE TABLE #inq(
InquiryID INT,
SubID INT
)

INSERT INTO #inq 
SELECT DISTINCT InquiryID, SUbId 
FROM CRM_Inquiries 
WHERE LName = 'Poe' AND SubID IS NOT NULL


DECLARE @totalCount INT
SELECT @totalCount = COUNT(*) FROM #inq 

PRINT @totalCount

WHILE(@totalCount > 0)
BEGIN

DECLARE @subId varchar(250)
DECLARE @inquiryID INT

SELECT TOP 1 @subId = subID, @inquiryID = inquiryID FROM #inq

PRINT 'SubID = ' + @subID
PRINT 'InquiryID= ' + CAST(@inquiryId AS VARCHAR(MAX))

UPDATE CRM_Inquiries 
SET FName = (SELECT SubFirstName FROM ICS_Subscribers WHERE SubID = @subId),
LName = (SELECT SubLastName FROM ICS_Subscribers WHERE SubID = @subId)
WHERE InquiryID = @inquiryID

DELETE FROM #inq WHERE InquiryID = @inquiryID


SELECT @totalCount = COUNT(*) FROM #inq 
PRINT @totalCount
END


DROP TABLE #inq

这是我的aspx.cs页面

<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnFeed" OnClick="btnFeed_Click" runat="server" Text="Button" />
        <asp:Label ID="lblRaceResults" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>

我很确定我的代理编码正确,因为我取消注释行

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebProgramming3.Week_3
{
    public partial class Exercise1 : System.Web.UI.Page
    {
        //only for testing
        static Person test_person;
        static Person person2;
        static Person person3;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                test_person = new Person("Neil");
                person2 = new Person("2");
                person3 = new Person("3");
                test_person.OnFullyFed += Test_person_OnFullyFed;
                person2.OnFullyFed += Test_person_OnFullyFed;
                person3.OnFullyFed += Test_person_OnFullyFed;
            }
        }

        private void Test_person_OnFullyFed(string message)
        {
            // HttpContext.Current.Response.Write(message + " is full");
            lblRaceResults.Text = message; //<--This is the label where text will not change
        }

        protected void btnFeed_Click(object sender, EventArgs e)
        {
            test_person.Feed(1);
            person2.Feed(2);
            person3.Feed(3);
        }
    }

    public delegate void StringDelegate(string message);

    public class Person
    {
        public string Name { get; set; }
        public int Hunger { get; set; }

        public event StringDelegate OnFullyFed;

        public Person(string name)
        {
            Name = name;
            Hunger = 3;
        }

        public void Feed(int amount)
        {
            if(Hunger > 0)
            {
                Hunger -= amount;
                if(Hunger <= 0)
                {
                    Hunger = 0;

                    //this person is full, raise an event
                    if (OnFullyFed != null)
                        OnFullyFed(Name);
                }
            }
        }

    }
}

每次单击按钮

时,我都会收到一条消息

1 个答案:

答案 0 :(得分:0)

  

这是因为页面生命周期已完成并且页面已呈现   /在线程完成更新之前发送到浏览器   控制。在调试期间,您可以看到线程完成其工作   但正在改变已经发送到浏览器的标签。

从加载事件中删除!IsPostBack应该可以解决问题并重新加载控件。当然,您可以使用其他选项来解决此问题,例如更新面板和自动刷新。

 protected void Page_Load(object sender, EventArgs e)
        {
                test_person = new Person("Neil");
                person2 = new Person("2");
                person3 = new Person("3");
                test_person.OnFullyFed += Test_person_OnFullyFed;
                person2.OnFullyFed += Test_person_OnFullyFed;
                person3.OnFullyFed += Test_person_OnFullyFed;

        }