?
Solved

Dynamic Event Handler

Posted on 2006-07-21
3
Medium Priority
?
1,237 Views
Last Modified: 2008-02-01
Hi,

Can anyone pease tell me why this Click event handler is not firing.  I create the button dynamically and then set the click event handler. Here is my code.

protected void DlistQuantity_SelectedIndexChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < Convert.ToInt32(DlistQuantity.SelectedValue); i++)
        {
            HtmlGenericControl NewContainer = new HtmlGenericControl("DIV");
            FileUpload MyUpload = new FileUpload();
            MyUpload.ID = "FU_" + (i + 1).ToString();
            Label MyLabel = new Label();
            MyLabel.Text = "Description";
            TextBox MyText = new TextBox();
            MyText.ID = "TXT_" + (i+1).ToString();
            NewContainer.Controls.Add(MyUpload);
            NewContainer.Controls.Add(MyLabel);
            NewContainer.Controls.Add(MyText);
            images.Controls.Add(NewContainer);
        }
        HtmlGenericControl ConfirmDiv = new HtmlGenericControl("DIV");
        Button SendButton = new Button();
        SendButton.Text = "Upload Images";
        ConfirmDiv.Controls.Add(SendButton);
        images.Controls.Add(ConfirmDiv);
        SendButton.Click += new EventHandler(SendButton_Click);
    }
0
Comment
Question by:REA_ANDREW
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 20

Author Comment

by:REA_ANDREW
ID: 17154815
sorry and this  is the function which i set with the button

protected void SendButton_Click(object sender, EventArgs e)
    {
        //throw new Exception("The method or operation is not implemented.");
        Response.Write("Triggerd");
        foreach (FileUpload fu in images.Controls)
        {
            string[] a = fu.PostedFile.FileName.Split('.');
            string extension = a[a.Length - 1].ToString();
            string filename = fu.ID + "_orig." + extension;
            fu.PostedFile.SaveAs(Server.MapPath("images/") + filename);
        }
    }
0
 
LVL 17

Accepted Solution

by:
akshah123 earned 2000 total points
ID: 17155144
Try replacing following
SendButton.Click += new EventHandler(SendButton_Click);

with

SendButton.Click += new System.EventHandler(this.SendButton_Click);

also, add the click event before adding the button to the Div controlls..

protected void DlistQuantity_SelectedIndexChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < Convert.ToInt32(DlistQuantity.SelectedValue); i++)
        {
            HtmlGenericControl NewContainer = new HtmlGenericControl("DIV");
            FileUpload MyUpload = new FileUpload();
            MyUpload.ID = "FU_" + (i + 1).ToString();
            Label MyLabel = new Label();
            MyLabel.Text = "Description";
            TextBox MyText = new TextBox();
            MyText.ID = "TXT_" + (i+1).ToString();
            NewContainer.Controls.Add(MyUpload);
            NewContainer.Controls.Add(MyLabel);
            NewContainer.Controls.Add(MyText);
            images.Controls.Add(NewContainer);
        }
        HtmlGenericControl ConfirmDiv = new HtmlGenericControl("DIV");
        Button SendButton = new Button();
        SendButton.Text = "Upload Images";
        SendButton.Click += new System.EventHandler(this.SendButton_Click);
        ConfirmDiv.Controls.Add(SendButton);
        images.Controls.Add(ConfirmDiv);

    }
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17160870
Is there a post-back?  If not, you won't get the Click event.

Bob
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month12 days, 14 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question