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

using FindControl to capture programitacialy created dropdown list

35
Hi All,
I have created a panel as a place holder on an input interface and I want to be able to dynamicaly fill it with drop down lists, check boxes etc. So below I'll show you the two main pieces of this effort - the function that creates the dropdown list in question and the function that fires when the submit button is clicked.
I've stepped through this and what I'm finding is that when the submit button is clicked the page posts back and the dropdown list is removed from the page.
Expand|Select|Wrap|Line Numbers
  1.     protected void lbNewShiftLog_Click(object sender, EventArgs e)
  2.     {
  3.         DropDownList ddlCusts = new DropDownList();
  4.                //datasource is populating the dropdown list fine
  5.         ddlCusts.DataSource = dS;
  6.         ddlCusts.DataTextField = "customer";
  7.         ddlCusts.DataValueField = "customer";
  8.         ddlCusts.DataBind();
  9.         ddlcusts.ID = "ddlcusts";
  10.         pnlOptions.Controls.Add(ddlcusts);
  11.     }
  12.  
  13.     protected void btnSubmit_Click(object sender, EventArgs e)
  14.     {
  15.     DropDownList ddl = (DropDownList)pnlOptions.FindControl("ddlCusts");
  16.     if (ddl != null)
  17.       {
  18.        Label1.Text = ddl.SelectedItem.ToString();
  19.       }
  20.     }
  21.  
-----------------------------------------------------------------------
The above function returns null - I have also tried
Expand|Select|Wrap|Line Numbers
  1. DropDownList ddl = (DropDownList)PreviousPage.FindControl("ddlCusts");
  2.  
Which returns the error:
Object reference not set to an instance of an object.

Plz Healp
Thank You
Jun 14 '07 #1
8 2018
Plater
7,872 Expert 4TB
Well, it might me just a copy/paste error, but the ID is all lowercase, and then in your search string it's got a capital letter?
Jun 14 '07 #2
tagg3rx
35
Hi Thanks for the response
I fixed that and I'm still getting the same thing.

What is the cause of this error when i'm using the previouspage namespace

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 67: case "NewLog":
Line 68: //FindControl
Line 69: DropDownList ddl = (DropDownList)PreviousPage.FindControl("ddlCusts") ;
Line 70: if (ddl != null)
Line 71: {


Source File: c:\Documents and Settings\dlewis\My Documents\Visual Studio Codename Orcas\WebSites\Log\Default.aspx.cs Line: 69
Jun 14 '07 #3
TRScheel
638 Expert 512MB
Hi Thanks for the response
I fixed that and I'm still getting the same thing.

What is the cause of this error when i'm using the previouspage namespace

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 67: case "NewLog":
Line 68: //FindControl
Line 69: DropDownList ddl = (DropDownList)PreviousPage.FindControl("ddlCusts") ;
Line 70: if (ddl != null)
Line 71: {


Source File: c:\Documents and Settings\dlewis\My Documents\Visual Studio Codename Orcas\WebSites\Log\Default.aspx.cs Line: 69

Did you check to make sure that PreviousPage isnt null? Or can be cast to DropDownList?
Jun 14 '07 #4
tagg3rx
35
Ok yea thats not going to work I just read up on previous page and realized its only for non postback operations, when i tried to set it I got "Circular file references are not allowed."

So the issue is when the script runs the dropdown list is no longer there, why is it disapearing, I dont have any code telling pnlOptions to purge or reset it's self. nor is there any code that i can tell that fires between the dropdown list being created and when the submit code runs???
Jun 14 '07 #5
TRScheel
638 Expert 512MB
Ok yea thats not going to work I just read up on previous page and realized its only for non postback operations, when i tried to set it I got "Circular file references are not allowed."

So the issue is when the script runs the dropdown list is no longer there, why is it disapearing, I dont have any code telling pnlOptions to purge or reset it's self. nor is there any code that i can tell that fires between the dropdown list being created and when the submit code runs???
To be honest am surprised that with the code you provided the dynamic DDL stays after a post back.

You mind posting all or a good chunk of your code for that page? (Or and place it within code tags).
Jun 14 '07 #6
tagg3rx
35
Ok I did a break out of how I have written it thus far (agian I appriciate your help I'm still learning the fundamentals here)

Heres what I got :

default.aspx
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head runat="server">
  7.     <title>Untitled Page</title>
  8. </head>
  9. <body>
  10.     <form id="form1" runat="server">
  11.     <div>
  12.         <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
  13.         <asp:Panel ID="Panel1" runat="server" Height="50px" Width="125px">
  14.             <asp:Button ID="btnSubmit" runat="server" Text="Submit" 
  15.             onclick="btnSubmit_Click" />
  16.         </asp:Panel>
  17.         <asp:Button ID="btnCreate" runat="server" Text="Create" 
  18.         onclick="btnCreate_Click" />
  19.     </div>
  20.     </form>
  21. </body>
  22. </html>
  23.  
default.aspx.cs
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.Data.SqlClient;
  12.  
  13. public partial class _Default : System.Web.UI.Page 
  14. {
  15.     public string csNetops = "server=****;" +
  16.     "User ID=****;Password=****; database=OpsR3";
  17.  
  18.     protected void Page_Load(object sender, EventArgs e)
  19.     {
  20.  
  21.     }
  22.     protected void btnCreate_Click(object sender, EventArgs e)
  23.     {
  24.         string sqlQuery = "SELECT customer FROM NOC_Customers";
  25.         SqlDataAdapter dA = new SqlDataAdapter(sqlQuery, csNetops);
  26.         DataSet dS = new DataSet();
  27.         dA.Fill(dS, "customers");
  28.  
  29.         DropDownList ddlCusts = new DropDownList();
  30.         ddlCusts.DataSource = dS;
  31.         ddlCusts.DataTextField = "customer";
  32.         ddlCusts.DataValueField = "customer";
  33.         ddlCusts.DataBind();
  34.         ddlCusts.ID = "ddlCusts";
  35.  
  36.         Panel1.Controls.Add(ddlCusts);
  37.  
  38.     }
  39.     protected void btnSubmit_Click(object sender, EventArgs e)
  40.     {
  41.         DropDownList ddl = (DropDownList)Panel1.FindControl("ddlCusts");
  42.         if (ddl != null)
  43.             Label1.Text = ddl.SelectedItem.ToString();
  44.         else
  45.             Label1.Text = "Failed";
  46.     }
  47. }
  48.  
  49.  
  50.  
Jun 14 '07 #7
tagg3rx
35
Ahh I figured out what I was doing wron - I was over thinking the issue.

I replaced the findcontrol with:

string cust = Request.Form["ddlCusts"];

And now all is well....

All this fancy oop coding I've been learning with .net made me forget about the data in the post....

DOH!
Jun 14 '07 #8
Frinavale
9,735 Expert Mod 8TB
Ahh I figured out what I was doing wron - I was over thinking the issue.

I replaced the findcontrol with:

string cust = Request.Form["ddlCusts"];

And now all is well....

All this fancy oop coding I've been learning with .net made me forget about the data in the post....

DOH!
Good stuff!

Thanks for sharing the solution.

-Frinny
Jun 14 '07 #9

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

Similar topics

0
by: Ruby | last post by:
Hi All... I have problem with web form datagrid: I have 1 dropdownlist and 1 text box in datagrid footer. I want every time index change in dropdown list a value from database table stored in...
4
by: MattB | last post by:
This is just a rephrased version of a question I posted earlier. I think I'm closer now, so it seemed worthy of a new (more specific) post. In my repeater I'm dynamically creating text boxes, so...
8
by: Adam Billmeier | last post by:
Problem: I am trying to use FindControl to grab a dropdownlist that is in the EditItemTemplate of a Datalist and then Bind it to a dataset. I also then need to put the correct values in all of...
2
by: zambizzi | last post by:
....I can't seem to get my hands on a control I'm loading in an editable datagrid. Here's my datagrid control: <asp:datagrid id="GLRulesGrid" runat="server" autogeneratecolumns="False"...
0
by: Jason James | last post by:
Guys, I have a webform that uses a datalist control to display a series of thumbnail images. Each image is is also associated with two dropdown lists. One contains the quantity of images...
3
by: Savas Ates | last post by:
My aim is showing product availability in a dropdown by checking the database issues and let user to change its value... The Error : ideefixesatisc.DataTextField = "Stokta Yok" Object...
53
by: Hexman | last post by:
Hello All, I'd like your comments on the code below. The sub does exactly what I want it to do but I don't feel that it is solid as all. It seems like I'm using some VB6 code, .Net2003 code,...
15
by: | last post by:
I dynamically create controls (textboxes) in a repeater control. I know their names (eg. TextBox1). How do I find the text of TextBox1 in the Form? FindControl does not seem to work.
1
by: Stuart Shay | last post by:
Hello All: I have a Datalist where each row has TextBoxes and Dropdowns that are created dynamically. The code renders correctly and is very similar to this sample I found ...
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: 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: 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
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: 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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.