473,403 Members | 2,354 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,403 software developers and data experts.

about FindControl ---> to find the control added by program

hi, everyone,
my Page_Load fill tblProducts with some TextBox.

<form runat="server">
<asp:Table id="tblProducts" runat="server"></asp:Table>
<hr>
<asp:Button id="Button1" onclick="Button1_Click" runat="server"
Text="Button"></asp:Button>
<hr>
<hr>
<asp:TextBox id="test" runat="server"/>

</form>

the method is :

TextBox getTextBox() {
TextBox txt = new TextBox();
txt.ID = "txtNum" + Convert.ToString(TextBoxCnt);
TextBoxCnt ++;

return txt;
}
when I use :
TextBox txt = (TextBox)Page.FindControl("txtNum" + Convert.ToString(i));

in the script of Button_Click, I cannot find the control. but in
Page_Load , I can get it.

And in Button_Click, TextBox "test" can be find.

How can I find the program-added controls in Button_Click?

thanks.

Nov 18 '05 #1
4 2657
Edward,

My guess is that you may be adding the controls only on page load and not
post back.

For dynamically added controls to be accessible on post back they must be
added to the page again.

I have an example of this (in VB.Net, but you should get the idea) on my web
site, www.aboutfortunate.com, just go to the code library (link on the top
right of page) and search for "Dynamically add text boxes".

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Edward" <zi***@citiz.net> wrote in message
news:e4**************@TK2MSFTNGP10.phx.gbl...
hi, everyone,
my Page_Load fill tblProducts with some TextBox.

<form runat="server">
<asp:Table id="tblProducts" runat="server"></asp:Table>
<hr>
<asp:Button id="Button1" onclick="Button1_Click" runat="server"
Text="Button"></asp:Button>
<hr>
<hr>
<asp:TextBox id="test" runat="server"/>

</form>

the method is :

TextBox getTextBox() {
TextBox txt = new TextBox();
txt.ID = "txtNum" + Convert.ToString(TextBoxCnt);
TextBoxCnt ++;

return txt;
}
when I use :
TextBox txt = (TextBox)Page.FindControl("txtNum" + Convert.ToString(i));
in the script of Button_Click, I cannot find the control. but in
Page_Load , I can get it.

And in Button_Click, TextBox "test" can be find.

How can I find the program-added controls in Button_Click?

thanks.

Nov 18 '05 #2
Hi,

A pretty good article is given in Microsoft KB "HOW TO: Dynamically Create Controls in ASP.NET by Using Visual C# .NET",

http://support.microsoft.com/default...b;EN-US;317794

Hope this link will be useful for u.

bye,

Regards,
Kamal T.

"Edward" wrote:
hi, everyone,
my Page_Load fill tblProducts with some TextBox.

<form runat="server">
<asp:Table id="tblProducts" runat="server"></asp:Table>
<hr>
<asp:Button id="Button1" onclick="Button1_Click" runat="server"
Text="Button"></asp:Button>
<hr>
<hr>
<asp:TextBox id="test" runat="server"/>

</form>

the method is :

TextBox getTextBox() {
TextBox txt = new TextBox();
txt.ID = "txtNum" + Convert.ToString(TextBoxCnt);
TextBoxCnt ++;

return txt;
}
when I use :
TextBox txt = (TextBox)Page.FindControl("txtNum" + Convert.ToString(i));

in the script of Button_Click, I cannot find the control. but in
Page_Load , I can get it.

And in Button_Click, TextBox "test" can be find.

How can I find the program-added controls in Button_Click?

thanks.

Nov 18 '05 #3
Thanks, Justin.

You are right. I put the code outside "postback=false".

although I got the result, but I was confused about the mechanism, How can
the textbox's value be refreshed when the second-Page_load runs ?

Thanks from heart.

"S. Justin Gengo" <sj*****@aboutfortunate.com> дÈëÏûÏ¢ÐÂÎÅ
:10*************@corp.supernews.com...
Edward,

My guess is that you may be adding the controls only on page load and not
post back.

For dynamically added controls to be accessible on post back they must be
added to the page again.

I have an example of this (in VB.Net, but you should get the idea) on my web site, www.aboutfortunate.com, just go to the code library (link on the top
right of page) and search for "Dynamically add text boxes".

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Edward" <zi***@citiz.net> wrote in message
news:e4**************@TK2MSFTNGP10.phx.gbl...
hi, everyone,
my Page_Load fill tblProducts with some TextBox.

