how can i call controls from ascx (in aspx page)

hi ,

i have page.aspx and i create 2 a.ascx and 1 b.ascx in onInit page.aspx

page.aspx code behind - OnInit

for (int i = 0; i < 2; i++)


Control c = this.LoadControl("~/a.ascx");

c.ID = "c_" + i.ToString();



Control c = this.LoadControl("~/b.ascx");

c.ID = "c_" + i.ToString();



i create dynamic 2 a.ascx and 1 b.ascx in page.aspx.

a.ascx have 1 buton , 1 textbox and 1 label b.ascx is same.

i want to take a.ascx and b.ascx textbox.text and want to click butons...


how can i do ?



4/25/2008 8:48:57 AM 93655 articles. 6 followers. Follow

7 Replies

Similar Articles

[PageSpeed] 37

can either create propeties in your user controls


public TextBox GetTextbox

get{ return Textbox1}

set {Textbox1 = value;}


or something like:

TextBox myTB = new TextBox(); 

foreach (control c in a.controls)


if (c.GetType() = typeof(TextBox))


myTB = (TextBox)c;

4/25/2008 10:45:22 AM

dont work :(

4/28/2008 8:46:52 AM

 let me clarify ur probem.
U've page.aspx page in which u created 2 ascx controls at runtime.
And u want to access the textbox and button inside the ascx controls from page.aspx?

plz mark it as answer if it helps u :)
4/28/2008 8:54:29 AM

Drag that .ascx control from Solution Explorer. And put in .aspx Page


"Mark as Answer "  if helpful

4/28/2008 9:01:51 AM

Here's what I understand on ur problem. I may be wrong :)
U can use FindControl method to access the textbox that is inside ascx page.
Or either way why don't u write code for accessing textbox in the ascx page itself?
If u requirement is to access from the aspx page then below is an example code that might help ur problem.

WebUserControl1.ascx page 
  <%@ Control Language="c#" AutoEventWireup="false" Codebehind="WebUserControl1.ascx.cs" Inherits="WebApplication2.WebUserControl1" TargetSchema=""%>
<table width="100%" cellpadding="0" cellspacing="0"&gt;
<td width="
<asp:TextBox id="
TextBox1" runat="server" Width="424px"&gt;</asp:TextBox>
<asp:Button id="
Button1" runat="server" Text="click here to set text in button">
In codebehind file of ascx file , add the method for button click event.
 private void Button1_Click(object sender, System.EventArgs e)
//do something here TextBox1.Text="Text is set from ascx page...";
Then u can use the following code in ur aspx page.
Control c = this.LoadControl("WebUserControl1.ascx");

c.ID = "c_" + 1;

//Accessing textbox in usercontrol(ascx) from aspx page ( for demonstration ) TextBox txt=(TextBox)c.FindControl("TextBox1");

if (txt!=null)
txt.Text="Text is set from aspx page...";

 Hope this helps...

plz mark it as answer if it helps u :)
4/28/2008 9:29:55 AM

yes khinly2k

4/28/2008 9:30:45 AM

thx khinly2k  :),


its working :)


4/28/2008 1:25:53 PM

Similar Artilces:

Can you return .net web controls in a .net page from a web service?
I've got a problem with a project. I've got to compile an entire .net page and return it as part of a Web Service for display. The problem I have is that I need to add WebControls and functionality to the page so that not only the html content and images are displayed (which I've managed) but that textboxes, labels, buttons and datasets can be displayed also. Essentially I need to make a call from one site for a page, populate the page and return it in a state as though it resided on the same server/webspace as the calling page. Code is currently built as follows into a string: strOpenHT...

Can my web page with a form submit to other web pages forms?
I was wondering if I created an aspx page that had a form on it that I could use the submit button to submit this form's contents to other web pages forms(servers accepting those form's data). I am thinking this would be a great way to update data on particular web pages without having to actually visit those similar web pages. Any ideas, examples would be much appreciated. Thanks in advance.  Yes you can.  It's called Cross Page Posting: might ...

