473,387 Members | 1,779 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,387 software developers and data experts.

Dynamically Load DropDownList Into Page

Not enough info on the user control part. You only have how to load the user control itself, dynamically, from the host page, which only in rare circumstances would you need to do (you could always redirect a user to another page, instead). You don't say how one might load something within that user control dynamically, how to get handles to the form, etc. In my user control's code-behind I put
Expand|Select|Wrap|Line Numbers
  1. public HtmlForm aspnetForm;
and tried
Expand|Select|Wrap|Line Numbers
  1. aspnetForm.Controls.Add(myDropDown);
after creating "myDropDown" as a DropDownList object and adding some items to it and it gives an error: "Object reference not set to an instance of an object".
Mar 3 '12 #1
4 2239
Frinavale
9,735 Expert Mod 8TB
@navyjax2,

I'm not sure what you are trying to do with the HtmlForm.
You should be loading your controls in an ASP.NET page, not an HTML Form.

-Frinny
Mar 5 '12 #2
Frinavale
9,735 Expert Mod 8TB
Well you cannot load ASP.NET controls into an HTML form. It doesn't make sense. ASP.NET pages are executed to generate HTML that is sent to the browser. You have some control over the HTML generated.

It really doesn't make sense to add an ASP.NET control to a HTML form.

It makes a lot more sense to take the HTML generated by the ASP.NET control and add it to the HTML. You would do this by overwriting the Render code. You would do this if you were creating a server-control (as apposed to a user control)...but even then it would be rare to have a reason to overwrite the Render code.

Does this make sense?

I don't understand why the DropDownList wouldn't dynamically load in the page itself? It's pretty straightforward...it would help if you showed what you were trying to do (if you were adding it to the HTML form, then it should not work)

I cannot come up with any ideas because I cannot see any broken code posted ;)

-Frinny
Mar 6 '12 #3
Yeah, sorry, it's on a computer at my other office, but I think I remember it pretty clearly as I burned out a Saturday on it (this past weekend).

What I was trying to do with the HtmlForm was something like was done in a blog posted here, just with adding dropdown items from SharePoint list items instead of buttons from XML items, but the concept is the exact same, so it seemed there was a proof of concept out there, already:

Expand|Select|Wrap|Line Numbers
  1. private void Page_Load(object sender, System.EventArgs e)
  2.     XPathDocument doc = new XPathDocument(Server.MapPath("xmlfile1.xml"));
  3.     XslTransform trans = new XslTransform();
  4.     trans.Load(Server.MapPath("xsltfile1.xslt"));
  5.  
  6.     System.IO.StringWriter writer = new System.IO.StringWriter();
  7.     trans.Transform(doc,null,writer);   
  8.     writer.Flush();
  9.     System.Text.StringBuilder sb = writer.GetStringBuilder();
  10.  
  11.     System.Web.UI.Control parsedControl = Page.ParseControl(sb.ToString());
  12.     HtmlForm form = (HtmlForm)Page.FindControl("WebForm1");
  13.     for (int i=0;i< parsedControl.Controls.Count;i++)
  14.     {
  15.         HtmlInputButton button = parsedControl.Controls[i] as HtmlInputButton;
  16.         if(null != button)
  17.         {        
  18.             button.ServerClick += new System.EventHandler(this.Button_Click);
  19.             form.Controls.Add(button);      
  20.         }
  21.     }
  22. }
So that didn't work for adding my dropdown. I tried putting it in Page_Init and adding it to a panel, just as you suggested, and while that worked later for what I did in the User Control, it didn't work here.

What I tried was doing something like this:

Expand|Select|Wrap|Line Numbers
  1. protected void Page_Init(object sender, EventArgs e)
  2. {
  3.     string[] myVals;
  4.     string[] myText;
  5.  
  6.     // pretend there is code here to return my list items
  7.     // into the array objects above... too lengthy to type 
  8.     // or read, but I did not have to modify it to get it 
  9.     // working, so I know it wasn't the problem
  10.  
  11.     DropDownList ddl = new DropDownList();
  12.     for (int i=0;myVals.length;i++)
  13.     {
  14.         ddl.Items.Add(new ListItem(myText[i],myVals[i]);
  15.     }
  16.     ddl.AutoPostBack = true;
  17.     myPagePanel.Controls.Add(ddl);
  18. }
And doing this (or something pretty close - I don't have the exact code in front of me right now) would not render my control to "myPagePanel", the asp:Panel I created on my page (probably didn't have that ID, but you get the idea).

When I just moved that same exact code to my User Control, and added the User Control dynamically to the main page through it's Page_Init like you did, all was happy in the land of Nod. Not sure why.

-Tom
Mar 6 '12 #4
Frinavale
9,735 Expert Mod 8TB
That is strange.
I'm glad it worked for you in the end.
What works in a UserControl should work in a Page as well.

-Frinny
Mar 8 '12 #5

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

Similar topics

4
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new...
2
by: J.Marsch | last post by:
I'm pretty new to ASP.Net (server side) Suppose that I have an instance of an ASP Page class, and I want the client's web browser to load it. I don't necessarily know the URL of the page that I...
5
by: Earl Teigrob | last post by:
I am creating an application where I would like to give web designers the ablity to create a static html page and dyanamically load it into my application(exactly like loading a user control into a...
2
by: Dimitris Pantazopoulos | last post by:
Ok, let's say I recreate the dynamically-added Dropdownlist controls of my web form even at postbacks. That means they are repopulated with values as well. (If I don't I won't be getting anything...
2
by: Nick | last post by:
I have two navigation user controls and based on a query string will load one or the other. On the page that will load the user control I cannot do the following Dim uc As FlashNavigation (The...
0
abehm
by: abehm | last post by:
I have a couple placeholders in which I want to dynamically load user controls into a multi-pageview based on db data. The database returns the filename of the user control (i.e....
4
daJunkCollector
by: daJunkCollector | last post by:
I am using AJAX to dynamically load content into a <div> layer. As it stands, I am loading .html's into the <div> layer and it works awesome. The problem is that when I try to replace the .html...
2
by: mylog | last post by:
Hi I am having a problem of getting the value from the dynamically generated table and dropdownlist. What I am facing is, I have created a table in the aspx page and now I need to add values to...
0
by: joeller | last post by:
I need to dynamically load controls and access the methods and property of the control from the page. I need to load a different control depending on a selection made by the user. I have buttons on...
2
by: teressa | last post by:
Hi Everyone, I was given a task to fix our printer friendly pages: Best practice recommendation was to dynamically load a JavaScript page. I have an asp page which is a printer-friendly page...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.