2

In the following code, the asp does not enter the action listener (Button1_Click). Can anyone help?

protected void Project_Click(object sender, EventArgs e)
    {
        Courses.ActiveViewIndex = 0;

        String ConnStr = ConfigurationManager.ConnectionStrings["MyRuMoR"].ToString();
        SqlConnection conn = new SqlConnection(ConnStr);
        conn.Open();
        SqlCommand cmd = new SqlCommand("ViewProjects", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@course_code", "PHYS305");

        SqlDataReader rdr = cmd.ExecuteReader();

        int c = 1;

        while (rdr.Read())
        {
            String name = rdr.GetString(rdr.GetOrdinal("name"));
            int p_id = rdr.GetInt32(rdr.GetOrdinal("p_id"));

            LinkButton lb = new LinkButton();
            lb.ID = p_id.ToString();

            lb.Text = "Project " + c + " is: " + name;
            form1.Controls.Add(lb);

            lb.Click += new EventHandler(this.Button1_Click);

            c++;
        }
        conn.Close();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write("Hello");
    }
2
  • You mean event handler. Action listener is a Java concept.
    – Oded
    Commented Dec 25, 2011 at 12:24
  • 2
    You better change your logic and use Repeater instead, binding its data source in the Project_Click method. Commented Dec 25, 2011 at 12:28

3 Answers 3

4

You need to read up on the ASP.NET page lifecycle.

When it comes to dynamic controls, you need to recreate them on postback, or they will not exist and the attached event handler will not fire.

Dynamic contorls are best created on the PreInit event of the page.

As described here:

protected virtual void OnPreInit(EventArgs e)
{
   base.OnPreInit(e);
   //your code
}
3
  • not in the init but PreInit. msdn.microsoft.com/en-us/library/ms178472.aspx
    – Royi Namir
    Commented Dec 25, 2011 at 13:55
  • thank you for your reply but could you please illustrate more? I don't really get what is the PreInit stage of the page.
    – RuMoR
    Commented Dec 25, 2011 at 15:05
  • I'm new to asp.net and this my first project, so i'm a little bit lost and don't understand how to add a dynamic control on the PreInit event of the page.
    – RuMoR
    Commented Dec 25, 2011 at 15:15
2

Yes not just understanding dynamic controls you need to TRULEY understnad them This is one of the best article on them by a ASP.NET team memeber.

TRULY UNDERSTANDING DYNAMIC CONTROLS

0

You have to put all or the part of your code that creates dynamic controls inside the Pre_Init event of page life cycle, something like this

protected void Page_PreInit(object sender, EventArgs e)
{
   LinkButton lb = new LinkButton();
   lb.ID = p_id.ToString();

   lb.Text = "Project " + c + " is: " + name;
   form1.Controls.Add(lb);

   lb.Click += new EventHandler(this.Button1_Click);
}

Then only it will achieve what you want.

Not the answer you're looking for? Browse other questions tagged or ask your own question.