0

In ASP.NET, we like using "child" SqlDataSource inside a bound grid server control (Girdview or ListView).

I've been using this FindControl() approach for years:

C# Codebehind:

protected void gridviewItems_RowDataBound(object sender, GridViewRowEventArgs e)

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

or like this:

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

Nothing wrong with it, but I'm quite tired of it and maybe there's a way to do this in .aspx markup?

Something like:

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

Is this possible?

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
A.B. User
  • 443
  • 1
  • 8
  • 20

1 Answers1

0

I am not sure what your situation is. Do you have something like this?

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>

This is a web form with a gridview which has a "child" datasource pointing to the class Person and it´s method GetAll. There arise no problems at all. Please post some complete markup with your situation.

Edit

If you have something like this:

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

you shouldn´t have any problems either. I am here a assuming that you are drawing data from a table named "Person". Please show your special case as this is of course a simple setup.

AGuyCalledGerald
  • 7,882
  • 17
  • 73
  • 120
  • I do have something similar, but instead of ObjectDataSource, I'm using SqlDataSource control. What I'm trying to do is have a dropdownlist with its own SqlDataSource inside a bound ListView row's EditItemTemplate – A.B. User Feb 01 '12 at 14:22
  • I can actually make it work, but I have to write some lines in code-behind. I'm wondering if it's possible to write it the FindControl in the .aspx – A.B. User Feb 01 '12 at 14:25