【问题标题】:ASP.NET inline ".FindControl()" in .aspx ListView?.aspx ListView 中的 ASP.NET 内联“.FindControl()”?
【发布时间】:2012-02-01 10:01:53
【问题描述】:

在 ASP.NET 中,我们喜欢在绑定的网格服务器控件(Girdview 或 ListView)中使用“子”SqlDataSource。

多年来我一直在使用这种FindControl() 方法:

C# 代码隐藏:

protected void gridviewItems_RowDataBound(object sender, GridViewRowEventArgs e)

    {
        Label labelItemId = (Label)e.Row.FindControl("labelItemId");
    }

或者像这样:

  protected void buttonSend_Click(object sender, EventArgs e)
    {
        Label labelItemId = (Label)((Control)sender).NamingContainer.FindControl("labelItemId");
    }

没有错,但我已经厌倦了,也许有办法在 .aspx 标记中做到这一点?

类似:

<asp:DropDownList 
     ID="dropdownItems" 
     runat="server" 
     DataSource=<% this.NamingContainer.FindControl("slqdatasourceItems") %> 
     DataTextField="ItemName" 
     DataValueField="ItemId" />

这可能吗?

【问题讨论】:

    标签: asp.net listview gridview findcontrol


    【解决方案1】:

    我不确定你的情况。你有这样的东西吗?

    namespace WebApplication1
    {
    
    public class Person
    {
        private string name;
    
        public string Name
        {
            set
            {
                name = value;
            }
    
            get
            {
                return name;
            }
        }
    
        public List<Person> GetAll()
        {
            List<Person> persons = new List<Person>();
    
            Person p1 = new Person();
            p1.Name = "John";
    
            persons.Add(p1);
    
            return persons;
        }
    }
    }
    
    <form id="form1" runat="server">
    <div>    
    
        <asp:GridView runat="server" ID="dataGrid" AutoGenerateColumns="false" DataSourceID="persons">
            <Columns>
                <asp:BoundField HeaderText="Person Name" DataField="Name" />
    
                <asp:TemplateField>
                <ItemTemplate>
                    <asp:DropDownList runat="server" DataTextField="Name" DataSourceID="persons"></asp:DropDownList>
                    <asp:ObjectDataSource runat="server" ID="persons" TypeName="WebApplication1.Person" SelectMethod="GetAll"></asp:ObjectDataSource>
                </ItemTemplate>
    
                </asp:TemplateField>
    
            </Columns>
    
        </asp:GridView>
    
        <asp:ObjectDataSource runat="server" ID="persons" TypeName="WebApplication1.Person" SelectMethod="GetAll"></asp:ObjectDataSource>
    </div>
    </form>
    

    这是一个带有 gridview 的 web 表单,它有一个指向 Person 类的“子”数据源和它的 GetAll 方法。根本不会出现任何问题。请根据您的情况发布一些完整的标记。

    编辑

    如果你有这样的事情:

    <asp:ListView runat="server" ID="lv" DataSourceID="SqlDataSource1">
        <LayoutTemplate><div runat="server" id="itemPlaceholder"></div></LayoutTemplate>
        <ItemTemplate>
            <asp:Label runat="server" Text='<%# Eval("Name") %>'></asp:Label>
            <asp:Button runat="server" CommandName="Edit" Text="Edit" />           
        </ItemTemplate>
        <EditItemTemplate>   
            <asp:Label runat="server" Text='<%# Eval("Name") %>'></asp:Label>        
            <asp:DropDownList ID="DropDownList1" runat="server" DataTextField="Name" DataSourceID="SqlDataSource1"></asp:DropDownList>
            <asp:SqlDataSource id="SqlDataSource1" runat="server" DataSourceMode="DataReader"
              ConnectionString="<%$ ConnectionStrings:SqlConnectionString%>" SelectCommand="SELECT Name FROM Person">
          </asp:SqlDataSource>
        </EditItemTemplate>
        </asp:ListView>
    
        <asp:SqlDataSource id="SqlDataSource1" runat="server" DataSourceMode="DataReader"
          ConnectionString="<%$ ConnectionStrings:SqlConnectionString%>" SelectCommand="SELECT Name FROM Person">
      </asp:SqlDataSource>
    

    你也不应该有任何问题。我在这里假设您正在从名为“Person”的表中提取数据。请展示您的特殊情况,因为这当然是一个简单的设置。

    【讨论】:

    • 我确实有类似的东西,但我使用的是 SqlDataSource 控件而不是 ObjectDataSource。我想要做的是在绑定的 ListView 行的 EditItemTemplate 中有一个带有自己的 SqlDataSource 的下拉列表
    • 我实际上可以让它工作,但我必须在代码隐藏中写一些行。我想知道是否可以在 .aspx 中编写 FindControl
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    • 2011-07-13
    • 2010-09-06
    • 2012-05-04
    相关资源
    最近更新 更多