Solved

Dynamic Event Handler

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to create an MVC5, Razor Widget 2 38
ASP.NET (VB) return a record 2 52
asp Google Map 2 64
Using sample Autorize.net c# simple example 1 29
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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 …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 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