1

我的Page_Load事件中有代码有条件地设置一个Session[]变量,然后在该代码之后使用该变量的值注入HTML页面。它工作正常,但我想移动HTML生成代码以控件事件之后运行,因为我有一个也可以影响这个变量的按钮。所以我将第二部分移到了Page_PreRender函数中,它停止了工作。

这是有效的代码。"Good"它甚至在事件中添加标签PreRender,这意味着Session[]变量工作正常:

protected void Page_Load(object sender, EventArgs e)
{
    using (OpenIdRelyingParty openid = new OpenIdRelyingParty())
    {
        var response = openid.GetResponse();
        if (response != null)
        {
            switch (response.Status)
            {
                case AuthenticationStatus.Authenticated:
                    Session["loginId"] = response.ClaimedIdentifier;
                    break;
            }
        }
    }
    if (Session["loginId"] != null)
    {
        Label l = new Label();
        l.Text = "Welcome " + Session["loginId"];
        loginPH.Controls.Add(l);

        Button b = new Button();
        b.Text = "Logout";
        b.Click += new EventHandler(logout_Click);
        loginPH.Controls.Add(b);
    }
    else
    {
        Button b = new Button();
        b.Text = "Log in with Google";
        b.Click += new EventHandler(loginGoogle_Click);
        loginPH.Controls.Add(b);
    }
}

void Page_PreRender()
{
    string s;
    if (Session["loginId"] != null)
        s = "Good";
    else
        s = "NULL";
    loginPH.Controls.Add(new Label { Text = s });
}

这是重构的代码以在PreRender事件中添加控件(我删除了之前的测试占位符)。它应该工作相同,但没有。它总是添加登录按钮:

protected void Page_Load(object sender, EventArgs e)
{
    using (OpenIdRelyingParty openid = new OpenIdRelyingParty())
    {
        var response = openid.GetResponse();
        if (response != null)
        {
            switch (response.Status)
            {
                case AuthenticationStatus.Authenticated:
                    Session["loginId"] = response.ClaimedIdentifier;
                    break;
            }
        }
    }
}

void Page_PreRender()
{
    if (Session["loginId"] != null)
    {
        Label l = new Label();
        l.Text = "Welcome " + Session["loginId"];
        loginPH.Controls.Add(l);

        Button b = new Button();
        b.Text = "Logout";
        b.Click += new EventHandler(logout_Click);
        loginPH.Controls.Add(b);
    }
    else
    {
        Button b = new Button();
        b.Text = "Log in with Google";
        b.Click += new EventHandler(loginGoogle_Click);
        loginPH.Controls.Add(b);
    }
}

有任何想法吗?

4

1 回答 1

2

我认为您的问题与您尝试在 PreRender 中连接事件处理程序有关。在回发时,直到处理了 click 事件之后才会创建按钮,这意味着该事件将被忽略。

尝试用页面范围声明按钮并在 Page_Init 中初始化它们:

protected Button a;
protected Button b;

void Page_Init(object sender, EventArgs e)
{
    a = new Button();
    a.Text = "Logout";
    a.Click += new EventHandler(logout_Click);

    b = new Button();
    b.Text = "Log in with Google";
    b.Click += new EventHandler(loginGoogle_Click);
}

然后在 PreRender 中,您可以决定使用哪个按钮:

void Page_PreRender(object sender, EventArgs e)
{
    if (Session["loginId"] != null)
    {
        Label l = new Label();
        l.Text = "Welcome " + Session["loginId"];
        loginPH.Controls.Add(l);

        loginPH.Controls.Add(a);
    }
    else
    {
        loginPH.Controls.Add(b);
    }
}
于 2013-06-18T11:21:52.057 回答