Solved

Dynamic Event Handler

Posted on 2006-07-21
3
1,228 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
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 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 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