How to initialize dropdownlist value in a gridview column edit template

Discussion in 'ASP .Net' started by keithb, Jun 1, 2006.

  1. keithb

    keithb Guest

    I have dropdown list in a gridview column edit template. It works OK, except
    when I edit a row, the dropdown list does not initialize to the existing
    value that shows in the textbox that displays in the item template. I tried
    to initialize the selected value of the dropdown list with some lines of
    code in the grid_RowEditing event but I get an error that I do not
    understand. The error occurs at this line of code:

    String DelID =
    ((DropDownList)grid.Rows[e.NewEditIndex].FindControl("DelDropDownList")).Text;

    the error is: "Object reference not set to an instance of an object."

    I do not understand this, because code from the grid_RowUpdating event that
    runs error-free is the same except that it uses e.RowIndex instead of
    e.NewEditIndex and both indexes have the same value. Can someone shed some
    light on this for me?

    Thanks,

    Keith
     
    keithb, Jun 1, 2006
    #1
    1. Advertising

  2. keithb

    Guest

    I think (and someone please correct me if I'm wrong) but all of the
    controls inside the edit item template are not created until the data
    grid is bound. Also the OnRowEditing event fires before the
    OnRowDataBound event. Therefore during the on row editing event the
    drop down list (and anything else inside the edit item template)
    doesn't exist yet.

    The way I got it to work for me when I ran into this problem is to
    initialize the selected value of the drop down list during the
    OnRowDataBound event- not the OnRowEditing event.
     
    , Jun 1, 2006
    #2
    1. Advertising

  3. keithb

    keithb Guest

    The solution turned was simple: I just needed to bind the SelectedValue
    property of the DropDownList to the related field in the GridView edit
    template. Now that I have done that, everything works.

    Thanks,

    Keith

    <> wrote in message
    news:...
    >I think (and someone please correct me if I'm wrong) but all of the
    > controls inside the edit item template are not created until the data
    > grid is bound. Also the OnRowEditing event fires before the
    > OnRowDataBound event. Therefore during the on row editing event the
    > drop down list (and anything else inside the edit item template)
    > doesn't exist yet.
    >
    > The way I got it to work for me when I ran into this problem is to
    > initialize the selected value of the drop down list during the
    > OnRowDataBound event- not the OnRowEditing event.
    >
     
    keithb, Jun 2, 2006
    #3
    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. San Diego Guy
    Replies:
    0
    Views:
    551
    San Diego Guy
    Aug 7, 2003
  2. =?Utf-8?B?U3Vieml6bw==?=
    Replies:
    0
    Views:
    569
    =?Utf-8?B?U3Vieml6bw==?=
    Nov 28, 2006
  3. Keith G Hicks
    Replies:
    3
    Views:
    770
    Keith G Hicks
    Feb 18, 2008
  4. Esperanza

    Template column base on another column value

    Esperanza, Feb 18, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    5
    Views:
    176
    Elton Wang
    Feb 21, 2005
  5. Replies:
    2
    Views:
    246
    jyoti
    Aug 19, 2006
Loading...

Share This Page