从列中获取数据到多个标签 [英] Get data from column into multiple labels
问题描述
你好专家
我有一个asp.net c#网站应用程序,它的数据库是sql server。
我必须获取有关他们工作的字段的用户信息。所以我使用了一个列表框并将一些初始数据放入其中。用户从此列表框中选择一些项目,数据仅存储在以逗号分隔的数据库表的一列中。
例如:
列表框有值:category-subcatagory
学校宿舍-cbse
school hostel-icse
hotel-2star
movie-english etc
现在如果用户选择第一个和第二个选项,那么数据库表中的列将具有
school hostel-cbse,school hostel-icse表格。
我能够做到这一点!
现在的问题是我想从网站中的数据库中以不同的标签显示这些选定的字段。
so我如何从列中抽取单个字段?并在标签中显示。
我可以根据列中的字段数创建动态标签控件吗?
我不喜欢知道从哪里开始:(
请帮助!!
在你的案例中查看这个
string strColumn = school hostel-cbse,school hostel-icse; // In你的情况你的专栏
string [] strSplitValues = strColumn.Split(' ,'); // 您使用的是什么分隔符,例如逗号
label1.text = strSplitValues [ 0 ]。ToString();
label2.text = strSplitValues [< span class =code-digit> 1 ]。ToString() ;
// 您还可以使用for循环来分配 $在aspx页面中的$ p>
< 表格 >
< asp:Repeater ID = Repeater1 runat = server >
< ItemTemplate >
&l t; tr >
< td >
< asp:标签 ID = lblval runat = server 文字 =' <% #Eval( your fieldname )%> ' > < / asp:Label >
< / td >
< / tr >
< / ItemTemplate >
< / asp:Repeater >
< / table >
CS Page
protected void Page_Load(object sender,EventAr gs e)
{
if(!IsPostback)
{
SqlConnection conn = new SqlConnection(你的连接字符串);
SqlDataAdapter da = new SqlDataAdapter(select command,conn);
DataSet ds = new DataSet();
da.Fill(ds);
Repeater1.DataSource = ds.Tables [0];
Repeater1.DataBind();
}
}
我能够在solution1的帮助下解决我的问题。
这是必需的代码。
protected void Page_Load(object sender,EventArgs e)
{
string data =school hostel-cbse,school hostel-icse;
string [] values = data.Split(new char [] {','},StringSplitOptions.RemoveEmptyEntries);
foreach(字符串v in values)
{
Label l = new Label();
l.Text = v;
Panel1.Controls.Add(l);
Panelr1.Controls.Add(new Literal {text =< br / > });
}
}
Hello Experts
I have a asp.net c# website application and its database is sql server.
In that I had to get user information about in which field they work. So i used a list box and had some initial data into it. User choose some items from this list box and the data is stored in just ONE column of database table separated by comma.
Example:
Listbox has values: category-subcatagory
school hostel-cbse
school hostel-icse
hotel-2star
movie-english etc
now if user selects 1st and 2nd option then the column in database table will have value in
"school hostel-cbse,school hostel-icse" form.
I was able to do that!
Now the problem is I want to show these selected fields from database in website in different labels.
so How can I abstract single field from the column? and show it in a label.
Can I create Dynamic Label control according to the number of fields in the column?
I don't know where to start :(
Please Help!!
Check out this in your case
string strColumn = "school hostel-cbse,school hostel-icse"; // In your case your column string[] strSplitValues = strColumn.Split(','); //Whatever separator you use e.g comma label1.text = strSplitValues[0].ToString(); label2.text = strSplitValues[1].ToString(); //You can also use for loop for assigning
in aspx page <table> <asp:Repeater ID="Repeater1" runat="server"> <ItemTemplate> <tr> <td> <asp:Label ID="lblval" runat="server" Text='<%#Eval("your fieldname") %>'></asp:Label> </td> </tr> </ItemTemplate> </asp:Repeater> </table> in CS Page protected void Page_Load(object sender, EventArgs e) { if(!IsPostback) { SqlConnection conn = new SqlConnection(your connection string); SqlDataAdapter da = new SqlDataAdapter("select command", conn); DataSet ds = new DataSet(); da.Fill(ds); Repeater1.DataSource = ds.Tables[0]; Repeater1.DataBind(); } }
I was able to solve my problem with the help of solution1.
this is the required code.
protected void Page_Load(object sender, EventArgs e) { string data = "school hostel-cbse,school hostel-icse"; string[] values = data.Split(new char[] { ',' },StringSplitOptions.RemoveEmptyEntries); foreach(string v in values) { Label l = new Label(); l.Text = v; Panel1.Controls.Add(l); Panelr1.Controls.Add(new Literal { Text = "<br/>" }); } }
这篇关于从列中获取数据到多个标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!