Solved

Dynamic Event Handler

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
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 …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now