简体   繁体   中英

How to find a control in ASP.NET LoginView?

I can't find my label ID in LoginView Control, Just to explain what i'm trying to build. If you are NOT logged in you can only see the content from the database, but if your ARE logged in you can edit it. But right now i just need help to make it read the data from the database

Here is the ASP.NET Code-Behind:

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();
            } 
        }
    }
}
}

Here is my ASP.NET code:

<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>

I have tried as you can see to use a formview with the following C# code but that dont work either var lblLeft = (Label)viewData.FindControl("lblLeft");

Try This.

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

FormView need in datasource, so i think you need somethink like this in your code

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();

            } 
        }
    }
}

and markup

<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
if you have a few content_text you can try something like this

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();
        }
    }
}

You need to find the label in it's NamingContainer which is the ItemTemplate of the FormView :

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");
    }
}

By the way, you should databind the label only if its not a postback:

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"));
            }   
        }    
    }
}

You can find label in LoginView control as following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM