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

Convert string to a control

Sue
All

I have a bunch of label controls on my form. All those label controls
will start with the id "lblEffDt_".
(lblEffDateValue_ddlDeliveryMethod, lblEffDateValue_txtPriority
etc...)

I want to have a common function, which receives the id of the
control as a string (which would be the string after the _ underscore
in the lblEffDate) along with actual value and based on it set some
value.

private void EnableChangedControls(string controlName, string effDate)
{
((Label)("lblEffDateValue_" + controlName)).Text = effDate;
}

and I can call that fucntion as

EnableChangedControls("ddlDeliveryMethod", "01/01/07");

But it erros out with the message

"Cannot convert type 'string' to 'System.Web.UI.WebControls.Label'

Is there an easier way to do this. I have whole lot of controls on the
webform and doing something similar will help.

thanks
sue..

Jul 20 '07 #1
2 3591
You can do this with reflection. See PropertyInfo.SetValue method and
related samples in VS help or in MSDN

Or you can enumerate Controls collection of the form - unchecked code:

foreach (Control c in form.Controls) {
if (c is Label) {
if (c.Name == yourName) {....}
}
}

"Sue" <iy*********@hotmail.comwrote in message
news:11**********************@22g2000hsm.googlegro ups.com...
All

I have a bunch of label controls on my form. All those label controls
will start with the id "lblEffDt_".
(lblEffDateValue_ddlDeliveryMethod, lblEffDateValue_txtPriority
etc...)

I want to have a common function, which receives the id of the
control as a string (which would be the string after the _ underscore
in the lblEffDate) along with actual value and based on it set some
value.

private void EnableChangedControls(string controlName, string effDate)
{
((Label)("lblEffDateValue_" + controlName)).Text = effDate;
}

and I can call that fucntion as

EnableChangedControls("ddlDeliveryMethod", "01/01/07");

But it erros out with the message

"Cannot convert type 'string' to 'System.Web.UI.WebControls.Label'

Is there an easier way to do this. I have whole lot of controls on the
webform and doing something similar will help.

thanks
sue..


Jul 20 '07 #2
Sue
On Jul 20, 1:22 pm, "AlexS" <salexru200...@SPAMrogers.comPLEASE>
wrote:
You can do this with reflection. See PropertyInfo.SetValue method and
related samples in VS help or in MSDN

Or you can enumerate Controls collection of the form - unchecked code:

foreach (Control c in form.Controls) {
if (c is Label) {
if (c.Name == yourName) {....}
}

}
"Sue" <iyer_san...@hotmail.comwrote in message

news:11**********************@22g2000hsm.googlegro ups.com...
All
I have a bunch of label controls on my form. All those label controls
will start with the id "lblEffDt_".
(lblEffDateValue_ddlDeliveryMethod, lblEffDateValue_txtPriority
etc...)
I want to have a common function, which receives the id of the
control as a string (which would be the string after the _ underscore
in the lblEffDate) along with actual value and based on it set some
value.
private void EnableChangedControls(string controlName, string effDate)
{
((Label)("lblEffDateValue_" + controlName)).Text = effDate;
}
and I can call that fucntion as
EnableChangedControls("ddlDeliveryMethod", "01/01/07");
But it erros out with the message
"Cannot convert type 'string' to 'System.Web.UI.WebControls.Label'
Is there an easier way to do this. I have whole lot of controls on the
webform and doing something similar will help.
thanks
sue..- Hide quoted text -

- Show quoted text -
All

I found an easier solution.

<form id="form1" runat="server">
<asp:Label runat="server" id="lblText"</asp:Label>
</form>

protected void Page_Load(object sender, EventArgs e)
{
EnableChangedControls("Text", "1/1/2006");
}

private void EnableChangedControls(string controlName, string
effDate)
{
Label lbl = (Label)FindControl("lbl" + controlName);
lbl.Text = effDate;
lbl.Visible = true;
}

Jul 24 '07 #3

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

Similar topics

4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
3
by: dale zhang | last post by:
Hi, I am trying to read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp The article author is using PictureBox for windows...
7
by: Scott Schluer | last post by:
Is there a way to use the Image class to convert a color photo (GIF or JPEG) to a B&W photo? Thanks, Scott
10
by: Nikolay Petrov | last post by:
How can I convert DOS cyrillic text to Unicode
14
by: Me | last post by:
Hi all I am getting a really bizzare error on when I convert a string into a datetime: The code is : DateTime dt1 = Convert.ToDateTime("10 Sep 2005"); Console.WriteLine(dt1.Year);
5
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public...
2
by: suji | last post by:
Hai, Are there any 3rd party controls who provide free libraries to convert my html form to pdf in c#? I need to have a button on my form, that should convert my page to pdf format. Can any one...
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
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
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,...
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.