[英]How to find a control in ASP.NET LoginView?
我无法在LoginView控件中找到我的标签ID,只是为了解释我要构建的内容。 如果您尚未登录,则只能看到数据库中的内容,但是如果您已登录,则可以对其进行编辑。 但是现在我只需要帮助,就可以从数据库中读取数据
这是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");
工作var lblLeft = (Label)viewData.FindControl("lblLeft");
尝试这个。
if (dr.Read())
{
Label lblLeft = (Label)viewData.FindControl("lblLeft")
lblLeft.text = dr["Text"].ToString();
}
数据源中需要FormView
,所以我认为您的代码中需要这样的想法
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>
更新
如果您有一些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();
}
}
}
您需要在NamingContainer
找到标签,该标签是FormView
的ItemTemplate
:
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"));
}
}
}
}
您可以在LoginView控件中找到标签,如下所示:
LoginView logView = (LoginView)viewdata.FindControl("LoginView1");
Label lblLeft = (Label)logView.FindControl("lblLeft");
lblLeft.Text = "Your text goes here";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.