Dynamically bind dropdown list etc in template column

Discussion in 'ASP .Net' started by Dmitry Korolyov [MVP], Oct 16, 2003.

  1. Is that possible? In other words, I want a dropdown list (and other list-type controls) which appears in edit more of a templated column to be populated with data at the run time. An attempt to do so results in non-existing object error. (Object reference not set to an instance of an object).

    --
    Dmitry Korolyov []
    MVP: Windows Server - Active Directory
     
    Dmitry Korolyov [MVP], Oct 16, 2003
    #1
    1. Advertising

  2. Dmitry Korolyov [MVP] wrote:
    > Is that possible? In other words, I want a dropdown list (and other
    > list-type controls) which appears in edit more of a templated column to
    > be populated with data at the run time. An attempt to do so results in
    > non-existing object error. (/Object reference not set to an instance of
    > an object)./
    >
    > --
    > Dmitry Korolyov []
    > MVP: Windows Server - Active Directory
    >
    >


    Check out the ItemDataBound event. This runs once for each item in the
    grid (or datalist) (in other words each row). e.Item.DataItem will give
    you the corresponding data item the current row was bound to, and you
    can populate your dropdown as needed there.

    Now to get reference to a control, as an example say we're trying to
    find a label in the first column (index 0), you would do the following
    in ItemDataBound

    protected void myDataGrid_ItemDataBound(Object sender,
    DataGridItemEventArgs e)
    {
    if ((e.Item.ItemType == ListItemType.AlternatingItem) ||
    (e.Item.ItemType == ListItemType.Item))
    {
    //bind/populate the label
    Label labelField = (Label)e.Item.Cells[0].FindControl("lMyLabel");

    //note i'm assuming i bound the grid to a dataview
    labelField.Text =
    ((DataRowView)e.Item.DataItem)["col_text"].ToString();
    }
    }

    Hopefully I didn't put any syntax errors in there as I wrote this
    without my IDE, but there's also various examples on sites such as
    4guysfromrolla.com and others.

    --
    Craig Deelsnyder
    Microsoft MVP - ASP/ASP.NET
     
    Craig Deelsnyder, Oct 16, 2003
    #2
    1. Advertising

  3. Thanks, but the example is populating the control from the datasource bound to the datagrid in general, while I need to populate it from some "abstract" data source - created at runtime or created at application level, something like this.

    --
    Dmitry Korolyov []
    MVP: Windows Server - Active Directory


    "Craig Deelsnyder" <> wrote in message news:e8I#...
    Dmitry Korolyov [MVP] wrote:
    > Is that possible? In other words, I want a dropdown list (and other
    > list-type controls) which appears in edit more of a templated column to
    > be populated with data at the run time. An attempt to do so results in
    > non-existing object error. (/Object reference not set to an instance of
    > an object)./
    >
    > --
    > Dmitry Korolyov []
    > MVP: Windows Server - Active Directory
    >
    >


    Check out the ItemDataBound event. This runs once for each item in the
    grid (or datalist) (in other words each row). e.Item.DataItem will give
    you the corresponding data item the current row was bound to, and you
    can populate your dropdown as needed there.

    Now to get reference to a control, as an example say we're trying to
    find a label in the first column (index 0), you would do the following
    in ItemDataBound

    protected void myDataGrid_ItemDataBound(Object sender,
    DataGridItemEventArgs e)
    {
    if ((e.Item.ItemType == ListItemType.AlternatingItem) ||
    (e.Item.ItemType == ListItemType.Item))
    {
    //bind/populate the label
    Label labelField = (Label)e.Item.Cells[0].FindControl("lMyLabel");

    //note i'm assuming i bound the grid to a dataview
    labelField.Text =
    ((DataRowView)e.Item.DataItem)["col_text"].ToString();
    }
    }

    Hopefully I didn't put any syntax errors in there as I wrote this
    without my IDE, but there's also various examples on sites such as
    4guysfromrolla.com and others.

    --
    Craig Deelsnyder
    Microsoft MVP - ASP/ASP.NET
     
    Dmitry Korolyov [MVP], Oct 16, 2003
    #3
  4. Dmitry Korolyov [MVP] wrote:
    > Thanks, but the example is populating the control from the datasource
    > bound to the datagrid in general, while I need to populate it from some
    > "abstract" data source - created at runtime or created at application
    > level, something like this.
    >
    > --
    > Dmitry Korolyov []
    > MVP: Windows Server - Active Directory
    >
    >
    >
    > "Craig Deelsnyder" <
    > <mailto:>> wrote in message
    > news:e8I#...
    > Dmitry Korolyov [MVP] wrote:
    > > Is that possible? In other words, I want a dropdown list (and other
    > > list-type controls) which appears in edit more of a templated

    > column to
    > > be populated with data at the run time. An attempt to do so

    > results in
    > > non-existing object error. (/Object reference not set to an

    > instance of
    > > an object)./
    > >
    > > --
    > > Dmitry Korolyov []
    > > MVP: Windows Server - Active Directory
    > >
    > >

    >
    > Check out the ItemDataBound event. This runs once for each item in the
    > grid (or datalist) (in other words each row). e.Item.DataItem will
    > give
    > you the corresponding data item the current row was bound to, and you
    > can populate your dropdown as needed there.
    >
    > Now to get reference to a control, as an example say we're trying to
    > find a label in the first column (index 0), you would do the following
    > in ItemDataBound
    >
    > protected void myDataGrid_ItemDataBound(Object sender,
    > DataGridItemEventArgs e)
    > {
    > if ((e.Item.ItemType == ListItemType.AlternatingItem) ||
    > (e.Item.ItemType == ListItemType.Item))
    > {
    > //bind/populate the label
    > Label labelField = (Label)e.Item.Cells[0].FindControl("lMyLabel");
    >
    > //note i'm assuming i bound the grid to a dataview
    > labelField.Text =
    > ((DataRowView)e.Item.DataItem)["col_text"].ToString();
    > }
    > }
    >
    > Hopefully I didn't put any syntax errors in there as I wrote this
    > without my IDE, but there's also various examples on sites such as
    > 4guysfromrolla.com and others.
    >
    > --
    > Craig Deelsnyder
    > Microsoft MVP - ASP/ASP.NET


    You can bind it to whatever you want, I just showed that as an example,
    since I assume you might need some value in the current row to use to
    get the data to bind the ddl with...if not, feel free to bind it as you
    normally would....

    --
    Craig Deelsnyder
    Microsoft MVP - ASP/ASP.NET
     
    Craig Deelsnyder, Oct 17, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vishnu
    Replies:
    1
    Views:
    1,241
    Patrick.O.Ige
    Mar 25, 2006
  2. Replies:
    2
    Views:
    458
  3. Paul
    Replies:
    3
    Views:
    2,910
  4. Kevin Walzer

    Re: PIL (etc etc etc) on OS X

    Kevin Walzer, Aug 1, 2008, in forum: Python
    Replies:
    4
    Views:
    438
    Fredrik Lundh
    Aug 13, 2008
  5. janaki
    Replies:
    0
    Views:
    856
    janaki
    Mar 15, 2011
Loading...

Share This Page