<form runat="server">
<asp:Table id="tblProducts" runat="server"></asp:Table>
<hr>
<asp:Button id="Button1" onclick="Button1_Click" runat="server"
Text="Button"></asp:Button>
<hr>
<hr>
<asp:TextBox id="test" runat="server"/>

</form>

the method is :

TextBox getTextBox() {
TextBox txt = new TextBox();
txt.ID = "txtNum" + Convert.ToString(TextBoxCnt);
TextBoxCnt ++;

return txt;
}
when I use :
TextBox txt = (TextBox)Page.FindControl("txtNum" +

Convert.ToString(i));

in the script of Button_Click, I cannot find the control. but in
Page_Load , I can get it.

And in Button_Click, TextBox "test" can be find.

How can I find the program-added controls in Button_Click?

thanks.


Nov 18 '05 #4
Edward,

Each control's properties are gathered upon page load. .NET runs through
each control on page load and fills it's properties from the post, but it
only loops through controls that exist. So if a control isn't recreated the
control is never matched up with the post from the client.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Edward" <zi***@citiz.net> wrote in message
news:uh**************@TK2MSFTNGP12.phx.gbl...
Thanks, Justin.

You are right. I put the code outside "postback=false".

although I got the result, but I was confused about the mechanism, How can
the textbox's value be refreshed when the second-Page_load runs ?

Thanks from heart.

"S. Justin Gengo" <sj*****@aboutfortunate.com> дÈëÏûÏ¢ÐÂÎÅ
:10*************@corp.supernews.com...
Edward,

My guess is that you may be adding the controls only on page load and not post back.

For dynamically added controls to be accessible on post back they must be added to the page again.

I have an example of this (in VB.Net, but you should get the idea) on my

web
site, www.aboutfortunate.com, just go to the code library (link on the top right of page) and search for "Dynamically add text boxes".

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Edward" <zi***@citiz.net> wrote in message
news:e4**************@TK2MSFTNGP10.phx.gbl...
hi, everyone,
my Page_Load fill tblProducts with some TextBox.

<form runat="server">
<asp:Table id="tblProducts" runat="server"></asp:Table>
<hr>
<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button>
<hr>
<hr>
<asp:TextBox id="test" runat="server"/>

</form>

the method is :

TextBox getTextBox() {
TextBox txt = new TextBox();
txt.ID = "txtNum" + Convert.ToString(TextBoxCnt);
TextBoxCnt ++;

return txt;
}
when I use :
TextBox txt = (TextBox)Page.FindControl("txtNum" +

Convert.ToString(i));

in the script of Button_Click, I cannot find the control. but in
Page_Load , I can get it.

And in Button_Click, TextBox "test" can be find.

How can I find the program-added controls in Button_Click?

thanks.



Nov 18 '05 #5

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

Similar topics

3
by: P.L. | last post by:
Hi! I have problem with Datarepeater control. When I load template from external aspx file ItemTemplate = Page.LoadTemplate(....); I can't find control in ItemDataBound event. ...
2
by: Josh | last post by:
Hi Guys, I have been stuck on this problem for several days now, i have a set of nested datagrids. Inside the second datagrid i have a dropdown list, a textbox and a label. I want the textbox...
3
by: Moojjoo | last post by:
I created an user control (.ascx file) with a DropDownList (DDL) with the following code behide: public string Selected_dept_value() { string dept_value = dept_dropdown.SelectedValue; return...
5
by: sck10 | last post by:
Hello, I am using the code below to set the values of a DetailsView template field using FindControl. My question is how would you find a control if its a Boundfield control? For example,...
1
by: Salim | last post by:
Hi, I'm trying to get UniqueID of a linkbutton. I have 2 web user controls. And a master page. In fisrst web user control there is a datalist. In datalist ItemCreated event, I try to find...
2
by: vijaya683 | last post by:
what is find control
1
by: shapper | last post by:
Hello, I have a custom control, MyCustomControl, with a property named Childs of type Generic.List(Of MyChildControl). I added a MyCustomControl to my page and added a few MyChildControls to...
1
by: =?Utf-8?B?aUhhdkFRdWVzdGlvbg==?= | last post by:
I am using a menu control in which i have a tab controld in which i have a dropdownlilst How do i find the values in a control. I have tried this in C# ?LTCMenu1.FindControl] Can any one...
7
by: =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?= | last post by:
i have a master page and aseries of controls labeled fn1, fn2, fn3 , ... i want ot loop and use find control but i'm finding them. i'm using the following w/o luck ContentPlaceHolder cph =...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.