473,396 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Dynamic Controls in asp.net

I am having some issues in dynamically created controls. My scenario is when the user selects a dropdown values, it should dynamically create image buttons, where URL is from database. Then when the user click’s any one of the image, it should create the same image in the placeholder. The problem is when I give the on click event in the code behind its not working and I know it has to be initialized in Page init, but since the creation is based on Selectedindexchange, I don’t know how to proceed further. When I click the image, the page is post back and the images are gone. Please see my below code


Expand|Select|Wrap|Line Numbers
  1. protected void Selectedindex_loadObjTemp(object sender, System.EventArgs e)
  2. {
  3.  
  4.     PlaceHolder PlaceHolder1 = new PlaceHolder();
  5.  
  6.       // Get the number of labels to create.
  7.      String objTyoe = ddlObjTemp.SelectedValue;
  8.      String strSQl = "select * from ObjectTemplates where type=" +    objTyoe;
  9.      DataTable dt = FillDataTable(strSQl);
  10.      Table addTable = new Table();   
  11.  
  12.      addTable.Controls.Clear();
  13.      if (dt.Rows.Count > 0)
  14.      {
  15.  
  16.         int datavount = 0;
  17.          for (int i = 0; i <= datavount; i++)
  18.              {
  19.                  TableRow newRow = new TableRow();                  
  20.  
  21.                  for (int j = 0; j <= 3; j++)
  22.                  {
  23.                      TableCell newcell = new TableCell();
  24.                      System.Web.UI.WebControls.ImageButton myImage = new System.Web.UI.WebControls.ImageButton();
  25.                      myImage.ID = "imgv" + dt.Rows[datavount]["id"];
  26.                      myImage.Attributes.Add("runat", "server");
  27.                      switch (objTyoe)
  28.                      {
  29.                          case "1":
  30.                              myImage.ImageUrl = "~/Images/streets/" + dt.Rows[datavount]["fileName"];
  31.                              break;
  32.                          case "2":
  33.                              myImage.ImageUrl = "~/Images/Street Templates/" + dt.Rows[datavount]["fileName"];
  34.  
  35.                              break;
  36.                          case "5":
  37.                              myImage.ImageUrl = "~/Images/vehicles/" + dt.Rows[datavount]["fileName"];
  38.                              break;
  39.                          case "6":
  40.                              myImage.ImageUrl = "~/Images/signs/" + dt.Rows[datavount]["fileName"];
  41.  
  42.                              break;
  43.                          case "7":
  44.                              myImage.ImageUrl = "~/Images/animals/" + dt.Rows[datavount]["fileName"];
  45.  
  46.                              break;
  47.                          case "8":
  48.                              myImage.ImageUrl = "~/Images/labels/" + dt.Rows[datavount]["fileName"];
  49.                              break;
  50.                          case "99":
  51.                              myImage.ImageUrl = "~/Images/other/" + dt.Rows[datavount]["fileName"];
  52.  
  53.                              break;
  54.  
  55.                          case "9":
  56.                              myImage.ImageUrl = "~/Images/ghosts/" + dt.Rows[datavount]["fileName"];
  57.  
  58.                              break;
  59.                      }
  60.  
  61.  
  62.                      Panel newpanel = new Panel();
  63.                      newpanel.Controls.Add(myImage);
  64.                      newcell.Controls.Add(newpanel);
  65.                      myImage.Click += new System.Web.UI.ImageClickEventHandler(
  66. this.btnCreate_Click);
  67.  
  68.                      datavount++;
  69.                      newRow.Cells.Add(newcell);
  70.                      if (datavount >= dt.Rows.Count)
  71.                          break;
  72.                  }
  73.                  if (datavount >= dt.Rows.Count)
  74.                      break;
  75.                  addTable.Rows.Add(newRow);
  76.  
  77.              }
  78.          mainPanel.Controls.Add(addTable);
  79.  
  80. }
  81. }
  82. private void btnCreate_Click(object sender, EventArgs e)
  83. {
  84.     showmsg(sender.ToString() + e.ToString());
  85.  
  86. }
Thanks
Sep 16 '10 #1
3 1475
Frinavale
9,735 Expert Mod 8TB
Store the number of image buttons created in a HiddenField. A HiddenField can be accessed in the Page Init event (using Request.Params("idOfHiddenField") to retrieve it). That way you can re-create the image buttons in the correct place, the Page Init event. Also be sure to specify the method that handles the image button click event in the Page Init event.

-Frinny
Sep 16 '10 #2
Frinny ,
Thanks for your reply.Hiddenfiled didn't worked for and i used session variable and able to create in page_init.

Thanks again.
Sep 17 '10 #3
Frinavale
9,735 Expert Mod 8TB
Session works too :)
It's a little trickier using the hidden field but I've got it to work in the past.

Glad you got it to work.

-Frinny
Sep 20 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Will | last post by:
Hi all. I'm learning VB.Net and am developing a WinForms app. I'm trying to make an app that I will use to scan in one or more than on image. I want to use a tabbed interface to hold each image....
2
by: theComputer7 | last post by:
I cut down the code to make this half way understandable... I have read Data Grid girls caution about over use of dynamic controls. I truly believe what I am doing requires dynamically inserted...
1
by: hybrid | last post by:
I have problems in understanding the behavior of the events triggered by dynamically created controls over a webform. Could you help me? In a webform, I have a static PlaceHolder PH containing...
2
by: Dave Williamson | last post by:
When a ASPX page is created with dynamic controls based on what the user is doing the programmer must recreate the dynamic controls again on PostBack in the Page_Load so that it's events are wired...
3
by: Leo J. Hart IV | last post by:
OK, here's another question for the experts: I am building a multi-step (3 steps actually) form using a panel for each step and hiding/displaying the appropriate panel/panels depending on which...
5
by: mytestemailaccount | last post by:
Hi, Hope you can help. I am relatively new to all this but would appreciate the groups help. The scenario: I am c# and asp.net to create a web application. The web page contains a user...
1
by: pbb | last post by:
I'm creating a set of dynamic controls on a webpage by calling my BuildControls sub in the Page_Init sub. I recreate the controls by calling the BuildControls sub in the LoadViewState override...
3
by: WebBuilder451 | last post by:
I have a series of dynamic link buttons created based upon a datareader. I've added a click event and it calls the sub ok: example: "while loop through the reader" Dim ltrCtrl As New...
1
by: Diffident | last post by:
Hello All, I am trying to add dynamic controls onto my page and here is how I am doing that. I have a page which has a button called as "AddMoreControls" and in this button's event handler I...
9
by: Tarscher | last post by:
hi all, I have this seemingly simple problem. I have lost a lot of time on it though. When a user selects a value from a dropdownlist (static control) a dynamic control is generated. I have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.