Using FindControl on editable datagrid

Discussion in 'ASP .Net' started by =?Utf-8?B?emFtYml6emk=?=, Jun 3, 2005.

  1. ....I can't seem to get my hands on a control I'm loading in an editable
    datagrid.

    Here's my datagrid control:

    <asp:datagrid
    id="GLRulesGrid"
    runat="server"
    autogeneratecolumns="False"
    oneditcommand="GLRulesGrid_Edit"
    ondeletecommand="GLRulesGrid_Delete"
    onupdatecommand="GLRulesGrid_Update"
    oncancelcommand="GLRulesGrid_Cancel"
    cssclass="embedded">
    <headerstyle cssclass="large-title-red"></headerstyle>
    <columns>
    <asp:editcommandcolumn buttontype="LinkButton" edittext="Edit"
    updatetext="Save" canceltext="Cancel" />
    <asp:templatecolumn headertext="Transaction Type">
    <itemtemplate>
    <%# DataBinder.Eval(Container.DataItem, "TransactionType") %>
    </itemtemplate>
    <edititemtemplate>
    <asp:dropdownlist
    id="TransTypeList"
    onload="TransTypeList_Load"
    datasource="<%# TransTypes %>"
    runat="server" />
    </edititemtemplate>
    </asp:templatecolumn>
    <asp:templatecolumn headertext="Description">
    <itemtemplate>
    <%# DataBinder.Eval(Container.DataItem, "Description") %>
    </itemtemplate>
    <edititemtemplate>
    <asp:textbox
    id="DescBox"
    runat="server"
    text='<%# DataBinder.Eval(Container.DataItem, "Description") %>' />
    </edititemtemplate>
    </asp:templatecolumn>
    </columns>
    </asp:datagrid>

    In the dropdown list in the first column...I'm simply loading this
    collection in the code behind class:

    protected IList TransTypes
    {
    get
    {
    IList tTypes = new ArrayList();
    tTypes.Add("RECV");
    tTypes.Add("ADJUST");
    tTypes.Add("JENSEN");
    tTypes.Add("VAP");
    tTypes.Add("SAMPLE");
    tTypes.Add("SENT");

    return tTypes;
    }
    }

    ....which works just fine...but I want to be able to select the current value
    in the datagrid cell in the DropDownList control so the user doesn't
    accidentally update the record w/ the wrong value, which would be the default
    selected item in the DDL.

    You'll see that I tried to use the Load event for the DDL but I'm still
    unable to use the FindControl method in the code behind to get ahold of the
    dropdownlist.

    How can I achieve this?

    Thanks!
     
    =?Utf-8?B?emFtYml6emk=?=, Jun 3, 2005
    #1
    1. Advertising

  2. zambizzi,

    You to find the Dropdown control in Itemdatabound event of the grid and
    set the selected value of the dropdrop from the one found in your dataitem

    Example in your Itemboundevent add this condition

    if (itemType == ListItemType.EditItem)
    {
    DataRowView objDataRowView = (DataRowView) e.Item.DataItem;

    if (objDataRowView["TransactionType"] != null && objDataRowView
    ["TransactionType"] !=System.DBNull.Value)
    {
    DropDownList cboTransType =
    (DropDownList)e.Item.Cells[2].FindControl
    ("TransTypeList");
    cboTransType.Items.FindByText((string)objDataRowView
    ["TransactionType"]).Selected = true;
    cboTransType.Items.FindByText(objDataRowView[").Selected=true;;
    }

    }

    In this way the dropdownlist will show the values selected which was there
    in the itemtemplate.

    Note :No need for onload="TransTypeList_Load"

    Hope this thing works for you

    Regards

    IntelYogi


    "zambizzi" wrote:

    > ...I can't seem to get my hands on a control I'm loading in an editable
    > datagrid.
    >
    > Here's my datagrid control:
    >
    > <asp:datagrid
    > id="GLRulesGrid"
    > runat="server"
    > autogeneratecolumns="False"
    > oneditcommand="GLRulesGrid_Edit"
    > ondeletecommand="GLRulesGrid_Delete"
    > onupdatecommand="GLRulesGrid_Update"
    > oncancelcommand="GLRulesGrid_Cancel"
    > cssclass="embedded">
    > <headerstyle cssclass="large-title-red"></headerstyle>
    > <columns>
    > <asp:editcommandcolumn buttontype="LinkButton" edittext="Edit"
    > updatetext="Save" canceltext="Cancel" />
    > <asp:templatecolumn headertext="Transaction Type">
    > <itemtemplate>
    > <%# DataBinder.Eval(Container.DataItem, "TransactionType") %>
    > </itemtemplate>
    > <edititemtemplate>
    > <asp:dropdownlist
    > id="TransTypeList"
    > onload="TransTypeList_Load"
    > datasource="<%# TransTypes %>"
    > runat="server" />
    > </edititemtemplate>
    > </asp:templatecolumn>
    > <asp:templatecolumn headertext="Description">
    > <itemtemplate>
    > <%# DataBinder.Eval(Container.DataItem, "Description") %>
    > </itemtemplate>
    > <edititemtemplate>
    > <asp:textbox
    > id="DescBox"
    > runat="server"
    > text='<%# DataBinder.Eval(Container.DataItem, "Description") %>' />
    > </edititemtemplate>
    > </asp:templatecolumn>
    > </columns>
    > </asp:datagrid>
    >
    > In the dropdown list in the first column...I'm simply loading this
    > collection in the code behind class:
    >
    > protected IList TransTypes
    > {
    > get
    > {
    > IList tTypes = new ArrayList();
    > tTypes.Add("RECV");
    > tTypes.Add("ADJUST");
    > tTypes.Add("JENSEN");
    > tTypes.Add("VAP");
    > tTypes.Add("SAMPLE");
    > tTypes.Add("SENT");
    >
    > return tTypes;
    > }
    > }
    >
    > ...which works just fine...but I want to be able to select the current value
    > in the datagrid cell in the DropDownList control so the user doesn't
    > accidentally update the record w/ the wrong value, which would be the default
    > selected item in the DDL.
    >
    > You'll see that I tried to use the Load event for the DDL but I'm still
    > unable to use the FindControl method in the code behind to get ahold of the
    > dropdownlist.
    >
    > How can I achieve this?
    >
    > Thanks!
     
    =?Utf-8?B?SW50ZWxZb2dp?=, Jun 3, 2005
    #2
    1. Advertising

  3. OK, with a little poking and prodding of your example I got it to *almost*
    work. I can now get a grip on the drop down list but am unable to select the
    item w/ the current dataitem value...here's my event handler for the datagrid
    OnItemDataBound event.

    protected void GLRulesGrid_ItemDataBound(object sender,
    DataGridItemEventArgs e)
    {
    ListItemType itemType = e.Item.ItemType;

    if (itemType == ListItemType.EditItem)
    {
    //set row editable
    if (e.Item.DataItem != null)
    {
    IGLRule rowRule = (IGLRule)e.Item.DataItem;

    DropDownList transTypes =
    (DropDownList)e.Item.Cells[1].FindControl("TransTypeList");
    transTypes.Items.FindByValue(rowRule.TransactionType).Selected = true;
    }
    }
    }

    ....I'm using an object model to represent data....that's what the type
    "IGLRule" is...FYI. I walked through it in the debugger and the data is
    working fine...I can read all of my values....including the "TransactionType"
    property of the rowRule variable (which is a business object with a type of
    IGLRule.) it fails on the line where I try to the ddl Selected to
    true....the last line.

    I get this error:

    "Object reference not set to an instance of an object."

    ....which doens't make sense....I can read from my business object's
    properties AND from the dropdownlist declared one line before the one that
    fails....why can't I select it?

    Thanks!

    "IntelYogi" wrote:

    > zambizzi,
    >
    > You to find the Dropdown control in Itemdatabound event of the grid and
    > set the selected value of the dropdrop from the one found in your dataitem
    >
    > Example in your Itemboundevent add this condition
    >
    > if (itemType == ListItemType.EditItem)
    > {
    > DataRowView objDataRowView = (DataRowView) e.Item.DataItem;
    >
    > if (objDataRowView["TransactionType"] != null && objDataRowView
    > ["TransactionType"] !=System.DBNull.Value)
    > {
    > DropDownList cboTransType =
    > (DropDownList)e.Item.Cells[2].FindControl
    > ("TransTypeList");
    > cboTransType.Items.FindByText((string)objDataRowView
    > ["TransactionType"]).Selected = true;
    > cboTransType.Items.FindByText(objDataRowView[").Selected=true;;
    > }
    >
    > }
    >
    > In this way the dropdownlist will show the values selected which was there
    > in the itemtemplate.
    >
    > Note :No need for onload="TransTypeList_Load"
    >
    > Hope this thing works for you
    >
    > Regards
    >
    > IntelYogi
    >
    >
    > "zambizzi" wrote:
    >
    > > ...I can't seem to get my hands on a control I'm loading in an editable
    > > datagrid.
    > >
    > > Here's my datagrid control:
    > >
    > > <asp:datagrid
    > > id="GLRulesGrid"
    > > runat="server"
    > > autogeneratecolumns="False"
    > > oneditcommand="GLRulesGrid_Edit"
    > > ondeletecommand="GLRulesGrid_Delete"
    > > onupdatecommand="GLRulesGrid_Update"
    > > oncancelcommand="GLRulesGrid_Cancel"
    > > cssclass="embedded">
    > > <headerstyle cssclass="large-title-red"></headerstyle>
    > > <columns>
    > > <asp:editcommandcolumn buttontype="LinkButton" edittext="Edit"
    > > updatetext="Save" canceltext="Cancel" />
    > > <asp:templatecolumn headertext="Transaction Type">
    > > <itemtemplate>
    > > <%# DataBinder.Eval(Container.DataItem, "TransactionType") %>
    > > </itemtemplate>
    > > <edititemtemplate>
    > > <asp:dropdownlist
    > > id="TransTypeList"
    > > onload="TransTypeList_Load"
    > > datasource="<%# TransTypes %>"
    > > runat="server" />
    > > </edititemtemplate>
    > > </asp:templatecolumn>
    > > <asp:templatecolumn headertext="Description">
    > > <itemtemplate>
    > > <%# DataBinder.Eval(Container.DataItem, "Description") %>
    > > </itemtemplate>
    > > <edititemtemplate>
    > > <asp:textbox
    > > id="DescBox"
    > > runat="server"
    > > text='<%# DataBinder.Eval(Container.DataItem, "Description") %>' />
    > > </edititemtemplate>
    > > </asp:templatecolumn>
    > > </columns>
    > > </asp:datagrid>
    > >
    > > In the dropdown list in the first column...I'm simply loading this
    > > collection in the code behind class:
    > >
    > > protected IList TransTypes
    > > {
    > > get
    > > {
    > > IList tTypes = new ArrayList();
    > > tTypes.Add("RECV");
    > > tTypes.Add("ADJUST");
    > > tTypes.Add("JENSEN");
    > > tTypes.Add("VAP");
    > > tTypes.Add("SAMPLE");
    > > tTypes.Add("SENT");
    > >
    > > return tTypes;
    > > }
    > > }
    > >
    > > ...which works just fine...but I want to be able to select the current value
    > > in the datagrid cell in the DropDownList control so the user doesn't
    > > accidentally update the record w/ the wrong value, which would be the default
    > > selected item in the DDL.
    > >
    > > You'll see that I tried to use the Load event for the DDL but I'm still
    > > unable to use the FindControl method in the code behind to get ahold of the
    > > dropdownlist.
    > >
    > > How can I achieve this?
    > >
    > > Thanks!
     
    =?Utf-8?B?emFtYml6emk=?=, Jun 3, 2005
    #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. Sid
    Replies:
    4
    Views:
    587
  2. Dave

    Datagrid FindControl in Footer

    Dave, Nov 14, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    3,063
  3. Mike Speak

    DataGrid FindControl HELP...

    Mike Speak, Nov 19, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    648
    Mike Speak
    Nov 19, 2003
  4. Ruby

    findcontrol in datagrid footer

    Ruby, Jan 28, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    1,815
  5. Diane
    Replies:
    5
    Views:
    272
    alvinz_c
    Nov 9, 2005
Loading...

Share This Page