Solved

add items to dropdownlist inside reapeater

Posted on 2011-02-24
11
254 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

713 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