GridView.EditItemTemplate with ListBox embedded

Last post 08-17-2007 9:06 AM by dcolemanturner. 8 replies.

Sort Posts:

  • GridView.EditItemTemplate with ListBox embedded

    03-20-2007, 8:52 AM

    Hi all,

     I am trying to code a gridview update section to include a listbox that will display all customers for a user.  I have created a ObjectDataSource that fills the listbox perfectly. 

    The main problem is that I need to highlight the rows that the user currently has stored in the DB.  I understand that the ListBox will only be rendered after certain events but I can't determine which event will allow me to..

    ..find the ListBox after it has been databound so I can make my changes to the data

    Here is my code pasted below...

     <!--HTML File-->

    <asp:GridView runat="server" ID="grdInfoLib" Width="100%" AutoGenerateColumns="False"
    DataSourceID="objDS" DataKeyNames="InfoLibID" AllowPaging="True" AllowSorting="True" OnRowDataBound
    ="grdInfoLib_OnRowDataBound"
    OnRowCommand="grdInfoLib_OnRowCommand" EmptyDataText
    ="No Records Returned">
    <HeaderStyle CssClass="gridhead1"
    />
    <RowStyle CssClass="gridbody1"
    />
    <AlternatingRowStyle CssClass="gridbody2"
    />
    <Columns
    >
    <asp:CommandField ShowEditButton="True" CausesValidation="false"
    />
    <asp:CommandField ShowDeleteButton="True" CausesValidation="false"
    />
    <asp:CommandField ShowInsertButton="True" CausesValidation="false"
    />
    <asp:TemplateField SortExpression="CustomerName" HeaderText
    ="Customer">
    <ItemTemplate
    >
    <asp:Label runat="server" ID="lblCustomerName"><%# DataBinder.Eval(Container.DataItem, "CustomerName") %></asp:Label
    >
    </ItemTemplate
    >
    <EditItemTemplate
    >
    <asp:ListBox runat="server" SelectionMode="Multiple" ID="lbCustomerName" DataSourceID
    ="objDSConfigs"
    DataTextField="CustomerCode" DataValueField="ConfigurationID" OnDataBound
    ="lbCustomerName_OnDataBound">
    </asp:ListBox
    >
    </EditItemTemplate
    >
    </asp:TemplateField
    >
    <asp:TemplateField SortExpression="InfoLibCategoryID" HeaderText
    ="Library Category"
    Visible
    ="False">
    <EditItemTemplate
    >
    <asp:DropDownList runat="server" ID="ddlCategoryIDEdit" DataSourceID
    ="objDSCategories"
    DataTextField="InfoLibCategory" DataValueField="InfoCategoryID" SelectedValue='<%# Bind("InfoCategoryID") %>
    '>
    </asp:DropDownList
    >
    </EditItemTemplate
    >
    </asp:TemplateField
    >
    <asp:BoundField DataField="Subject" HeaderText="Subject" SortExpression="Subject"
    />
    <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description"
    />
    <asp:TemplateField HeaderText="Is Visible" SortExpression="Visible" Visible
    ="False">
    <EditItemTemplate
    >
    <asp:CheckBox runat="server" ID="chkVisibleEdit"
    />
    </EditItemTemplate
    >
    </asp:TemplateField
    >
    <asp:TemplateField HeaderText="All Users" SortExpression="AllUsers" Visible
    ="False">
    <EditItemTemplate
    >
    <asp:CheckBox runat="server" ID="chkAllUsersEdit"
    />
    </EditItemTemplate
    >
    </asp:TemplateField
    >
    </Columns
    >
    </asp:GridView
    >

    <asp:ObjectDataSource runat="server" ID="objDS" TypeName="Pages" SelectMethod="GetInfoLibItemsInDB">
    <SelectParameters
    >
    <asp:ControlParameter ControlID="hfInfoLibTypeID" Name="InfoLibTypeID" Type="int32"
    />
    </SelectParameters
    >
    <InsertParameters
    >
    <asp:Parameter Name="DBName" Type="string"
    />
    <asp:Parameter Name="InfoCategoryID" Type="int32"
    />
    <asp:Parameter Name="InfoLibTypeID" Type="int32"
    />
    <asp:Parameter Name="Subject" Type="String"
    />
    <asp:Parameter Name="Description" Type="string"
    />
    <asp:Parameter Name="Visible" Type="Boolean"
    />
    <asp:Parameter Name="AllProducts" Type="Boolean"
    />
    <asp:Parameter Name="AllUsers" Type="Boolean"
    />
    </InsertParameters
    >
    <UpdateParameters
    >
    </UpdateParameters
    >
    </asp:ObjectDataSource>

    <asp:ObjectDataSource runat="Server" ID="objDSCategories" TypeName="Sections" SelectMethod="GetAllInfoLibCategories">
    </asp:ObjectDataSource
    >
    <asp:ObjectDataSource runat="server" ID="objDSConfigs" TypeName="Customer" SelectMethod
    ="GetConfigurations">
    <SelectParameters
    >
    <asp:Parameter Name="SPType" DefaultValue="All" Type="string"
    />
    <asp:Parameter Name="DBName" DefaultValue="empty" Type="String"
    />
    </SelectParameters
    >
    </asp:ObjectDataSource>

    <asp:HiddenField runat="server" ID="hfInfoLibTypeID" />

    <!--.CS File-->

    protected void grdInfoLib_OnRowDataBound(Object sender, GridViewRowEventArgs e)
    {
       if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lbl = (Label)e.Row.FindControl("lblCustomerName");
                ListBox lb = (ListBox)e.Row.FindControl("lbCustomerName");
            }
    }


    protected void lbCustomerName_OnDataBound(Object sender, EventArgs e)
    {
    ListBox lb = (ListBox) grdInfoLib.FindControl("lbCustomerName");
    }

    Any light shed on this problem would be a major help and a huge time saver for me.  Thanks and have a 1337-day.

    -Derrick

     

  • Re: GridView.EditItemTemplate with ListBox embedded

    03-20-2007, 9:19 AM

    I forgot to mention that if anyone has a better idea or ways to implement this, please mention it.  I am open to any changes, etc.

    Thanks for your time.

  • Re: GridView.EditItemTemplate with ListBox embedded

    03-20-2007, 12:53 PM
    Answer

    Found the fix.  Used the OnRowCreated event of the GridView.

     

    protected void grdInfoLib_OnRowCreated(Object sender, GridViewRowEventArgs e)
        {
            ListBox lb = new ListBox();

            for (int x = 0; x < e.Row.Cells.Count; x++)
            {
                if (e.Row.Cells[x].FindControl("lbCustomerName") != null)
                {
                    lb = (ListBox)e.Row.Cells[x].FindControl("lbCustomerName");
                }
            }
        }

     

    -Derrick (phew!)

  • Re: GridView.EditItemTemplate with ListBox embedded

    03-20-2007, 1:05 PM

    I believe to get the control you first need an identitier of the row that its contained in.  Therefore, any of the RowEvents for the GridView should be able to correctly identify the row, and from there, the control (if rendered before event fires). 

    If anyone can add some wisdom to this statement, please feel free.

     

    -Derrick

  • Re: GridView.EditItemTemplate with ListBox embedded

    08-17-2007, 6:27 AM

    Derrick,

     

       To be honest, I don't know how your code would work. Because the gridview didn't get databound until rowdatabound and that's after RowCreated Event is finished. You can surely get the control in RowCreated Event, but unless your listbox is databound somewhere else, it will be empty. I think you can get the databound controls in PreRender event but I don't know how you can get the row you are dealing with. Let me know if I am wrong. Cheers.

  • Re: GridView.EditItemTemplate with ListBox embedded

    08-17-2007, 6:30 AM

    Sorry, my bad. Didn't see your got different ODS. Well, what if you have just textbox in edittemplate and want to change the value of that at run time? this textbox retrieves value from the same ODS of its parent gridview?

  • Re: GridView.EditItemTemplate with ListBox embedded

    08-17-2007, 8:46 AM

    I would probably setup the GridView_OnRowCommand Event and check for a e.CommandName == "Edit"

    Inside of this event, I would locate and bind the textbox with it's data.

    protected void grdInfoLib_OnRowCommand(Object sender, GridViewCommandEventArgs e)
    {
    if (e.CommandName == "Edit")
    {
    recordID = Convert.ToInt32(e.CommandArgument);
    //Get Record From DB, locate the textbox inside of the GridView controls, and Populate TextBox
    }
    }
     

    You might be able to get around the extra code by doing something like this...

     <asp:TemplateField HeaderText="Description" SortExpression="Description">
         <EditTemplate>
            <asp:TestBox runat="server" ID="txtDescription" Text="<%# Eval("ColumnName") %>></asp:Label>
        </EditTemplate>
    </asp:TemplateField>

    I haven't tested this out, but figure the Gridview, along with it's bindings to the ODS in the aspx page, might allow this to work as well.

  • Re: GridView.EditItemTemplate with ListBox embedded

    08-17-2007, 8:58 AM

    yeah, I think your method will probably work as well. I simply used OnRowDataBound since I can find controls and the data is already populated as well. Even though I doubt if it's possible to put Eval() as the text value of textbox because my gridview doesn't read from DB directly but computed at run time and then assembled at data layer. So each row of the gridview doesn't represent an entry inside database table. But anyway, RowDatabound is working for me. Cheers.

     

     

     

  • Re: GridView.EditItemTemplate with ListBox embedded

    08-17-2007, 9:06 AM

    Good to hear Kent.  Take care buddy.

    -Derrick 

Page 1 of 1 (9 items)
Microsoft Communities
Page view counter