Solved

Dynamic Event Handler

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

691 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