i want to set the value for Label1 dynamically.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • saifulhaque
    New Member
    • Aug 2010
    • 16

    i want to set the value for Label1 dynamically.

    Hi dear,

    I am making a small program in ASP.NET in C# but am not able to set the label names from a Database table.


    Table Name: Columns

    col_id col_name
    ======= =============== ===========
    Label1 City ID
    Label2 City Name
    Label3 City Addrs


    i want to set all the lables from the table "Columns".

    This function will be called at the time of Page Load()


    More Details of My code "I am not Sure It is True"

    In Class file Main.cs

    Code:
    
     public static string GetColum_Des(string p_opt_id)
    
    {
    
        string str = System.Configuration.ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
      
        SqlCommand cmd;
        SqlDataReader rd;
        SqlConnection con = new SqlConnection(str);
       
              
            
        unsafe
        {
            string str1, str2;
           
            
            con.Open();
            cmd = new SqlCommand("select col_id,col_name from LOCATIN_DETAIL where opt_ID=@p_opt_id ", con);
            cmd.Parameters.AddWithValue("@p_opt_id", p_opt_id);
             rd = cmd.ExecuteReader();
            while (rd.Read())
            {
                str1 = rd["col_id"].ToString() + "Text";//I want to get Label Id from here +.Text Means "Label1.Text" in str
                str2=rd["col_name"].ToString();//I want to get Label value from here  which had stored in database fied col_name
                str1 = str2; //Means Label1.Text=str2 is it possible
            }
        }
    
        return "";
    }
    This Function is calling on Page_Load of Default.aspx

    Code:
      protected void Page_Load(object sender, EventArgs e)
        {
    Main.GetColum_Des("001001001")//Passing value of p_opt_id
    }
    Expecting Your Help
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    You are using a pretty strange approach but it's possible. Use the FindControl(... ) method to retrieve the control that matches the ID, cast the control into a Label and then set the Text for that label.

    For example
    Code:
    while (rd.Read())
    {
      string labelID = rd["col_id"].ToString();
      string text = rd["col_name"].ToString();
      
      Label lbl = (Label)this.FindControl(labelID);
      
      if(lbl != null)
      { 
        lbl.Text = text;
      }
    
    }
    -Frinny
    Last edited by Frinavale; Aug 4 '10, 03:15 PM. Reason: Changed "Me" to "this"

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Sorry I just realized that I had the VB.NET Keyword "me" in my code instead of the C# keyword "this" .

      Fixed it.

      -Frinny

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Saifulhaque, I do not help people through PMs.
        Please respond to my suggestions in thread instead of through PM.

        This is what you sent me in PM:
        Originally posted by saifulhaque
        Dear

        Now I am getting this Two error Messages

        Error 1 . Keyword 'this' is not valid in a static property, static method, or static field initialize

        Error 2 'Class' does not contain a definition for 'FindControl'
        Edit: I did not have a clear picture of what you were doing.

        -Frinny
        Last edited by Frinavale; Aug 4 '10, 04:02 PM. Reason: Updated reply after re-reading the question

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Ah I should have read your question better.

          Your Labels are located on your Page, however the code that fetches the Text for the Labels is in a class acting as the "data access layer".

          Therefore, my suggestion earlier will not work.

          Instead change your method to accept the Page that contains the Label controls as a parameter:
          Code:
          public static string GetColum_Des(string p_opt_id, Page thePage)
          Then change my code to the following:
          Code:
          while (rd.Read())
          {
            string labelID = rd["col_id"].ToString();
            string text = rd["col_name"].ToString();
           
            Label lbl = (Label)thePage.FindControl(labelID);
           
            if(lbl != null)
            { 
              lbl.Text = text;
            }
           
          }
          When you call the method be sure to pass it the Page...

          -Frinny

          Comment

          • saifulhaque
            New Member
            • Aug 2010
            • 16

            #6
            Here one doubt how can i pass thepage parameter as Page

            Here suppose my Label containing page is Default.aspx

            how i can pass this page as parameter.

            public static string GetColum_Des("0 01001001", "Default.as px") //getting Error

            Comment

            Working...