473,625 Members | 3,253 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using ParseControl and Subsequently Getting Specific Control Type

Using .NET 3.5, I'm wondering how to get a control's specific type (e.g,.
"Button" and not simply "Control") after the control is inserted into a
control hierarchy - when ParseControl is used.

For example, I have the following code:

string str = "<table border=1><tr><t d>Hello
World</td></tr><tr><td><asp :Button ID=\"TestButton \" onclick=\"b_Cli ck\"
runat=\"server\ " Text=\"Button\" /></td></tr><tr><td>Good bye
Everybody!</td></tr></table>";
Control ctl = ParseControl(st r);
this.form1.Cont rols.Add(ctl);

This code inserts an html table that contains a Button into the
form1.Controls collection.

My question: How can I subsequently retrieve a Button type reference to the
inserted Button?

What I'm trying to do is implement logic that walks the control tree looking
for any Button and TextBox objects that may have been inserted into the
control tree. This logic may or may not encounter Button and/or TextBox
controls - depending on the contents of the string passed to ParseControl.
This logic that walks the control tree does not (and cannot) know ahead of
time if there are actually any Button or TextBox objects in the Control
hierarchy. So I can't use FindControl to find any particular Button or
TextBox instances.

Thanks.


Jun 27 '08 #1
1 1818
I found the answer:
In brief, you just cast the 'Control' typed object to the desired object
type, as in the following code.

There is really no mystery here and nothing at all complicated. The reason I
thought something was difficult was because I had a bug in my code in which
the recursive call passed the current control [in the recursive call] and
not the current child control of the current control. Consequently, the
recursive calls never went beyond the current control. Total error on my
part.

But for those interested, here is a method [sans bug] that will get the
first control of the type you want to retrieve from a control collection
[possibly populated via ParseControl]:
----------------------------------------------
private T FindFirstContro lRecursive<T>(C ontrol control) where T : Control
{
string theType = string.Empty;
foreach (Control subcontrol in control.Control s)
{
if (subcontrol is T)
{
return (T)subcontrol;
}
T nestedcontrol = FindFirstContro lRecursive<T>(s ubcontrol);
if (nestedcontrol != null)
return nestedcontrol;
}
return null;
}
----------------------------------------------
"Jordan S." <A@B.comwrote in message
news:OW******** ********@TK2MSF TNGP04.phx.gbl. ..
Using .NET 3.5, I'm wondering how to get a control's specific type (e.g,.
"Button" and not simply "Control") after the control is inserted into a
control hierarchy - when ParseControl is used.

For example, I have the following code:

string str = "<table border=1><tr><t d>Hello
World</td></tr><tr><td><asp :Button ID=\"TestButton \" onclick=\"b_Cli ck\"
runat=\"server\ " Text=\"Button\" /></td></tr><tr><td>Good bye
Everybody!</td></tr></table>";
Control ctl = ParseControl(st r);
this.form1.Cont rols.Add(ctl);

This code inserts an html table that contains a Button into the
form1.Controls collection.

My question: How can I subsequently retrieve a Button type reference to
the inserted Button?

What I'm trying to do is implement logic that walks the control tree
looking for any Button and TextBox objects that may have been inserted
into the control tree. This logic may or may not encounter Button and/or
TextBox controls - depending on the contents of the string passed to
ParseControl. This logic that walks the control tree does not (and cannot)
know ahead of time if there are actually any Button or TextBox objects in
the Control hierarchy. So I can't use FindControl to find any particular
Button or TextBox instances.

Thanks.




Jun 27 '08 #2

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

Similar topics

121
10012
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode support IDEs are DreamWeaver 8 and Zend PHP Studio. DreamWeaver provides full support for Unicode. However, DreamWeaver is a web editor rather than a PHP IDE. It only supports basic IntelliSense (or code completion) and doesn't have anything...
1
3394
by: Matthias Lohrer | last post by:
Hi, I'm playing around with the possibilities of Page.ParseControl. I parse a string with an input-field and an RequiredFieldValidator-control. For testing the server-side validation I disable JavaScript and get this scenario: - When you enter a name, no warning appears, that's good! - When you don't enter a name, the warning appears, that's very good!
1
1920
by: Jeremy McPeak | last post by:
Good morning. I am having a problem with the TemplateControl.ParseControl() method. It does parse the string into a control; however, added attributes are left out of the parsing and make it to the final HTML code. For example: ParseControl("<asp:button OnClick="Btn_Click" text='Click here!' runat='server' />"); Becomes this HTML:
2
1485
by: Chris Simeone | last post by:
When I use ParseControl in a C# code behind to create a button... Control c3 = ParseControl("<asp:button id='Button3' text='Btn3' oncommand='OnButton' commandname='Btn' commandargument='b3' runat='server' />"); The HTML that's rendered looks like this... <input type="submit" name="Button3" value="Btn3" id="Button3" oncommand="OnButton" />
0
1405
by: PB | last post by:
According to the documentation, ParseControl returns a parsed control from an input string. The documentation seems to assume that *one* control will be parsed from the string. My implementation will require an unknown number of controls (always at least one) to be parsed from the string... after which I inject the resulting HTML into a PlaceHolder control (see code below). In my testing, it appears that ParseControl will successfully...
21
2933
by: matvdl | last post by:
I have a system that was originally developed in asp - the pages are saved in SQL (there are over 10,000 pages) and saved to a temp directory in the server when requested by a client. I have updated this system and changed the pages that are saved to the server as aspx - everything works fine and pages can be served - but Its not impossible for a single client to request 100 plus pages in one session - as each page is requested it is...
0
1138
by: Fox | last post by:
Greetings, How can i parse inline code? I have the following code: System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append("<asp:TextBox id=\"myControl\" runat=\"server\" />"); sb.Append("<%= \"Hello!\"%>");
5
3175
by: rajdevramasamy | last post by:
Hi, In my webpage, i am adding my user control using rendercontrol() using the following code: Dim NumericEditControl As New Control NumericEditControl = LoadControl("Controls\NumericEdit.ascx") Dim sb As New StringBuilder() Dim sw As New StringWriter(sb) Dim htw As New HtmlTextWriter(sw)
2
1251
by: anthonykallay | last post by:
Hi there, I have a built a CMS system that allows users to do all the usual functions (add/edit pages), what i would like is for the user to be able to be able to insert a pre defined control into the page text using the editor that loads into a Literal control.. For example they may write out apage as below <p>This is para1</p>
0
8256
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
8189
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
8694
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
8635
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
8497
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
7184
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
4089
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...
1
2621
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
1
1803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.