473,769 Members | 6,267 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using FindControl() to access controls created by javascript

Hello,

I'm using javascript's insertAdjacentH tml() to insert images to the
webform at runtime. This runs fine(image successfully displayed at the
browser) but when I tried to access the control's src using FindControl()
function in the code-behind, it seems that the control doesn't even exist.

I know that I can add controls on the server-side, but then a postback
will be needed which is the thing I want to avoid. Does anyone have idea on
how to do so? Thanks a lot.

--
If you want to mail to me, please replace all instance of "e" to "a" in my
email address. Thank you.
Sorry for any inconvience caused, but there's many spam-mailers.
Nov 19 '05 #1
7 7001
How can server know what you are doing on client side? If you want server to
be aware of controls, you must make them server ones. What is the purpose of
adding controls? May be you can prepare everything on server side and then
just hide/show them on client?

Eliyahu

"Lau Lei Cheong" <le****@yehoo.c om.hk> wrote in message
news:OR******** ******@TK2MSFTN GP10.phx.gbl...
Hello,

I'm using javascript's insertAdjacentH tml() to insert images to the
webform at runtime. This runs fine(image successfully displayed at the
browser) but when I tried to access the control's src using FindControl()
function in the code-behind, it seems that the control doesn't even exist.

I know that I can add controls on the server-side, but then a postback
will be needed which is the thing I want to avoid. Does anyone have idea on how to do so? Thanks a lot.

--
If you want to mail to me, please replace all instance of "e" to "a" in my
email address. Thank you.
Sorry for any inconvience caused, but there's many spam-mailers.

Nov 19 '05 #2
Hereis how I think about this:
In theory, any htmlcontrol with runat="server" tag within a server-side form
will be posted back to server on the "post" action. So I think by inserting
things such as "<input id=\"txtctrl_1\ " type=\"text\" runat=\"server\ "
value=\"\someth ing">" the control id and it's value will eventally be posted
back to server on a post event within the request stream.
And from my understanding, Page.FindContro l() is simple searching the
returned stream for the specified key and return the result(So that I can
still access the controls created dynamically on the server-side using
Form.Controls.A dd()). Therefore, there's no reason I can think of that will
prevent me to access the controls created by the method stated in the
beginning.

For the second question, the answer is no. The webform will allow users to
add as many pictures(and/or texts) as they wish on it and there is no way I
can predict how much picture a user may enter. (even set a value as high as
100 may be insufficient)

"Eliyahu Goldin" <re************ *@monarchmed.co m> ¦b¶l¥ó
news:eY******** ******@TK2MSFTN GP10.phx.gbl ¤¤¼¶¼g...
How can server know what you are doing on client side? If you want server to be aware of controls, you must make them server ones. What is the purpose of adding controls? May be you can prepare everything on server side and then
just hide/show them on client?

Eliyahu

"Lau Lei Cheong" <le****@yehoo.c om.hk> wrote in message
news:OR******** ******@TK2MSFTN GP10.phx.gbl...
Hello,

I'm using javascript's insertAdjacentH tml() to insert images to the
webform at runtime. This runs fine(image successfully displayed at the
browser) but when I tried to access the control's src using FindControl() function in the code-behind, it seems that the control doesn't even exist.
I know that I can add controls on the server-side, but then a postback will be needed which is the thing I want to avoid. Does anyone have idea

on
how to do so? Thanks a lot.

--
If you want to mail to me, please replace all instance of "e" to "a" in my email address. Thank you.
Sorry for any inconvience caused, but there's many spam-mailers.


Nov 19 '05 #3
ASP.NET controls are abstractions of various user interface elements such as
text fields and drop down lists. The controls on a page are held in the
Form.Controls collection. When ASP.NET renders a page it iterates this
collection and renders every single control as HTML code. The controls are
rendered as HTML elements such as INPUT and SELECT contained with in a HTML
FORM element. When the rendering process is complete the HTML code is
returned to the browser as an HTTP response.
The browser then parses the HTML code and adds the various elements to it's
document object model (DOM) making it possible to use script add, remove or
change HTML elements in the browser.
When a post back is performed the FORM containing is submitted to the
server. The entire DOM is not sent to the server, only the values and
identifiers of the INPUT and SELECT elements are included in the HTTP POST
request.

