Solved

add items to dropdownlist inside reapeater

Posted on 2011-02-24
11
264 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

 
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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

623 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