我无法在LoginView控件中找到我的标签ID,只是为了解释我想要构建的内容。如果您未登录,则只能看到数据库中的内容,但如果您的ARE已登录,则可以对其进行编辑。但现在我只是需要帮助,使其从数据库中读取如何在ASP.NET LoginView中找到控件?
这里的数据是ASP.NET代码隐藏:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
{
using (SqlConnection connection = new SqlConnection("Data Source=10.138.22.47;Initial Catalog=Student10157;User ID=Studentxxxxx;Password=xxxxxxxxxxxxxxxxx"))
{
connection.Open();
SqlCommand cm = new SqlCommand("Select * from Content_Text", connection);
SqlDataReader dr;
dr = cm.ExecuteReader();
if (dr.Read())
{
lblLeft.text = dr["Text"].ToString();
}
}
}
}
}
这里是我的ASP.NET代码:
<asp:FormView runat="server" ID="viewdata">
<ItemTemplate>
<asp:LoginView ID="LoginView1" runat="server">
<AnonymousTemplate>
<asp:Label ID="lblLeft" runat="server"></asp:Label>
</AnonymousTemplate>
<LoggedInTemplate>
<asp:TextBox ID="TxBLeft" runat="server" />
</LoggedInTemplate>
</asp:LoginView>
</ItemTemplate>
</asp:FormView>
我已经尝试,因为你可以看到使用以下C#代码的formview,但不工作要么var lblLeft = (Label)viewData.FindControl("lblLeft");
请详细说明“那不行”。你会得到编译错误(如果是的话,哪一个)?运行时你会得到一个异常吗? – Marshall777
如果您打算从db中读取至多1行,请使用ExecuteScalar方法。 – NoChance
为什么你使用FormView? – Grundy