Solved

add items to dropdownlist inside reapeater

Posted on 2011-02-24
11
245 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

16 Experts available now in Live!

Get 1:1 Help Now