views:

155

answers:

1

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()
}
+1  A: 

Hi

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%5FCreated%5FControls%5Fin%5FASPNET.aspx

Its long article , but plz go thru.

Ajax2020