我無法在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