473,760 Members | 10,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1924
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.W ebControls;
using System.Componen tModel;

namespace DynPanel
{
/// <summary>
/// Summary description for WebCustomContro l1.
/// </summary>
[ToolboxData("<{ 0}:WebCustomCon trol1
runat=server></{0}:WebCustomCo ntrol1>")]
public class WebCustomContro l1 : System.Web.UI.W ebControls.WebC ontrol,
INamingContaine r
{
private RadioButtonList rblTemp = new RadioButtonList ();
private Label lblTemp = new Label();

protected override void CreateChildCont rols()
{
Controls.Add(ne w LiteralControl( "<P><B>"));

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

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

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

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

Button1_Click Event:
private void Button1_Click(o bject sender, System.EventArg s e)
{
WebCustomContro l1 wcc1Temp = ((WebCustomCont rol1)Panel1.Con trols[0]);
RadioButtonList rblTemp =
((RadioButtonLi st)wcc1Temp.Fin dControl("rblTe st"));
Response.Write( rblTemp.Selecte dItem.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(EventArg s e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeCompo nent();
base.OnInit(e);

WebCustomContro l1 wcc1Temp = new WebCustomContro l1();
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.publi c.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
262
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 Page_Init event. In that control I have hyperlink that loads a new window through javascript with a calendar in it.
7
3260
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 load one of several controls: private void btnCategory_Click(object sender, System.EventArgs e) { Control myControl = LoadControl(DropDownList1.SelectedItem.Text + ".ascx"); PlaceHolder1.Controls.Add(myControl);
1
8166
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 first time the control is dynamically loaded, everything works fine. After that, if the control is loaded again from the page button event handler, the user controls events fail to fire on the first click NOTE: I (believe I) am rebuilding all...
1
13720
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 and set methods on the user control "editButton.ascx" which work fine. How do i pass parameter into the user controls "c1", "c2" ? Here is a bit of my code that is calling the user control from the
2
2091
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 Control B dynamically. Now in Control B there is a "Return" button which calls the method A of Control A. In method A, a session variable is being retrieved. My problem is whenever the control is passed back to the Control A, error is being thrown...
0
1177
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
3119
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 UserControls into the PlaceHolder's child control collection depending upon which of the three radio buttons is selected. Each of the three UserControls have postback events themselves triggered by button clicks. The problem I'm having is keeping track of...
1
369
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 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
3
2542
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 controls are not present nor their valves in Request.Form.AllKeys.
0
10107
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9945
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9765
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8768
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6599
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5214
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5361
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3863
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.