Asp.net 寻呼机单击事件未触发,其背后的逻辑是什么?

发布于 2024-08-16 00:38:46 字数 1777 浏览 6 评论 0原文

我不明白的是,我有一个 pager_Create 函数,就像那样

public void createPager()
{   
    Div_Pager.Controls.Clear();

    ImageButton left = new ImageButton(); left.ID = "leftButton";// +new Random().Next();
    left.Click+=new ImageClickEventHandler(pager_Left_Click);
    left.ImageUrl = "http://" + Request.ServerVariables["SERVER_NAME"] + ":" + Request.ServerVariables["SERVER_PORT"] + "/Teknik_raporlar/left.bmp";

    ImageButton right = new ImageButton(); right.ID = "rightButton";// +new Random().Next();
    right.Click+=new ImageClickEventHandler(pager_Right_Click);
    right.ImageUrl = "http://" + Request.ServerVariables["SERVER_NAME"] + ":" + Request.ServerVariables["SERVER_PORT"] + "/Teknik_raporlar/right.bmp";

    Div_Pager.Controls.Add(left);
    Div_Pager.Controls.Add(right);

    if (int.Parse(ViewState["NOP"].ToString()) <= 1) // Number of Pages
    {
        Div_Pager.Visible = false;
    }
    else
    {
        Div_Pager.Visible = true;
        if (int.Parse(ViewState["CurrentPage"].ToString()) <= 1)
        {
            left.Visible = false;
        }
        if (int.Parse(ViewState["CurrentPage"].ToString()) >= int.Parse(ViewState["NOP"].ToString()))
        {
            right.Visible = false;
        }
    }
}


protected void pager_Left_Click(object sender, EventArgs e) 
{
    ViewState["CurrentPage"] = int.Parse(ViewState["CurrentPage"].ToString()) - 1;

}
protected void pager_Right_Click(object sender, EventArgs e)
{
    ViewState["CurrentPage"] = int.Parse(ViewState["CurrentPage"].ToString()) + 1;

}

,我不明白的是,为什么这个寻呼机按钮不触发?我的意思是每次创建寻呼机时我都会创建相同的按钮。那么怎么可能呢。它与页面渲染有什么关系。我的简化代码是这样的:

override Render()
{
 function1()
}
Page_Load()
{
 function1()
}
funtion1()
{
 createpager()
}

What I don't understand is, I have a pager_Create function, it is like that

public void createPager()
{   
    Div_Pager.Controls.Clear();

    ImageButton left = new ImageButton(); left.ID = "leftButton";// +new Random().Next();
    left.Click+=new ImageClickEventHandler(pager_Left_Click);
    left.ImageUrl = "http://" + Request.ServerVariables["SERVER_NAME"] + ":" + Request.ServerVariables["SERVER_PORT"] + "/Teknik_raporlar/left.bmp";

    ImageButton right = new ImageButton(); right.ID = "rightButton";// +new Random().Next();
    right.Click+=new ImageClickEventHandler(pager_Right_Click);
    right.ImageUrl = "http://" + Request.ServerVariables["SERVER_NAME"] + ":" + Request.ServerVariables["SERVER_PORT"] + "/Teknik_raporlar/right.bmp";

    Div_Pager.Controls.Add(left);
    Div_Pager.Controls.Add(right);

    if (int.Parse(ViewState["NOP"].ToString()) <= 1) // Number of Pages
    {
        Div_Pager.Visible = false;
    }
    else
    {
        Div_Pager.Visible = true;
        if (int.Parse(ViewState["CurrentPage"].ToString()) <= 1)
        {
            left.Visible = false;
        }
        if (int.Parse(ViewState["CurrentPage"].ToString()) >= int.Parse(ViewState["NOP"].ToString()))
        {
            right.Visible = false;
        }
    }
}


protected void pager_Left_Click(object sender, EventArgs e) 
{
    ViewState["CurrentPage"] = int.Parse(ViewState["CurrentPage"].ToString()) - 1;

}
protected void pager_Right_Click(object sender, EventArgs e)
{
    ViewState["CurrentPage"] = int.Parse(ViewState["CurrentPage"].ToString()) + 1;

}

and what I don't understand is this, how come this pagers buttons not firing? I mean I am creating the same buttons everytime the pager is created. So how is it possible. What is its relation about page rendering. My simplified code is like this:

override Render()
{
 function1()
}
Page_Load()
{
 function1()
}
funtion1()
{
 createpager()
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的背包 2024-08-23 00:38:46

你正在创造&重新创建动态控件,对于普通控件事件绑定有效
如果完成的是 Page_Load() ,但不适用于动态创建的控件。

您需要在 PreInit 中创建动态控件(或至少 OnInit() 并在其中附加事件本身。动态控件需要在每次发回的

按钮 ID 都需要是新的,
就像 left.ID = "leftButton"+random_no.ToString();

这是动态创建控件的最佳实践。

http://www.singingeels.com/Articles/Dynamically_Created_Controls_in_ASPNET.aspx

它的长文,但请继续。

You are creating & recreating dynamic controls , for normal controls event bindings works
if done is Page_Load() , but not for dynamically created controls.

you need to create dynamic controls in PreInit (or at least OnInit() & attach events there itself . Dynamic controls needs to be recreated on every post back.

Also u button ids need to be new every time,
like left.ID = "leftButton"+random_no.ToString();

Here is a best practices for creating controls dynamically.

http://www.singingeels.com/Articles/Dynamically_Created_Controls_in_ASPNET.aspx

Its long article , but plz go thru.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文