When you add a new HTML element to the page via script you're only adding it
to the DOM. You can add a runat attribute to any DOM element, but this
attribute is not recognised by the browser and has no function in practice.

If you want to upload the pictures to the server you will have to use a
INPUT type="file" HTML element to include the actual image in the HTTP
request. Just adding the HTML code dynamically won't cut it.

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
Nov 19 '05 #4
Well, you have to correct your understanding. runat=server has no effect on
client. It's a server-side tag. Client doesn't know what it means and just
ignores it.

As to the second question, if you wish to pass images to server, I don't
think you can get much more than standard file uploading one-by-one.
Alternatively you can use/make a special ActiveX control.

Eliyahu

"Lau Lei Cheong" <le****@yehoo.c om.hk> wrote in message
news:OU******** ******@tk2msftn gp13.phx.gbl...
Hereis how I think about this:
In theory, any htmlcontrol with runat="server" tag within a server-side form will be posted back to server on the "post" action. So I think by inserting things such as "<input id=\"txtctrl_1\ " type=\"text\" runat=\"server\ "
value=\"\someth ing">" the control id and it's value will eventally be posted back to server on a post event within the request stream.
And from my understanding, Page.FindContro l() is simple searching the
returned stream for the specified key and return the result(So that I can
still access the controls created dynamically on the server-side using
Form.Controls.A dd()). Therefore, there's no reason I can think of that will prevent me to access the controls created by the method stated in the
beginning.

For the second question, the answer is no. The webform will allow users to
add as many pictures(and/or texts) as they wish on it and there is no way I can predict how much picture a user may enter. (even set a value as high as 100 may be insufficient)

"Eliyahu Goldin" <re************ *@monarchmed.co m> ¦b¶l¥ó
news:eY******** ******@TK2MSFTN GP10.phx.gbl ¤¤¼¶¼g...
How can server know what you are doing on client side? If you want server
to
be aware of controls, you must make them server ones. What is the purpose
of
adding controls? May be you can prepare everything on server side and
then just hide/show them on client?

Eliyahu

"Lau Lei Cheong" <le****@yehoo.c om.hk> wrote in message
news:OR******** ******@TK2MSFTN GP10.phx.gbl...
Hello,

I'm using javascript's insertAdjacentH tml() to insert images to the webform at runtime. This runs fine(image successfully displayed at the
browser) but when I tried to access the control's src using FindControl() function in the code-behind, it seems that the control doesn't even exist.
I know that I can add controls on the server-side, but then a postback will be needed which is the thing I want to avoid. Does anyone have
idea on
how to do so? Thanks a lot.

--
If you want to mail to me, please replace all instance of "e" to "a"

in my email address. Thank you.
Sorry for any inconvience caused, but there's many spam-mailers.



Nov 19 '05 #5
Yes, of course, I've created another form with <input type="file"> to upload
the images, that's why I could simply add an image element to it to show it.

Taking yours and Eliyahu's advice I've created <input type="hidden"> fields
that holds various properties of the controls yet they seem do not return in
the request stream. Any ideas?

"Anders Norås [MCAD]" <an**********@o bjectware.no> ¦b¶l¥ó
news:Ok******** ******@TK2MSFTN GP11.phx.gbl ¤¤¼¶¼g...
ASP.NET controls are abstractions of various user interface elements such as text fields and drop down lists. The controls on a page are held in the
Form.Controls collection. When ASP.NET renders a page it iterates this
collection and renders every single control as HTML code. The controls are
rendered as HTML elements such as INPUT and SELECT contained with in a HTML FORM element. When the rendering process is complete the HTML code is
returned to the browser as an HTTP response.
The browser then parses the HTML code and adds the various elements to it's document object model (DOM) making it possible to use script add, remove or change HTML elements in the browser.
When a post back is performed the FORM containing is submitted to the
server. The entire DOM is not sent to the server, only the values and
identifiers of the INPUT and SELECT elements are included in the HTTP POST
request.

