我想动态设置Label1的值. [英] I want to set the value for Label1 dynamically.

查看:111
本文介绍了我想动态设置Label1的值.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,

我正在用C#在ASP.NET中制作一个小程序,但无法从数据库表中设置标签名称.


表名称:列

col_id col_name
======= =========================
标签1城市ID
标签2城市名称
标签3城市地址


我想从列"表中设置所有标签.

此函数将在Page Load()时调用


我的代码我不确定这是真的"的更多详细信息


在Class文件Main.cs


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


 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 "";
}







此函数正在调用Default.aspx的Page_Load








This Function is calling on Page_Load of Default.aspx


protected void Page_Load(object sender, EventArgs e)
    {
      //Passing value of p_opt_id
      Main.GetColum_Des("001001001")
    }






期待您的帮助

[edit]编辑以提高可读性[/edit]






Expecting Your Help

[edit] edited for more readability[/edit]

推荐答案

我在代码中不知道不安全的用法.无论如何,这将对您有帮助

I don''t know the use of unsafe here in your code. anyway this will help you

public static string GetColum_Des(string p_opt_id,Label label1,Label label2)
   {
       //do your DB opertation
       //this value from your code
       label1.Text = "str";
       //this value from your code
       label2.Text = "str2";
       return "";
   }









protected void Page_Load(object sender, EventArgs e)
   {
       //labels in your page.
       GetColum_Des("your p_opt_id", Label1, Label2);
   }


这可能会帮助您..
我正在考虑您有一个名为
的数据库表 数据和结构为列"的

col_id ----- col_name ---- opt_ID
______ _________ ________

lbl1 --------- Asp ---------- 1
lbl2 ------ --- C#---------- 2
lbl3 --------- VB ---------- 22
lbl4 --------- Ado --------- 13

正如您所说的,您的网页上有标签...
假设您已将容器控件"中的标签"指定为面板"为
"panel1"作为ID ....
并且您已经按照数据库表中的方式设置了标签ID
列"列"col_id" ...

现在是代码了.
may be this will help you..
i am considering that you have a Database table named as
"Columns" whose data and structure is

col_id ----- col_name ---- opt_ID
______ _________ ________

lbl1 --------- Asp ---------- 1
lbl2 ------ --- C# ---------- 2
lbl3 --------- VB ---------- 22
lbl4 --------- Ado --------- 13

and as you said you have the Labels in on you web Page...
suppose you have assigned that Labels in a Container Control say Panel as
''panel1'' as id....
and you have set your Labels ids as in the Database table
"Columns" column "col_id"...

now here is the Code..
/*for getting the Collection of Label Controls from your Container Control say Panel whose id is "panel1" into Collection "labels"*/
var labels = from Control c in panel1.Controls where c.GetType().Equals(typeof(Label)) select c;

 con.Open();
 cmd = new SqlCommand("select col_id,col_name from Columns where opt_ID=@p_opt_id ", con);
 cmd.Parameters.AddWithValue("@p_opt_id", p_opt_id);
  rd = cmd.ExecuteReader();
 while (rd.Read())
 {
    foreach(Control c in labels)/*looping only the Labels
                                Collection of "panel1"*/
    {

       Label l = (Label)c;
       if(l!=null)
       {
           if(l.id==rd["col_id"].ToString())
                l.Text=rd["col_name"].ToString();/*you can set
                                                 various
                                                 Properties of
                                             Label Control here*/
       }
    }
 }


并在面板"panel1"中设置标签的ID..
使用可以设置和获取的标签控件的"ID"属性....
作为... Label1.ID="lbl1";

而且我不知道您的代码中什么是不安全的"....
希望对您有帮助
可能是我的回答很不相关....
如果是这样,对不起朋友……


and to set the ID''s of your Labels in Your Panel "panel1"..
use the "ID" Property of Label Control that can be set and get....
as... Label1.ID="lbl1";

and i dont know what is "unsafe" in your code....
hope this will help you
may be i am answering quite irrelevant....
if so, sorry friends......


您的页面上是否有Label Control Label1.如果没有创建控件



受保护的void Page_Load(对象发送者,EventArgs e)
{
标签Label1 =新Label();
Main.GetColum_Des("001001001")//p_opt_id的传递值
}

我认为这对您有帮助....:)
do u have Label Control Label1 on your page. If not create a control



protected void Page_Load(object sender, EventArgs e)
{
Label Label1=new Label();
Main.GetColum_Des("001001001")//Passing value of p_opt_id
}

I think it help u.... :)


这篇关于我想动态设置Label1的值.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