2014-01-09 60 views
0

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

+0

請詳細說明「那不行」。你會得到編譯錯誤(如果是的話,哪一個)?運行時你會得到一個異常嗎? – Marshall777

+2

如果您打算從db中讀取至多1行,請使用ExecuteScalar方法。 – NoChance

+0

爲什麼你使用FormView? – Grundy

回答

1

FormView需要在數據源,所以我認爲你需要somethink像這樣在你的代碼

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()) 
      { 
       viewdata.DataSource = new []{new { N = dr["Text"] }}; 
       viewdata.DataBind(); 

      } 
     } 
    } 
} 

和標記

<asp:FormView runat="server" ID="viewdata"> 
    <ItemTemplate> 
     <asp:LoginView runat="server"> 
      <AnonymousTemplate> 
       <asp:Label ID="lblLeft" runat="server" Text='<%# Eval("N") %>'></asp:Label> 
      </AnonymousTemplate> 
      <LoggedInTemplate> 
       <asp:TextBox ID="TxBLeft" runat="server" /> 
      </LoggedInTemplate> 
     </asp:LoginView> 
    </ItemTemplate> 
</asp:FormView> 

UPDATE
如果你有幾個content_text即可嘗試像這樣

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(); 
      List<object> ds = new List<object>(); 
      while (dr.Read()) 
      { 
       ds.Add(new { N = dr["Text"] }); 
      } 

      viewdata.DataSource = ds; 
      viewdata.DataBind(); 
     } 
    } 
} 
2

試試這個。

if (dr.Read()) 
{ 
    Label lblLeft = (Label)viewData.FindControl("lblLeft") 
    lblLeft.text = dr["Text"].ToString(); 
} 
+0

它發現標籤,所以謝謝,但我仍然遇到錯誤,當我運行該網站。 未將對象引用設置爲對象的實例。 描述:執行當前Web請求期間發生未處理的異常。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及源代碼的位置。 異常詳細信息:System.NullReferenceException:未將對象引用設置爲對象的實例。 –

0

你需要找到的標籤,它的NamingContainer這是FormViewItemTemplate

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (viewdata.CurrentMode == FormViewMode.ReadOnly) 
    { 
     LoginView lv = (LoginView)viewdata.FindControl("LoginView1"); 
     Label lblLeft = (Label)lv.FindControl("lblLeft"); 
    } 
} 

順便說一句,你應該進行數據綁定的標籤只有在它不是一個回傳:

if(!IsPostBack && viewdata.CurrentMode == FormViewMode.ReadOnly) 
{ 
    LoginView lv = (LoginView)viewdata.FindControl("LoginView1"); 
    Label lblLeft = (Label)lv.FindControl("lblLeft"); 
    using (SqlConnection connection = new SqlConnection("Data Source=10.138.22.47;Initial Catalog=Student10157;User ID=Studentxxxxx;Password=xxxxxxxxxxxxxxxxx")) 
    { 
     connection.Open(); 
     using(var cm = new SqlCommand("Select TOP 1 Text from Content_Text", connection)) 
     using(SqlDataReader dr = cm.ExecuteReader()) 
     { 
      if(dr.Read()) 
      { 
       lblLeft.Text = dr.GetString(dr.GetOrdinal("Text")); 
      } 
     }  
    } 
} 
0

您可以在LoginView控件中找到如下標籤:

LoginView logView = (LoginView)viewdata.FindControl("LoginView1"); 
Label lblLeft = (Label)logView.FindControl("lblLeft"); 
lblLeft.Text = "Your text goes here";