Can I call a method on aspx page thru the method of control(ascx)
Can I call a method on aspx page thru the method of control(ascx)Ajay Pathak MVP All About better u can createa method in class and access from ascxHAPPY CODING Your question is not clear to me, You mean to call method which is in aspx form ascx??Imran[MCTS]Few have audacity to speak truth Hi, Please refer the following code,  uc.ascx:<%@ Control Language="C#" ClassName="uc" %> <script runat="server"> protected void Page_Load(object sender, EventAr...

Validating web user control(ascx) in parent page (aspx) -- .NET 1.1
Hi,  I have a user control which has some server controls like drop down list, list box etc. These fields are required. I'm using this control in a parent page which has its own server controls which are also required. The button on the parent page which is supposed to validate the controls is not working after I add the web user control.  Is there a work-around for this scenario  other than manually calling Page.Validate() and checking for Page.IsValid property ? I tried calling a javascript on button click which calls Page_ClientValidate() whic...

A page can have only one server-side Form tag
I have a web user control in my web page.  The user control (ascx) consist of a text box and button. The web page consist of text boxes and buttons also.  Both pages have form tags with runat=server.  When I run the web page it comes up with the error "A page can have only one server-side Form tag." Both the user control and web page need the form tag with runat=server.  Is there a way I can code these pages where there won't be a conflict between the web user control and the web page? Thanks   JohnMo wrote:Both the user control and web page need the form tag wit...

Call public method on aspx page from a web control embedded in the page
I have a nut to crack. How do I call a public method on my aspx page from my web control, the web control is embedded into the page. The markup looks like this:-   1 <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 2 <ContentTemplate> 3 <asp:Xml ID="Xml1" runat="server"></asp:Xml> 4 <uc2:tier3menu ID="menu1" runat="server" /> 5 </ContentTemplate> 6 </asp:UpdatePanel> I wrote an itemcommand thingie in the web control that needs to set the Documentsource a...

how to call controls of web control form in coding of other forms?
hello everyone i have one problem i have created one webcontrol shoppingcart in which there are two labels one of qty and other of amt which are initially enable =falsenow on other page(aspx page) i have one txtbox of qty as txtqty i want tat as soon as user enters qty txtqty and clicks on add cart button it shd be display d value in lblqty of web control shopping cart make sure i m using datalist on aspx page so give me suggestions on tat basis and my coding is in vb. plz give ur suggestions as fast as possible. Neeti what do you mean by webcontrol.... it this user control or normal ...

how can i access .ascx (user control) controls like textbox,label .... from .aspx page
 I have 2 textboxes in my web user control(.ascx).i need to set some value to those textboxes from my .aspx page how is it possible.any one plz help me..............................  waiting for your solution :-)  :-)   Thankyou in advance ,have a greate day>>>>>>>>>>>>>>>>  You can try something like this from your aspx page's code behind: Dim txtTest As Textbox txtTest = usercontrol.FindControl("textbox_name_on_control") txtTest.Text = "the text you need"  &nb...

Can I have a web user control call a method on the page it's parent page?
I'm building an application where I'm using serveral different controls on a page and as one of them causes a postback I may want the page to make changes to other controls.  Is there a way to either fire and event off from the control that the page could then capture or simply have the control call a method of it's parent page? Obviously the Page object is there to work with, but I can't seem to figure out how to specifically cast it as a specific page.  For real man, In your user control you need to create a public event which is raised by the event of the...

Web Menus / User controls in aspx web page and .aspx C# based web templates
Dear all, I have created several user controls and included in the webpages. (This user control is a navigation menu consisting of images) This has to be included in all webpages. But when i tried to publish i get error messages for them. But i can’t remove since i need these user controls. Error message given below Error 2 The type 'WebUserControl' exists in both 'c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\new_latest\d203e69a\d67c356f\App_Web_ijsq-eqk.dll' and 'c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\new_la...

