DropDownList and Sorting using Adapter

Posts   
 
    
TonyLacey
User
Posts: 17
Joined: 11-Mar-2008
# Posted on: 28-Mar-2008 16:20:21   

Hi,

First of all... Using 2.5 Final Demo version (you should be getting money any day for real version... I only have 5 days left on DEMO!!!!)... and I like it alot. Thank you. I came from NetTiers and liked that too but yours works with DB2, which I need to support.

.Net 3.5 and ASP.NET...

My question is... What is the easiest way to sort the items in a dropdownlist. I have it bound to a LLBLGenProDataSource2. What I have is a FormView with a bunch of fields.. 10 being dropdownlists to FK of other EntityCollections. For example, one of the fields is Employee... and I list all available employees... How can I EASILY simple_smile sort the employees by UFullname?

Thanks! Tony

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 28-Mar-2008 16:29:01   

For each dropdownlist set its LLBLGenProDataSource.SorterToUse to the required sortExpression.

TonyLacey
User
Posts: 17
Joined: 11-Mar-2008
# Posted on: 28-Mar-2008 16:50:28   

Thank you Walaa for fast response!

That is what I thought but when I do this I get the following error.

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

Could be I'm just not thinking right. wink Also, are there any plans to add an OrderByClause to DataSource? so I can do...


<llblgenpro:LLBLGenProDataSource2 ID="caretakerDataSource" runat="server" AdapterTypeName="InventoryDAL.DatabaseSpecific.DataAccessAdapter, InventoryDALDBSpecific" DataContainerType="EntityCollection" EntityFactoryTypeName="InventoryDAL.FactoryClasses.UserEntityFactory, InventoryDAL"
OrderByClause="UFullname, UAge">
</llblgenpro:LLBLGenProDataSource2>

without adding anything to code behind? This would be awesome!

Here is problem aspx:

<asp:DropDownList ID="caretakerDropDownList" runat="server" AppendDataBoundItems="true" DataSourceID="caretakerDataSource" DataTextField="UFullname" DataValueField="UId" SelectedValue='<%# Eval("ICaretakerId") %>'>
     <asp:ListItem Value="">Unassigned</asp:ListItem>
</asp:DropDownList>&nbsp;&nbsp;<asp:ImageButton ID="addUserButton1" runat="server" AlternateText="Add New User" ImageUrl="~/Images/new.jpg" OnClick="addUserClick" />
<llblgenpro:LLBLGenProDataSource2 ID="caretakerDataSource" runat="server" AdapterTypeName="InventoryDAL.DatabaseSpecific.DataAccessAdapter, InventoryDALDBSpecific" DataContainerType="EntityCollection" EntityFactoryTypeName="InventoryDAL.FactoryClasses.UserEntityFactory, InventoryDAL">
</llblgenpro:LLBLGenProDataSource2>

and problem Code behind:

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["EditIId"] == null)
                {
                    // Inserting new inventory....
                    FormView1.DefaultMode = FormViewMode.Insert;
                }
                else
                {
                    if (Session["ReadOnly"] == null)
                        FormView1.DefaultMode = FormViewMode.Edit;
                    else
                        FormView1.DefaultMode = FormViewMode.ReadOnly;
                    InventoryDataSource.PrefetchPathToUse = new PrefetchPath2((int)EntityType.InventoryEntity);
                    InventoryDataSource.PrefetchPathToUse.Add(InventoryEntity.PrefetchPathRoom);
                    InventoryDataSource.PrefetchPathToUse.Add(InventoryEntity.PrefetchPathCategory);
                    InventoryDataSource.PrefetchPathToUse.Add(InventoryEntity.PrefetchPathSubcategory);
                    InventoryDataSource.PrefetchPathToUse.Add(InventoryEntity.PrefetchPathCaretakerUser);
                    InventoryDataSource.PrefetchPathToUse.Add(InventoryEntity.PrefetchPathDisposalApprovedByUser);
                    InventoryDataSource.PrefetchPathToUse.Add(InventoryEntity.PrefetchPathInstalledByUser);

                    LLBLGenProDataSource2 ds = (LLBLGenProDataSource2)FormView1.FindControl("caretakerDataSource");
                    SortExpression sort = new SortExpression(UserFields.UFullname | SortOperator.Ascending);
                    ds.SorterToUse = sort;
                }
            }
        }

Thanks again, Tony

goose avatar
goose
User
Posts: 392
Joined: 06-Aug-2007
# Posted on: 29-Mar-2008 19:13:46   

Seems like the exception originates in this part of your code:

... SelectedValue='<%# Eval("ICaretakerId") ...

Please double check the full stack trace of your exception.

TonyLacey
User
Posts: 17
Joined: 11-Mar-2008
# Posted on: 01-Apr-2008 20:00:13   

Thanks Goose.

Here is the Exception. The weird part about this is everything works fine and the dropdownlist is populated fine without those 3 lines of code I added to sort. But with the three lines I get what is below... So those three lines I added IS the official way to add sorting to each dropdownlist?

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

Source Error:

Line 58: </td> Line 59: <td> Line 60: <asp: DropDownList ID="caretakerDropDownList" runat="server" AppendDataBoundItems="true" Line 61: DataSourceID="caretakerDataSource" DataTextField="UFullname" Line 62: DataValueField="UId" SelectedValue='<%# Eval("ICaretakerId") %>'>

