簡體   English   中英

如何在ASP.NET LoginView中找到控件?

[英]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找到標簽,該標簽是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"));
            }   
        }    
    }
}

您可以在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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM