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

How to Child Control ID from Parent control ID in ASP.NET 4.0

I have a Datalist (datalist1) control which populates a list of Categories in a Linkbutton.
When user clicks specific Linkbutton, I would like to populate subcategories into another DATALIST (datalist2) control which will be inside datalist1 control.

But I am unable to retrieve ID of child DATALIST, I am getting error: The name datalist2 does not exist in the current context

BELOW IS THE SAMPLE code

/------------- CODE IN ASPX FILE ------------------
Expand|Select|Wrap|Line Numbers
  1. <asp:DataList ID="DataList1" runat="server" DataKeyField="CatID">
  2.                 <ItemTemplate>
  3.                     <li>
  4.                         <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Display" OnCommand="ImageButton_Command" CommandArgument='<%# Eval("CatID") %>'><%# Eval("Category") %></asp:LinkButton>
  5.                     </li>
  6.                     <ul>
  7.                         <asp:DataList ID="DataList2" runat="server">
  8.                             <ItemTemplate>
  9.                                 <li>
  10.                                     <asp:LinkButton ID="LinkButton2" runat="server" CommandName="Display"><%# Eval("SubCategory") %></asp:LinkButton>
  11.                                 </li>
  12.                             </ItemTemplate>
  13.                         </asp:DataList>
  14.                     </ul>
  15.                 </ItemTemplate>
  16.             </asp:DataList>
  17.  
  18. //------------- CODE IN CS FILE ------------
  19.  
  20. protected void Page_Load(object sender, EventArgs e)
  21.     {
  22.         if (!IsPostBack)
  23.         {
  24.             populateCatefories();
  25.         }
  26.     }
  27.  
  28.     protected void populateCatefories()
  29.     {
  30.         SqlConnection conn = new SqlConnection(clsConnection.connectionString());
  31.         SqlCommand cmd = new SqlCommand();
  32.         cmd.CommandType = CommandType.StoredProcedure;
  33.         cmd.CommandText = "GetAllCategories";
  34.         cmd.Connection = conn;
  35.         try
  36.         {
  37.             conn.Open();
  38.             DataList1.DataSource = cmd.ExecuteReader();
  39.             DataList1.DataKeyField = "CatID";
  40.             DataList1.DataBind();
  41.         }
  42.         catch (Exception ex)
  43.         {
  44.             Response.Write(ex.ToString());
  45.         }
  46.         finally
  47.         {
  48.             conn.Close();
  49.             cmd.Dispose();
  50.         }
  51.     }
  52.  
  53.     protected void ImageButton_Command(object sender, CommandEventArgs e)
  54.     {
  55.         if (e.CommandName == "Display")
  56.         {
  57.             getSubCategories(Convert.ToInt32(e.CommandArgument));
  58.         }
  59.     }
  60.     protected void getSubCategories(int subCatID)
  61.     {
  62.         SqlConnection conn = new SqlConnection(clsConnection.connectionString());
  63.         SqlCommand cmd = new SqlCommand();
  64.         cmd.CommandType = CommandType.StoredProcedure;
  65.         cmd.CommandText = "getsubCategories";
  66.         cmd.Parameters.AddWithValue("@CatID", subCatID);
  67.         cmd.Connection = conn;
  68.         try
  69.         {
  70.             conn.Open();
  71.             //Control subDataList = FindControl("DataList2");
  72.             DataList2.DataSource = cmd.ExecuteReader();
  73.             DataList2.DataKeyField = "SubCatID";
  74.             DataList2.DataBind();
  75.         }
  76.         catch (Exception ex)
  77.         {
  78.             Response.Write(ex.ToString());
  79.         }
  80.         finally
  81.         {
  82.             conn.Close();
  83.             cmd.Dispose();
  84.         }
  85.     }
  86.  
  87.  
---------------------- UPDATE -------------------------
I have added below code to get ID of child datalist control, but it is getting errror: Object reference not set to an instance of an object.
Expand|Select|Wrap|Line Numbers
  1. DataList childDataList = (DataList)DataList1.FindControl("DataList2");

On some another site I found below code, but My code not able to recognize item element.

Expand|Select|Wrap|Line Numbers
  1. DataList dlSubChild = (DataList)e.Item.FindControl("dlSubChild")
Aug 14 '13 #1
0 1259

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

Similar topics

7
by: David Sobey | last post by:
hi Here's some code: void SomeFunc(childClass arg); .... parentClass a; a=new childClass(); SomeFunc(a);
2
by: David K. | last post by:
My question concerns C# Windows Forms user controls. I have created navigation user control. It gets the parent control's CurrencyManager to its bindable property. In the control, any change in...
4
by: Jim Margarit | last post by:
I'm trying to write my first windows control, subclassed from Panel. In the constructor I try to use the parent property but it is undefined (null). Is there any way to get to the parent in the...
5
by: Steve Richter | last post by:
In my user control I want to read the ViewState dictionary of the Parent control. But this sensible idea is not permitted by the compiler: Compiler Error Message: CS1540: Cannot access...
3
by: luna | last post by:
i have a parent form which opens a child form, i need to pass a variable back to parent then reload parent so parentopens child, child passes to parent, child closes, parent reloads its only...
7
by: aharding | last post by:
Hello! I have a form that opens with the click of a toggle button on another form. I would like to link the child and parent fields of the forms for data input but am not experienced enough with...
2
by: Barkingmadscot | last post by:
How do you find out the name of the parant control of the control you are working working with I have try me.parent.name.tostring() ..... etc any help would be great ta
6
by: shapper | last post by:
Hello, I am working in a class library with various custom controls. In which cases should a control inherit Control, WebControl and CompositeControl classes? And when should a custom...
2
by: ChaoticDALO | last post by:
Hello, I am wondering if this is possible. If I have a parent class and a child that inherits the parent, the child can access a parents member function right? Well let's say we are in the parent...
0
by: Sep410 | last post by:
How can I hide some of the child columns in child and parent data grid?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.