2014-01-09 57 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";