如何在没有代码的情况下从sql获取列值到标签

问题描述 投票:-1回答:1

我是编程新手,请告诉我如何从MYSql数据库中检索到的列值到标签。我从sql获得的数据如下

 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ShopsConnectionStringM"].ConnectionString);
 con.Open();
 sda = new SqlDataAdapter("Select UserName,Email,ShopCountry,PIN,ShopName,ShopCity,ShopDistrict,ShopState, Location,ShopBoard from Shops_Table where UserName= '" + HiddenField1.Value + "'", con);
 DataTable dt = new DataTable();
 sda.Fill(dt);

现在我有一个asp .net标签

<asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>

如何获得Label5的“电子邮件”列的值?

有可能这样做吗?

<asp:Label ID="Label5" runat="server" Text='<%#Eval("Email")%>'></asp:Label>

我不想使用后面的代码或c#。

请帮忙。

asp.net
1个回答
1
投票

有很多方法可以实现这一点。一个人可能正在使用FormView:

标记

<asp:FormView ID="formView" runat="server">
    <ItemTemplate>
        <span><%# Eval("Email") %></span>
    </ItemTemplate>
</asp:FormView>

代码旁边

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ShopsConnectionStringM"].ConnectionString);
con.Open();
sda = new SqlDataAdapter("Select UserName,Email,ShopCountry,PIN,ShopName,ShopCity,ShopDistrict,ShopState, Location,ShopBoard from Shops_Table where UserName= '" + HiddenField1.Value + "'", con);
DataTable dt = new DataTable();
sda.Fill(dt);
formView.DataSource = dt;
formView.DataBind();

另一种方法可能是使用Page DataBind,例如:

标记

<asp:Label ID="Label5" runat="server" Text='<%# Email %>'></asp:Label>

代码旁边

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ShopsConnectionStringM"].ConnectionString);
    con.Open();
sda = new SqlDataAdapter("Select UserName,Email,ShopCountry,PIN,ShopName,ShopCity,ShopDistrict,ShopState, Location,ShopBoard from Shops_Table where UserName= '" + HiddenField1.Value + "'", con);
DataTable dt = new DataTable();
sda.Fill(dt);

Email = dt.Rows[0].Field<string>("Email);
DataBind(); //This is the Page data bind

您需要将电子邮件声明为属性。

protected string Email { get; set; }
© www.soinside.com 2019 - 2024. All rights reserved.