Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

add items to dropdownlist inside reapeater

Posted on 2011-02-24
11
Medium Priority
?
279 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

886 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