getting value of dynamically created control as null

Dear All,

I am mess with one situation let me explain the scenario.

I am making search form where I display 15 field in checkboxlist and user select one or two or three or any no to all field. Depending on his selection (he tick the field  and click filter button ) Now in filter button click I create label and  dropdownlist control(populated from database) and add it to placeholder control. Now I select value and click the search  button. Now in search button I need to access those control and its selected value(ie label and droplistbox ).

I  have made function to create label and dropdownlistbox  which is

 private void createcontrols()    {        foreach (ListItem li in CheckBoxListmst.Items)        { 

           if (li.Selected == true)

            {                 //code added to fetch dropdownlist                setting =    ConfigurationManager.ConnectionStrings["StyleSearchConnectionString"];                if (setting != null)                {                    Code to access data for populatingdropdownlistbox                     try                    {                        // added controls                        Label label = new Label();          

 

          DropDownList dropdownlist = new DropDownList();                 

 label.ID = "label" + i.ToString();

                        dropdownlist.ID = "dropdownlist" + .ToString();                                                   i++;                        rdr = cmd.ExecuteReader();                    

    while (rdr.Read())

                        {   

                         Items fordropdownlist

                          }                        PlaceHolder1.Controls.Add(label); 

PlaceHolder1.Controls.Add(dropdownlist)

                       }                     catch (SqlException ex)                    {                                         } 

   Now this create function I call in one in filter button click event and second as

    protected override void Oninit(EventArgs e)   

 {

        base.OnLoad(e);        createcontrols();     } Now in seach button click event when I am trying to access Code is  Label  objlbl;DropDownList objdrop;objlbl = placeholder1.FindControl("label1") as Label;objdrop = placeholder1.FindControl("dropdownlist1") as DropDownList;if ((objlbl != null) && (objdrop != null))  {  

cmd.Parameters.Add("@para1", SqlDbType.VarChar).Value =objlbl.Text;    

 

   cmd.Parameters.Add("@value1", SqlDbType.VarChar).Value = objdrop.SelectedValue;

                   }

 

 I am getting  objlbl and objdrop as null.   

Please help

 

 

thanks

     

 

 

0
mohdimranabdulaziz
7/26/2007 3:00:06 PM
asp.net.web-forms 93655 articles. 5 followers. Follow

2 Replies
507 Views

Similar Articles

[PageSpeed] 34

Dear All,

i corrected some code but still problem remain the same

I am mess with one situation let me explain the scenario.

I am making search form where I display 15 field in checkboxlist and user select one or two or three or any no to all field. Depending on his selection (he tick the field  and click filter button ) Now in filter button click I create label and  dropdownlist control(populated from database) and add it to placeholder control. Now I select value and click the search  button. Now in search button I need to access those control and its selected value(ie label and droplistbox ).

I  have made function to create label and dropdownlistbox  which is

 private void createcontrols()    {        foreach (ListItem li in CheckBoxListmst.Items)        { 

           if (li.Selected == true)

            {                 //code added to fetch dropdownlist                setting =    ConfigurationManager.ConnectionStrings["StyleSearchConnectionString"];                if (setting != null)                {                    Code to access data for populatingdropdownlistbox                     try                    {                        // added controls                        Label label = new Label();          

 

          DropDownList dropdownlist = new DropDownList();                 

 label.ID = "label" + i.ToString();

                        dropdownlist.ID = "dropdownlist" + .ToString();                                                   i++;                        rdr = cmd.ExecuteReader();                    

    while (rdr.Read())

                        {   

                         Items fordropdownlist

                          }                        PlaceHolder1.Controls.Add(label); 

PlaceHolder1.Controls.Add(dropdownlist)

                       }                     catch (SqlException ex)                    {                                         } 

   Now this create function I call in one in filter button click event and second as

    protected override void Oninit(EventArgs e)   

 {

        base.OnLoad(e);        createcontrols();     } Now in seach button click event when I am trying to access Code is  Label  objlbl;DropDownList objdrop;objlbl = placeholder1.FindControl("label1") as Label;objdrop = placeholder1.FindControl("dropdownlist1") as DropDownList;if ((objlbl != null) && (objdrop != null))  {  

cmd.Parameters.Add("@para1", SqlDbType.VarChar).Value =objlbl.Text;    

 

   cmd.Parameters.Add("@value1", SqlDbType.VarChar).Value = objdrop.SelectedValue;

                   }

 

 I am getting  objlbl and objdrop as null.   

Please help

 

 

thanks

     

 
0
mohdimranabdulaziz
7/26/2007 3:59:48 PM

Hi mohdimranabdulaziz,

Double click your code,please.

1.Where is the variable "i"? When your define it?

2.Error1: dropdownlist.ID = "dropdownlist" + .ToString();

  You'd better change your code as follows,

  dropdownlist.ID = "dropdownlist" + i.ToString();

3.Make sure you have set 1 to "i" if you define "i".

DropDownList objdrop;

objdrop = placeholder1.FindControl("dropdownlist1") as DropDownList

"objdrop" always get null value if you do not set 1 to "i".

Hope it helps,

Hong Gang


Hong-Gang Chen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Hong
7/31/2007 2:00:46 AM
Reply:

Similar Artilces:

How to get the value of dynamically created text box which is created dynamically on click of button control.
<p>Dear AllI am new in asp.net.I am doing one project there I have one doubt.what I want is I have taken two  button controls in my page, in click of first button1 I have  dynamically created one textbox and I added this textbox to my page and also set the id for this textbox,.after that in click of second button2 I want that the value of the textbox will be print in my page by getting the value of the textbbox. some codes are given below. <form id="form1" runat="server">    <div>      ...

How get value from a dynamically created control in a user control
HI, I am working with a version of IBuySpy. On one page is a form that contains a user control that lists the items the user purchased, the subtotal, shipping, etc. I wanted to add a DropDownList from which the user could select the shipper. The dropdown list works fine as long as it has .AutoPostBack = false. However, I cannot figure out how to get the value that the user selected when they finally press the submit button on the form. All attempts to locate the control and it's value have failed. I tried drillingdown from the form to the child controls, and sub child controls. N...

Reading form values in dynamically created child controls in a user control
I have a common problem for which I've searched the web for answers for hours to no avail. I have some radiobuttonlists ,dropdownlists,checkboxlists and text controls in a user control. The lists are created statically, however the items in the lists are populated at run time. The lists are displayed in default.aspx (the containing page) and populated on the Page_Load event of the user control .ascx file based on an external data source containing arrays of data. I want to be able to read the values selected by the user from deafult.aspx, the parent page. details: The .ascx...

Get user input from dynamic created control, why i cant get the value???
Hi!, i was tried to dynamically generate some textboxes according the selected number in a dropdownlist, but when i need to get the user input from those textboxes.. i cant get at all!!, Can any1 guide me on tis? Below is my code.Basically, i have 1 dropdownlist (ddltbCount), 1 placeHolder(Pholder1), 1 button(btGetValue), i dont know y i cant post the html here... Private count As Integer = 1 Sub IterateThroughChildren(ByVal parent As Control) For Each c As Control In parent.Controls If c.GetType.ToString.Equals("System.Web.UI.WebControls.TextBox") AndAlso c.ID Is Nothing Then ' CType(c, T...

Problem in accessing web form data in user controls when create user control dynamically
HI. I have a problem using web user control. I designed a user control named NewsForm. This user control contains some web form elements like <asp:textbox>. When I add it in an .ASPX page, all things may be true. I can know which user filles in textbox (for example and other controls) <%@ Register TagPrefix="uc1" TagName="NewsForm" Src="NewsForm.ascx" %> and <UC1:NewsForm id="newsForm" runat="server"></UC1:NewsForm> But when i create it in code behined dynamically, however user control has web form controls but their value are blank for example //aspx co...

How to get the HREF value of the Anchor tag in a web page using the web browser control in vb.net
Hi Friends,               i have a webserver control in my application.i load the web browser control on a button click using the below code Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click                AxWebBrowser1.Navigate("http://www.rateitall.com/i-322335-.aspx", False) End sub once the page is loaded in the browser i want the <a> HREF value in that page.For example if that page has ...

when loading a previously created record on to form, some controls are missing values(controls are depended on values selected in other controls)
Hello - I have created a page for Editing records.  this page will open up a chosen record and allow user to edit the data.On the form, I have 3 drop down lists.   The second dropdown list should fill values depending on value chosen in first dropdown list.  The third dropdown list should show/fill values depending on value chosen in second dropdown list.So taking this into count, i have several records with various data.  When I open-up/load a record in Edit form, only the first dropdown list has a value selected that was the pre-existing va...

Get the value of dynamically created controls
I have created the following function to get the text out of dynamically created text boxes that I have created from the code behind page.When call the function I get a message saying Object Reference not set to an instance of an object on this line -For Each c In Me.Parent.ControlsThe ID of the textboxes have the following format - Quantity_1111 Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnUpdate.Click Dim c As Control Dim strID As String Dim arrProducts(2) As String Dim dtProducts As New DataTable 'Dim parent As C...

Creating dynamic controls and getting their values
 Hi everyone,i have to create a number of dynamic controls (dropdownlist and textboxes) acording to a number inputed by the user.i've read some articles, posted some posts, but aynone has truly help me, so i'm asking if someone can hepl me beeing objective.i've put the code in the page_load, and in the the page_init (not on both simultaneous), but i don't know in which one is right.The code i putted on the Page_Load was this:Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load        If (Page.IsPo...

Get values of dynamic created controls?
Hello there,I'm creating some dynamic controls, where i'm getting some info from a db. - Thats working, but I dont know how to get the values from the dynamic created controls again, so i could save them in my database? This code am i using to create the controls (This get looped for each record in the db) 1 ' Connection 2 Dim str As String = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString 3 Dim conn As SqlConnection = New SqlConnection(str) 4 conn.Open(...

Dynamically creating controls in Web forms
I am able to create a web control programmatically and display on the form. In the click event of the button then I am trying to capture the value entered in the above created textbox in another click of button but I am unable to obtain the reference of the control that has been created dynamically. Can anyone help me please? protected System.Web.UI.WebControls.Panel myPanel; private void btnMake_Click(object sender, System.EventArgs e) { TextBox txtBox = new TextBox(); myPanel.Controls.Add(txtBox); } private void btnGotValues_Click(object sender, System...

Dynamically created controls in web form
Hi there. This is bit of an open ASP.Net (C#) question with regards to dynamically created controls and in particular tabular or panel controls. What I currently have is a web page that creates between 1 and 6  tables, each of which holds specific data about a set of products, each table (or panel, if you want) has a couple of multi-line TextBoxes and a few other controls, all lined up in a set format. The number is determined by the data records returned from a database table. My question is what is a best way to create these tables of data at run time?  Has anybody else had e...

Problem with getting the value of a dynamically created CONTROL
Dear,I'm facing a problem with dynamically created controlsI'm creating RadioButtons  according to certain data from a database, the problem is that the RadioButtons are being created and are being displayed in a dynamically created HtmlTable inside the HtmlTableCell. When i want to get the value of the RadioButton if is it checked or not , i'm using the following codeRadioButton rbtn = (RadioButton)Page.FindControl(ID);The rbtn is always null knowing that if i check the Page source i find the RadioButton with the Same ID that I'm calling in the FindControl. Co...

Get the value or Text of dynamically created controls?
Hello, I have a page with a placeHolder, in wich I create several controls (of textBox, checkBox and dropdownList type) I found several articles on the web about dynamic controls, but I couldn't find how to access each controls properties. I would like to be able to check if the user has checked the checkBox or not, and read the text inside the textBox I created Is that not possible? Thank you An important thing to keep in mind is that you need to recreate the dynamic controls you add to your PlaceHolder control each time a postback occurs.  After that, you can use FindContr...

Web resources about - getting value of dynamically created control as null - asp.net.web-forms

Placebase team at Apple file “Schematic Maps” patent dynamically detailing important data
In a report from Computerworld way back in 2009, we learned that Apple had quietly acquired Maps API company Placebase. Then, founder of Placebase ...

Apple's Lightning port dynamically assigns pins to allow for reversible use
... a closer look at how Apple's new Lightning connector works, and has come to the conclusion that the 8 pins on each side of the plug are dynamically ...

In a dynamically typed language, is it a bad idea to return different data types?
In some cases, returning different types from a single function works.

Urlbox Launches Screenshots-as-a-Service Platform to Dynamically Update Galleries
... changes. Launched yesterday, Urlbox allows anyone with a gallery displaying website screenshots to automate the process and keep them dynamically ...

Everything.me launcher dynamically adapts to what you’re doing right now
... phone according to what you’re doing. Perform a search for ‘football’ and your background will change to the NFL logo. A launcher that dynamically ...


A YouTube Experiment Resizes the Player Dynamically
There's a new YouTube experiment that changes the video player's size, depending on the browser window's size. When resizing Chrome's window, ...

Apple Granted “Dynamically Changing Cursor for User Interface” Patent, Originally Filed in 2003
... a utility patent that covers the use of the mouse cursor that changes according to the context of the task it is engaged in. Called a “Dynamically ...

Atmel's FPSLIC II Dynamically Reconfigurable SoC Supports "Silicon-Sharing" For Peripherals & Interfaces ...
Silicon-Sharing Fits 200K Gate Design into 40K Gate FPSLIC II's, Cuts Power Drain by 97%

Everything.me Launcher Wants to Dynamically Change Phones Based on Location, Interests, and Mental State ...
What if your phone’s launcher dynamically changed based upon interests, your location, or topics you had in mind at a specific moment? A new ...

Resources last updated: 11/24/2015 12:39:03 AM