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

ASP.NET Retrieving values from dynamically loaded user control

I am working on an ASP.NET web app. One of the pages will
be using several user controls. The controls will be
loaded dynamically based on SQL Server data.

I need to be able to retrieve the values from the
individual fields in those user controls. So far I am not
able to access the controls at postback. Any suggestions
would be appreciated.

Joel Reinford
Jul 21 '05 #1
1 1878
Hello Joel,

Thanks for posting in the group.

Since the control(s) are added before the page is rendered (dynamically on
the server), they don't exist until you re-add them at some point before
the page is rendered. Page and its controls are created/rendered/destroyed
on every request and there is nothing in the framework that automatically
remeber which controls were previously loaded/created. So we need to create
it again in postback. It's just the way .NET works with dynamically added
controls.

Here are some sample code for your reference: (Please add control in
Page_Init method)

WebControl:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace DynPanel
{
/// <summary>
/// Summary description for WebCustomControl1.
/// </summary>
[ToolboxData("<{0}:WebCustomControl1
runat=server></{0}:WebCustomControl1>")]
public class WebCustomControl1 : System.Web.UI.WebControls.WebControl,
INamingContainer
{
private RadioButtonList rblTemp = new RadioButtonList();
private Label lblTemp = new Label();

protected override void CreateChildControls()
{
Controls.Add(new LiteralControl("<P><B>"));

lblTemp.ID = "lblTest";
lblTemp.Text = "Label Test";
Controls.Add(lblTemp);

Controls.Add(new LiteralControl("</B>"));

rblTemp.ID = "rblTest";
rblTemp.Items.Add("A");
rblTemp.Items.Add("B");
rblTemp.Items.Add("C");
rblTemp.Items.Add("D");
Controls.Add(rblTemp);

Controls.Add(new LiteralControl("<BR><HR>"));
}
}
}

Button1_Click Event:
private void Button1_Click(object sender, System.EventArgs e)
{
WebCustomControl1 wcc1Temp = ((WebCustomControl1)Panel1.Controls[0]);
RadioButtonList rblTemp =
((RadioButtonList)wcc1Temp.FindControl("rblTest")) ;
Response.Write(rblTemp.SelectedItem.Text);
}

I also used the OnInit event to load the composite control as well. Here
is the code used for that:
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);

WebCustomControl1 wcc1Temp = new WebCustomControl1();
Panel1.Controls.Clear();
Panel1.Controls.Add(wcc1Temp);
}

By the way, in the future, it would be best to post these questions in the
following newsgroup.

Microsoft.public.dotnet.aspnet

All .NET asp.net issues, configuration and other questions are posted in
the newsgroup above.

The reason why we recommend posting appropriately is you will get the most
qualified pool of respondents, and other partners who the newsgroups
regularly can either share their knowledge or learn from your interaction
with us. Also, this is to make sure that the responders can better track
the problem. Thank you for your understanding.

Have a nice day!

Best regards,
Yanhong Huang
Microsoft Online Partner Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Mark Stokes | last post by:
Hi guys, I have seen various messages on this front, but none specific to my requirement. I have an application that dynamically loads a user control and displays it on the page during the...
7
by: Tim T | last post by:
Hi, I have the need to use dynamically loaded user controls in a webform page. I have the controls loading dynamically, and that part works fine. this is the code used in a webform to dynamically...
1
by: Earl Teigrob | last post by:
PROBLEM: When a user control is loaded into a PlaceHolder control more than once, the events do not fire on the first click of a control on the dynamically loaded user control. In other words, the...
1
by: Josh | last post by:
Hi Guys, I have been having a big problem with trying to pass parameters into a user control when the user control is dynamically loaded into a placholder. I am developing in c#. I have get...
2
by: Diffident | last post by:
Hello All, I have a nested user control i.e., Control B which is loaded from Control A. Control A is itself dynamically loaded from a web form. Control A has a "Save" button which loads the...
0
by: Diffident | last post by:
Hi Steve, Please find the code snippets below: Method A inside User Control A: ------------------------------------------- public void LoadDataGrid() { DataSet ds = new DataSet();
4
by: Harry | last post by:
Hello, I have a page with a RadioButtonList and a PlaceHolder control. The RadioButtonList's AutoPostBack attribute is set to TRUE and its SelectedIndexChanged event loads one of three...
1
by: Joel Reinford | last post by:
I am working on an ASP.NET web app. One of the pages will be using several user controls. The controls will be loaded dynamically based on SQL Server data. I need to be able to retrieve the...
3
by: Dotnet Gruven | last post by:
I've built a WebForm with a Table added dynamically in Page_Load when IsPostBack is false. The table includes a couple of TextBoxes, RadioButtonLists and CheckboxLists. On postback, those...
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...
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
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: 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...

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.