i have given 2 link buttons in default.aspx page .....but how can i call these buttons in default2.aspx.cs page???please can u help me??
can u give any suggestions please???? Hi You can use the ID's of the two link buttons to call them in .cs file. If you want to call a method or fire an event when button is clicked you need to write an button click event for those two buttons.  the following link button can  be used in the code with "linkButton" object and when this button is clicked 'OnClick_Submit" event will fire in the .cs file <asp:LinkButton ID="linkButton" runat="server" OnClick="OnClick_Submit">  Shekar.Y(GGKTech)http://ggktech....

How can I control load sequence of web controls in a web page?
Hi, How can I control load sequence of web controls in a web page? Thanks. For the most part, the load sequence on a web page is not relevant since all controls must be present before the page can be used. Could you provide more information as to what you're trying to do?Christopher Reed"The oxen are slow, but the earth is patient." Hi, As far as I know, we have no luck to control the load order of the Web controls on a page. However, if we need to load some controls under given conditions, we can achieve this by adding them dynamically instead.Forwa...

Urgent!!! Form inside Windows form control (that is embedded to Web page i.e aspx), not displayed on button click
 Hi all I have a Windows Form Control(i.e Windows Control Library), that consists of a Button "button1" and  a panel "panel1". Also I have a form "form1.cs". On form "form1.cs" I have a label whose text value i have set to  "Hi". On click of button "button1" on windows form control i am opening the form "form1.cs"  inside a panel and it displays "Hi". The code of windows form control is as below :  public partial class UserControl1 : UserControl    {    ...

Calling an aspx page on form submission in another aspx page
I have 2 aspx pages called say page1.aspx and page2.aspx, and I am calling the second aspx page on submission of a form in the first page. Thus, I have the following code in page1.aspx <form name="frmtest" id="frmtest" method="post" runat="server" action="page2.aspx"> Now, when I submit the form, I am getting the following error message: The viewstate is invalid for this page and might be corrupted. Could anyone help me in this? Thanx in advance. Himanshu Hi, currently ASP.NET sets the action back to itself (...

Web resources about - how can i call controls from ascx (in aspx page) -

Control - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

Obama may be able to do something on gun control at last: reports
US President Barack Obama is expected to announce executive actions expanding background checks on gun sales.

LG’s Amazon Echo-like device controls smart home devices and plays music
... a new device that looks somewhat familiar. The company announced its SmartThinq Hub today, a smart home hub with an LCD display that can control ...

Disable Windows 10 upgrade prompts with GWX Control Panel 1.7
... then the constant "Get Windows 10!" alerts are probably getting annoying. Fortunately you can disable them in seconds with the free GWX Control ...

Obama Touts First Seven Years, Pledges to ‘Move Forward on Our Unfinished Business’ on Gun Control
Obama Rips Congress, Pledges to 'Move Forward on Our Unfinished Business' on Gun Control

Thinga is a kid-friendly search engine with parental controls
Keeping kids away from questionable content while they surf the web is an ongoing battle, and there's a new site that's looking to help. Thinga, ...

The Air Traffic Control System Privatizing A Failed FAA NextGen Program Is Bad For America
For some reason, the sudden din from the aviation cognoscenti and aviation groupies to privatize the air traffic control system has gone way ...

'EQUAL TREATMENT' Call to scrap Va. gov's guards over gun control
'EQUAL TREATMENT' Call to scrap Va. gov's guards over gun control

PHOTOS: Hot To Trot Hunks Can Hardly Control Themselves In Brussels
PHOTOS: Hot To Trot Hunks Can Hardly Control Themselves In Brussels

Poland Lawmakers Approve New Law On State Media Control
WARSAW, Poland (AP) — Poland's parliament on Wednesday approved new legislation that gives the government control of state radio and television ...

Resources last updated: 1/1/2016 10:31:03 PM