Solved

add items to dropdownlist inside reapeater

Posted on 2011-02-24
11
247 Views
Last Modified: 2012-05-11
hi all code below

TradesList doesnt exist in the current context, is it becuase its inside the repeater?
how can i get the below to work?

thanks
################ aspx page ###################
<asp:Repeater ID="Repeater1" runat="server">
            <ItemTemplate>
                <tr>
                    <td align="center">
                        <input class="CheckBox" value='<%# Eval("ID") %>' type="checkbox" />
                    </td>
                    <td>
                        <asp:Label runat="server" Text='<%# Eval("DateAdded") %>' />
                    </td>
                    <td>
                        <asp:Label runat="server" Text='<%# Eval("CompanyName") %>' />
                    </td>
                    <td>
                        <asp:Label runat="server" Text='<%# Eval("CompanyType") %>' />
                    </td>
                    <td>
                        <asp:Label runat="server" Text='<%# Eval("CompanyReg") %>' />
                    </td>
                    <td>
                        <asp:DropDownList runat="server" ID="TradesList"></asp:DropDownList>
                    </td>
                    <td>
                        <asp:Label runat="server" Text='<%# Eval("Address1") %>' />
                    </td>
                    <td>
                        <asp:Label runat="server" Text='<%# Eval("Postcode") %>' />
                    </td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>
############### code behind #########################
CompanyDataContext db = new CompanyDataContext();
        var results = from Data in db.CompanyInfos select Data;
        Repeater1.DataSource = results;
        Repeater1.DataBind();

        
        IQueryable<CompanyInfo> companyInfos = DB.Functions.GetCompanyInfo();

        foreach (CompanyInfo Result in companyInfos) {
            string[] TradeNames = Result.Trades.Split(',');
            Array.Sort(TradeNames);

            foreach (string result in TradeNames)
            {
                TradesList.???
            }

        }

Open in new window

0
Comment
Question by:awilderbeast
  • 6
  • 4
11 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34971970
You have to bind it in the ItemDataBound event for the repeater. There is no context for a repeater until the point where it is being bound. The markup is purely a template that will be used for each item being bound.
protected void YourRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
      if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
      {
            DropDownList dd = e.Item.FindControl("TradesList") as DropDownList;

            // select items, bind to dropdown, etc, etc
      }
}

Open in new window

0
 
LVL 1

Author Comment

by:awilderbeast
ID: 34977827
ok i have no errors now but its just not working, can you have a look for me?

cheers
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DropDownList dd = e.Item.FindControl("TradesList") as DropDownList;

            IQueryable<CompanyInfo> companyInfos = DB.Functions.GetCompanyInfo();
            foreach (CompanyInfo Result in companyInfos)
            {
                string[] TradeNames = Result.Trades.Split(',');
                Array.Sort(TradeNames);

                foreach (string result in TradeNames)
                {
                    dd.Items.Add(new ListItem(result, result));

                }      
            }
            dd.DataBind();
        }
    }

Open in new window

0
 
LVL 9

Expert Comment

by:mayank_joshi
ID: 34978362
once you have added items to dropdownlist dd don't call dd.DataBind() beacause no data source is assigned to dd.The following code will be enough:-

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DropDownList dd = e.Item.FindControl("TradesList") as DropDownList;
            dd.Items.Clear();

            IQueryable<CompanyInfo> companyInfos = DB.Functions.GetCompanyInfo();
            foreach (CompanyInfo Result in companyInfos)
            {
                string[] TradeNames = Result.Trades.Split(',');
                Array.Sort(TradeNames);

                foreach (string result in TradeNames)
                {
                    dd.Items.Add(new ListItem(result, result));

                }      
            }
           
        }
    }






0
 
LVL 1

Author Comment

by:awilderbeast
ID: 34978443
this doesnt work, ive realised i need to get the trades from the repeater databound, too, it still doesnt work, but will what i have below work if i change what is orginally not working?


protected void Page_Load(object sender, EventArgs e)
    {
        //GET DB DATA
        CompanyDataContext db = new CompanyDataContext();
        var results = from Data in db.CompanyInfos select Data;
        Repeater1.DataSource = results;
        Repeater1.DataBind();

    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DropDownList dd = e.Item.FindControl("TradesList") as DropDownList;
            dd.Items.Clear();

            string TNames = Convert.ToString(Eval("Trades"));
            string[] TradeNames = TNames.Split(',');
            Array.Sort(TradeNames);

            foreach (string result in TradeNames)
            {
                dd.Items.Add(new ListItem(result, result));

            }
        }
    }

Open in new window

0
 
LVL 9

Expert Comment

by:mayank_joshi
ID: 34978479
the above code seems to be correct
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 1

Author Comment

by:awilderbeast
ID: 34978647
yeah no errors, the drop down lists have nothing in them :S
0
 
LVL 1

Author Comment

by:awilderbeast
ID: 34978663
i think its cos i didnt add

<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_OnItemDataBound" >

now i have done that im getting

can i not use eval there then?
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 64:             dd.Items.Clear();
Line 65: 
Line 66:             string TNames = Convert.ToString(Eval("Trades"));

Open in new window

0
 
LVL 9

Expert Comment

by:mayank_joshi
ID: 34978753
you cannot use eval in code behind.add the "Trades" field in datakeys property of gird.


0
 
LVL 1

Author Comment

by:awilderbeast
ID: 34978755
how do i do that?
0
 
LVL 9

Accepted Solution

by:
mayank_joshi earned 500 total points
ID: 34978867
use a hidden field in repeater on aspx page like:-

<asp:HiddenField ID="hfId" Value='<%# Eval("Trades")%>' runat="server" />


then on item databound event add the code:-

 HiddenField hfId = item.FindControl("hfId") as HiddenField;
 string trade =Convert.ToString(hfId.Value);
0
 
LVL 1

Author Comment

by:awilderbeast
ID: 34978905
thanks alot!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now