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



在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" />

这可能吗?

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

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"的表中绘制数据。请出示您的特殊情况,因为这当然是一个简单的设置。

最新更新