Source File: c:\MyFiles\DotNet 2008\inventory\InventoryApp\InventoryApp\admin\inventoryEdit.aspx Line: 60

Stack Trace:

[InvalidOperationException: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.] System.Web.UI.Page.GetDataItem() +2049122 System.Web.UI.TemplateControl.Eval(String expression) +111 ASP.admin_inventoryedit_aspx.__DataBinding__control7(Object sender, EventArgs e) in c:\MyFiles\DotNet 2008\inventory\InventoryApp\InventoryApp\admin\inventoryEdit.aspx:60 System.Web.UI.Control.OnDataBinding(EventArgs e) +99 System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) +11 System.Web.UI.WebControls.ListControl.PerformSelect() +31 System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +70 System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82 System.Web.UI.WebControls.ListControl.OnPreRender(EventArgs e) +26 System.Web.UI.Control.PreRenderRecursiveInternal() +86 System.Web.UI.Control.PreRenderRecursiveInternal() +170 System.Web.UI.Control.PreRenderRecursiveInternal() +170 System.Web.UI.Control.PreRenderRecursiveInternal() +170 System.Web.UI.Control.PreRenderRecursiveInternal() +170 System.Web.UI.Control.PreRenderRecursiveInternal() +170 System.Web.UI.Control.PreRenderRecursiveInternal() +170 System.Web.UI.Control.PreRenderRecursiveInternal() +170 System.Web.UI.Control.PreRenderRecursiveInternal() +170 System.Web.UI.Control.PreRenderRecursiveInternal() +170 System.Web.UI.Control.PreRenderRecursiveInternal() +170 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2041

THANKS! Tony

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 02-Apr-2008 11:18:13   

I remember seeing this bug before, it has nothing to do with LLBLGen Pro, it occurs in rare cases when binding a DropDownList control inside a FormView. And it has something to do with switching the views of a Form view from code behind.

I don't remember exactly the resolution, but would you please try the following code.

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["EditIId"] == null)
            {
                // Inserting new inventory....
                FormView1.DefaultMode = FormViewMode.Insert;
                **FormView1.ChangeMode(FormViewMode.Insert);**
            }
            else
            {
                if (Session["ReadOnly"] == null)
                {
                    FormView1.DefaultMode = FormViewMode.Edit;
                    **FormView1.ChangeMode(FormViewMode.Edit);**
                }
                else
                {
                    FormView1.DefaultMode = FormViewMode.ReadOnly;
                    **FormView1.ChangeMode(FormViewMode.ReadOnly);**
                }

                InventoryDataSource.PrefetchPathToUse = new PrefetchPath2((int)EntityType.InventoryEntity);
                InventoryDataSource.PrefetchPathToUse.Add(InventoryEntity.PrefetchPathRoom);
                InventoryDataSource.PrefetchPathToUse.Add(InventoryEntity.PrefetchPathCategory);
                InventoryDataSource.PrefetchPathToUse.Add(InventoryEntity.PrefetchPathSubcategory);
                InventoryDataSource.PrefetchPathToUse.Add(InventoryEntity.PrefetchPathCaretakerUser);
                InventoryDataSource.PrefetchPathToUse.Add(InventoryEntity.PrefetchPathDisposalApprovedByUser);
                InventoryDataSource.PrefetchPathToUse.Add(InventoryEntity.PrefetchPathInstalledByUser);

                LLBLGenProDataSource2 ds = (LLBLGenProDataSource2)FormView1.FindControl("caretakerDataSource");
                SortExpression sort = new SortExpression(UserFields.UFullname | SortOperator.Ascending);
                ds.SorterToUse = sort;
            }
            **//FormView1.DataBind();// you might need this line too**
        }
    }
TonyLacey
User
Posts: 17
Joined: 11-Mar-2008
# Posted on: 02-Apr-2008 15:23:05   

Thanks alot Walaa. You are certainly correct that is has nothing to do with LLBLGen. I did a search on that error and found tons of hits.. I tried what you suggested and I got the same error unfortunately. I tried a few of the other suggestions on others sites and still get the same error. So now that I know at least I'm doing it the correct way in terms of LLBLGen... I'll make it work some way. It's just weird that I don't get the error until I insert that sorting code.

I appreciate your time VERY much! Take care.

LLBLGen: Still would like my OrderByClause="UFullname, UAge" property! wink

Thank you, Tony

raist
User
Posts: 114
Joined: 19-Apr-2010
# Posted on: 08-Jul-2011 18:27:41   

System.Web.UI.Control.FindControl not always works. The easiest way I have found (v.3.1 on .Net 2.0):

<llblgenpro:LLBLGenProDataSource ID="_SupplierDS" ... OnLoad="setSorter"/>

NOTE: SortingMode="ServerSide" (Default)

Code Behind:

    protected void setSorter(object sender, EventArgs e)
    {
            LLBLGenProDataSource ds = (LLBLGenProDataSource)sender;
            SortExpression sort = new SortExpression(SupplierFields.CompanyName | SortOperator.Ascending);
            ds.SorterToUse = sort;
    }