Convert string to a control

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sue

    #1

    Convert string to a control

    All

    I have a bunch of label controls on my form. All those label controls
    will start with the id "lblEffDt_" .
    (lblEffDateValu e_ddlDeliveryMe thod, 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 EnableChangedCo ntrols(string controlName, string effDate)
    {
    ((Label)("lblEf fDateValue_" + controlName)).T ext = effDate;
    }

    and I can call that fucntion as

    EnableChangedCo ntrols("ddlDeli veryMethod", "01/01/07");

    But it erros out with the message

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

    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..

  • AlexS

    #2
    Re: Convert string to a control

    You can do this with reflection. See PropertyInfo.Se tValue 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_sansid@ho tmail.comwrote in message
    news:1184954707 .382906.264310@ 22g2000hsm.goog legroups.com...
    All
    >
    I have a bunch of label controls on my form. All those label controls
    will start with the id "lblEffDt_" .
    (lblEffDateValu e_ddlDeliveryMe thod, 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 EnableChangedCo ntrols(string controlName, string effDate)
    {
    ((Label)("lblEf fDateValue_" + controlName)).T ext = effDate;
    }
    >
    and I can call that fucntion as
    >
    EnableChangedCo ntrols("ddlDeli veryMethod", "01/01/07");
    >
    But it erros out with the message
    >
    "Cannot convert type 'string' to 'System.Web.UI. WebControls.Lab el'
    >
    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..
    >
    >

    Comment

    • Sue

      #3
      Re: Convert string to a control

      On Jul 20, 1:22 pm, "AlexS" <salexru200...@ SPAMrogers.comP LEASE>
      wrote:
      You can do this with reflection. See PropertyInfo.Se tValue 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...@ho tmail.comwrote in message
      >
      news:1184954707 .382906.264310@ 22g2000hsm.goog legroups.com...
      >
      >
      >
      All
      >
      I have a bunch of label controls on my form. All those label controls
      will start with the id "lblEffDt_" .
      (lblEffDateValu e_ddlDeliveryMe thod, 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 EnableChangedCo ntrols(string controlName, string effDate)
      {
      ((Label)("lblEf fDateValue_" + controlName)).T ext = effDate;
      }
      >
      and I can call that fucntion as
      >
      EnableChangedCo ntrols("ddlDeli veryMethod", "01/01/07");
      >
      But it erros out with the message
      >
      "Cannot convert type 'string' to 'System.Web.UI. WebControls.Lab el'
      >
      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(objec t sender, EventArgs e)
      {
      EnableChangedCo ntrols("Text", "1/1/2006");
      }

      private void EnableChangedCo ntrols(string controlName, string
      effDate)
      {
      Label lbl = (Label)FindCont rol("lbl" + controlName);
      lbl.Text = effDate;
      lbl.Visible = true;
      }

      Comment

      Working...