ASP.NET内联";。FindControl();在.aspx列表视图中?

ASP.NET内联";。FindControl();在.aspx列表视图中?,asp.net,listview,gridview,findcontrol,Asp.net,Listview,Gridview,Findcontrol,在ASP.NET中,我们喜欢在绑定的网格服务器控件(Girdview或ListView)中使用“子”SqlDataSource 多年来,我一直在使用这种FindControl()方法: C#代码隐藏: protected void gridviewItems_RowDataBound(object sender, GridViewRowEventArgs e) { Label labelItemId = (Label)e.Row.FindControl("labelIt

在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");
    }
<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>
没什么问题,但我已经厌倦了,也许有一种方法可以在.aspx标记中实现这一点

比如:

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


这可能吗?

我不知道你的情况如何。有这样的吗

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>
命名空间WebApplication1
{
公共阶层人士
{
私有字符串名称;
公共字符串名
{
设置
{
名称=值;
}
得到
{
返回名称;
}
}
公共列表GetAll()
{
列表人员=新列表();
人员p1=新人员();
p1.Name=“John”;
新增(p1);
返回人员;
}
}
}
这是一个带有gridview的web表单,其中有一个指向类Person及其方法GetAll的“子”数据源。根本没有问题。请张贴一些完整的标记与您的情况

编辑

如果你有这样的东西:

  protected void buttonSend_Click(object sender, EventArgs e)
    {
        Label labelItemId = (Label)((Control)sender).NamingContainer.FindControl("labelItemId");
    }
<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”的表中绘制数据。请显示您的特殊情况,因为这当然是一个简单的设置。

我有类似的设置,但我使用的不是ObjectDataSource,而是SqlDataSource控件。我想做的是在绑定的ListView行的EditItemTemplate中有一个dropdownlist,它有自己的SqlDataSource。实际上,我可以让它工作,但我必须在代码后面写一些行。我想知道是否可以将其写入.aspx中的FindControl