When you add a new HTML element to the page via script you're only adding it to the DOM. You can add a runat attribute to any DOM element, but this
attribute is not recognised by the browser and has no function in practice.
If you want to upload the pictures to the server you will have to use a
INPUT type="file" HTML element to include the actual image in the HTTP
request. Just adding the HTML code dynamically won't cut it.

Anders Norås
http://dotnetjunkies.com/weblog/anoras/

Nov 19 '05 #6
Do your <input type="hidden"> have runat=server?

Eliyahu

"Lau Lei Cheong" <le****@yehoo.c om.hk> wrote in message
news:On******** ******@TK2MSFTN GP14.phx.gbl...
Yes, of course, I've created another form with <input type="file"> to upload the images, that's why I could simply add an image element to it to show it.
Taking yours and Eliyahu's advice I've created <input type="hidden"> fields that holds various properties of the controls yet they seem do not return in the request stream. Any ideas?

"Anders Norås [MCAD]" <an**********@o bjectware.no> ¦b¶l¥ó
news:Ok******** ******@TK2MSFTN GP11.phx.gbl ¤¤¼¶¼g...
ASP.NET controls are abstractions of various user interface elements such
as
text fields and drop down lists. The controls on a page are held in the
Form.Controls collection. When ASP.NET renders a page it iterates this
collection and renders every single control as HTML code. The controls
are rendered as HTML elements such as INPUT and SELECT contained with in a

HTML
FORM element. When the rendering process is complete the HTML code is
returned to the browser as an HTTP response.
The browser then parses the HTML code and adds the various elements to

it's
document object model (DOM) making it possible to use script add, remove

or
change HTML elements in the browser.
When a post back is performed the FORM containing is submitted to the
server. The entire DOM is not sent to the server, only the values and
identifiers of the INPUT and SELECT elements are included in the HTTP POST request.

When you add a new HTML element to the page via script you're only

adding it
to the DOM. You can add a runat attribute to any DOM element, but this
attribute is not recognised by the browser and has no function in

practice.

If you want to upload the pictures to the server you will have to use a
INPUT type="file" HTML element to include the actual image in the HTTP
request. Just adding the HTML code dynamically won't cut it.

Anders Norås
http://dotnetjunkies.com/weblog/anoras/


Nov 19 '05 #7
Yes. But seems all "client-side added" hidden boxes cannot add themselves in
the request stream. (Also examinated request stream to confirm that)

As time is running out I'll settle this by adding a hidden box at design
time, and serialize the image/layers' values using javascript just before a
postback to server.

I'd be appreciated if someone can solve it, as the additional javascript
makes my code grow very large. And is likely to get problem when the
serialized data size exceeds the hidden box's size limit.

"Eliyahu Goldin" <re************ *@monarchmed.co m> ¦b¶l¥ó
news:em******** ******@TK2MSFTN GP09.phx.gbl ¤¤¼¶¼g...
Do your <input type="hidden"> have runat=server?

Eliyahu

"Lau Lei Cheong" <le****@yehoo.c om.hk> wrote in message
news:On******** ******@TK2MSFTN GP14.phx.gbl...
Yes, of course, I've created another form with <input type="file"> to upload
the images, that's why I could simply add an image element to it to show

it.

Taking yours and Eliyahu's advice I've created <input type="hidden">

fields
that holds various properties of the controls yet they seem do not return in
the request stream. Any ideas?

"Anders Norås [MCAD]" <an**********@o bjectware.no> ¦b¶l¥ó
news:Ok******** ******@TK2MSFTN GP11.phx.gbl ¤¤¼¶¼g...
ASP.NET controls are abstractions of various user interface elements such
as
text fields and drop down lists. The controls on a page are held in the Form.Controls collection. When ASP.NET renders a page it iterates this
collection and renders every single control as HTML code. The controls

are rendered as HTML elements such as INPUT and SELECT contained with in a

HTML
FORM element. When the rendering process is complete the HTML code is
returned to the browser as an HTTP response.
The browser then parses the HTML code and adds the various elements to

it's
document object model (DOM) making it possible to use script add, remove or
change HTML elements in the browser.
When a post back is performed the FORM containing is submitted to the
server. The entire DOM is not sent to the server, only the values and
identifiers of the INPUT and SELECT elements are included in the HTTP POST request.

When you add a new HTML element to the page via script you're only

adding
it
to the DOM. You can add a runat attribute to any DOM element, but this
attribute is not recognised by the browser and has no function in

practice.

If you want to upload the pictures to the server you will have to use

a INPUT type="file" HTML element to include the actual image in the HTTP
request. Just adding the HTML code dynamically won't cut it.

Anders Norås
http://dotnetjunkies.com/weblog/anoras/



Nov 19 '05 #8

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

Similar topics

1
7783
by: James G. Beldock | last post by:
I have seen the following behavior: when issuing a Page.FindControl() for a control which exists in an item template (from within an ItemDataBound() event, for example), I get nulls back regularly. Has anyone seen this before? It's pretty aggravating to have to iterate through the controls in each grid cell to find the ones I need, especially since finding those cells is not always easy. Here's my ItemDataBound() handler: private void...
4
2804
by: Kevin Phifer | last post by:
Ok, before anyone freaks out, I have a solution I need to create that gathers content from maybe different places. Each one can return a <form> in the html, so its the classic can't have more than one runat=server form on a asp.net page. However, I still want developers to be able to use asp.net controls to create some apps that are created on the page. So I need multiple forms on a asp.net page(doesn't everyone). I purchased the...
8
7436
by: Adam Billmeier | last post by:
Problem: I am trying to use FindControl to grab a dropdownlist that is in the EditItemTemplate of a Datalist and then Bind it to a dataset. I also then need to put the correct values in all of the text boxes in the EditItemTemplate. I have set the EditItemIndex for the Datalist to -1, and then bound the Datalist. I then want to Bind the DropDown, and populate the values. All attempts to use FindControl on the control return "Nothing"
7
4374
by: tma | last post by:
My code below returns the .text property of the chkEqmtRental check box but not it's checked state. I'm creating the control in a placeholder and need to access the check box in the codebehind. Also, is it possible to access events for that control and if so, how? chkEqmtRental = plh.FindControl("chkEqmtRental")
3
1328
by: Kezza | last post by:
Hi There.. I have dynamically created some textbox and checkbox controls by adding them to a panel. Now I would like to get the values out. I have created a for each loop that I can see the controls Unique ID with. But I for the life of me can't figure out how to see the value the control is storing. Could somebody please help me. I'm thinking that I might need to use something like "findcontrol", but this wouldn't make sense to me. ...
53
4756
by: Hexman | last post by:
Hello All, I'd like your comments on the code below. The sub does exactly what I want it to do but I don't feel that it is solid as all. It seems like I'm using some VB6 code, .Net2003 code, and .Net2005 code. I'm developing in vb.net 2005. This test sub just reads an input text file, writing out records to another text file, eliminating records that have a '99' in them (it is similar to a CSV file). Some of my concerns are:
1
2982
by: kcddoorman | last post by:
I have searched all over the internets for a solution to this problem and there are many ways to work around it but I'm wondering if there is a more simple solution. I have asp.net 2.0 working with SQL Server 2005 Express and IIS 5.0. I made a page that contains the CreateUserWizard so people can get access to rest of my applications. Using the default (asp:CreateUserWizard) allows us to store data such as username, email address, and password...
15
3918
by: | last post by:
I dynamically create controls (textboxes) in a repeater control. I know their names (eg. TextBox1). How do I find the text of TextBox1 in the Form? FindControl does not seem to work.
1
2054
by: ptcoder | last post by:
Hello everyone. I have done a lot of reading regarding the recursive issue with FindControl. I have a FormView with an <EditItemTemplate> and all I want to do is add javascript attributes to the form control like this: smsTxt.Attributes.Add("onkeydown", "backspacerDOWN(this,event);"); My goal is to use JavaScript with the ASP.NET controls. I found some code on another forum to do the recursive lookup and here it is: public static class...
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10045
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...
1
9994
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
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
8872
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...
1
7409
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3959
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

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.