Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

asp.net TableControlRepeater_ItemDataBound for dropdrownlist

Posted on 2010-09-05
14
Medium Priority
?
370 Views
Last Modified: 2012-05-10
hi
i am trying to load some data from the database on my initial stage i am just looking at different options to display the data right now i am stuck on dropdownlist issue
when i try to assign data to the dropdownlist object it only loads one records
please advice i am attaching the code with this post

<asp:Repeater runat="server" id="TableControlRepeater" OnItemDataBound="TableControlRepeater_ItemDataBound" EnableViewState="false">
                                                <itemtemplate>
                                                    <div id="AJAXUpdateRecordRow">
                                                    <tr>
                                                   
                                                        <td class="tic" scope="row"><asp:ImageButton runat="server" id="RecordRowEditButton" CausesValidation="False" CommandName="Redirect" CssClass="button_link" ImageURL="~/Images/icon_edit.gif" CommandArgument='<%# Eval("VideoItemID") %>' ToolTip="Edit Record" AlternateText="" OnClick="RecordRowButton_Click" /></td>
                                                        <td class="tic" onclick="moveToThisTableRow(this);"><asp:CheckBox runat="server" id="RecordRowSelection" /></td>
                                                    
                                                        <td class="ttc" ><asp:Literal runat="server" id="VideoFileName" Text='<%# Eval("VideoStartTime") %>' /></td>
                                                        <td class="ttc" ><asp:Literal runat="server" id="Show" Text='<%# Eval("VideoItemName") %>' /></td>
                                                         
                                                                        <td class="ttc" ><asp:Literal runat="server" id="Literal1" Text='<%# Eval("VideoItemName") %>' /></td>
                                               
                                                        <td class="ttc" ><asp:TextBox runat="server" id="Rating" Text='<%# Eval("VideoItemID") %>' /></td>
                                                          <td class="ttc" > 
                                                              <asp:DropDownList ID="DropDownList1" runat="server">
                                                              </asp:DropDownList> </td>
                                                            <td>
                                               <BCS:BCSStories ID="storyid" runat="server" CssClass="field_input" 
                                                   onkeypress="dropDownListTypeAhead(this,false)" />
                                           </td>
                                                    </tr>
                                                    </div>
                                                </ITEMTEMPLATE>
                                                </asp:Repeater> 


  public void TableControlRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        try
        {
  
            string ShowID = ((HiddenField)e.Item.FindControl("VideoFileName")).Value;
            DropDownList SlotShow = (DropDownList)e.Item.FindControl("DropDownList").Controls[0];
            SlotShow.SelectedValue = ShowID;
            SlotShow.DataValueField = ShowID;
       

        }
        catch (Exception ex)
        {
        }
    }


 private void LoadItems()
    {
        try
        {
            SlotVideoItemsTableAdapter da = new SlotVideoItemsTableAdapter();
            DataTable dt = da.GetData(Convert.ToInt32(Session["UserID"].ToString()), TextBox1.Text);
        
        
            if (m_sortField != "")
                dt.DefaultView.Sort = m_sortField + " " + m_sortOrder;

            #region Search and Filters
            //
            // search and filter criterias
            //
            string _suffix = " AND ";
            string _rowFilter = "";
            string _searchArea = SearchArea.Text.Trim();
            // search string
            if (_searchArea.Length > 0)
            {
                _rowFilter += " ((VideoItemName LIKE '%" + _searchArea + "%')";
                _rowFilter += " OR (VideoFileName LIKE '%" + _searchArea + "%'))";
            }
            // filters
            if (((DropDownList)GameFilter.Controls[0]).SelectedValue != "0" && ((DropDownList)GameFilter.Controls[0]).SelectedIndex != -1)
            {
                string _filter1 = " GameID = " + ((DropDownList)GameFilter.Controls[0]).SelectedValue;
                if (_rowFilter.Length > 0)
                    _rowFilter += _suffix + _filter1;
                else
                    _rowFilter += _filter1;
            }

            if (((DropDownList)StoryFilter.Controls[0]).SelectedValue != "0" && ((DropDownList)StoryFilter.Controls[0]).SelectedIndex != -1)
            {
                string _filter1 = " StoryID = " + ((DropDownList)StoryFilter.Controls[0]).SelectedValue;
                if (_rowFilter.Length > 0)
                    _rowFilter += _suffix + _filter1;
                else
                    _rowFilter += _filter1;
            }
            if (((DropDownList)StoryFilter.Controls[0]).SelectedValue != "0" && ((DropDownList)StoryFilter.Controls[0]).SelectedIndex != -1)
            {
                string _filter1 = " ShowID = " + ((DropDownList)ShowFilter.Controls[0]).SelectedValue;
                if (_rowFilter.Length > 0)
                    _rowFilter += _suffix + _filter1;
                else
                    _rowFilter += _filter1;
            }
            if (((DropDownList)PlaceFilter.Controls[0]).SelectedValue != "-1" && ((DropDownList)PlaceFilter.Controls[0]).SelectedIndex != -1)
            {
                string _filter1 = " PlacementID = " + ((DropDownList)PlaceFilter.Controls[0]).SelectedValue;
                if (_rowFilter.Length > 0)
                    _rowFilter += _suffix + _filter1;
                else
                    _rowFilter += _filter1;
            }

            if (_rowFilter.Length > 0)
                dt.DefaultView.RowFilter = _rowFilter;
            //
            // end of search and filters
            //
            #endregion

            TableControlRepeater.DataSource = dt;
            TableControlRepeater.DataBind();

            PagedDataSource objPds = new PagedDataSource();
            objPds.DataSource = dt.DefaultView;
            objPds.AllowPaging = true;
            objPds.PageSize = m_pageSize;

            objPds.CurrentPageIndex = m_currentPage;

            // placing number of page out of total pages for display
            Pagination.CurrentPage.Text = (m_currentPage + 1).ToString();
            Pagination.TotalPages.Text = objPds.PageCount.ToString();
            Pagination.TotalItems.Text = objPds.Count.ToString();
            m_totalPages = objPds.PageCount - 1;

            // Disable Prev or Next buttons if necessary
            Pagination.PreviousPage.Enabled = !objPds.IsFirstPage;
            Pagination.NextPage.Enabled = !objPds.IsLastPage;
            Pagination.FirstPage.Enabled = !objPds.IsFirstPage;
            Pagination.LastPage.Enabled = !objPds.IsLastPage;

            TableControlRepeater.DataSource = objPds;
            TableControlRepeater.DataBind();

        }
        catch (Exception ex)
        {
            Helper.StoreErrLog("UserAnimations", "LoadItems", ex.ToString());
        }
    }

Open in new window

0
Comment
Question by:mattibutt
[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
  • 8
  • 4
  • 2
14 Comments
 
LVL 27

Expert Comment

by:nmarun
ID: 33606685
Remove the line

SlotShow.DataValueField = ShowID;

and see what happens.

Arun
0
 
LVL 11

Author Comment

by:mattibutt
ID: 33606708
nothing happens
0
 
LVL 27

Expert Comment

by:nmarun
ID: 33606719
u mean you're still seeing only one item in the dropdownlist or even that's missing now?

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

Author Comment

by:mattibutt
ID: 33606727
i am not seeing anything in the dropdown list here is the show if you look at the parent control which is TableControlRepeater and i am able to load stuff inside the textbox but dropdown box doesnt work question is it possible to load dropdownlist inside the TableControlRepeater?
0
 
LVL 25

Expert Comment

by:DBAduck - Ben Miller
ID: 33607146
You are putting the DataSource and DataBind 2 times on your TableControlRepeater.  That will cause some interesting things to happen.

Also when you e.Item.FindControl you are looking for "DropDownList" it should be "DropDownList1".
0
 
LVL 11

Author Comment

by:mattibutt
ID: 33609631
Hi
Can you elaborate how I am putting datasource twice I have called it dropdownlist1 as well but it just doesn't work
0
 
LVL 25

Expert Comment

by:DBAduck - Ben Miller
ID: 33612881
In your code above, if you look at line 112, and line 134, they are in the same Item_DataBound and you are setting the DataSource in 112 and DataBind and then in 134 you do the same thing with a different data source.  So there will be some confusion there.
0
 
LVL 11

Author Comment

by:mattibutt
ID: 33613080
hi
this is the way ironspeed codes are done its for the purpose of putting datasource inside the pagination region
0
 
LVL 25

Expert Comment

by:DBAduck - Ben Miller
ID: 33613097
OK, the first one really does not do anything and probably should be removed, but the other problem I alluded to is on line 34 you have

          DropDownList SlotShow = (DropDownList)e.Item.FindControl("DropDownList").Controls[0];
 
If you see that the control name which is what FindControl uses is DropDownList and the actual name is DropDownList1.  This is what I was talking about.
0
 
LVL 11

Author Comment

by:mattibutt
ID: 33613840
HI
I HAVE TRIED EVERYTHING NAMING DROPDOWNLIST1 ETC WHAT I HAVENT TRIED IS BASICALLY TAKING OUT LINE 134 RIGHT NOW I AM TRYING FROM DIFFERENT ANGLE INSTEAD OF PUTTING INSIDE THE REPEATER I AM PUTTING IN DATALIST HOWEVER SO FAR I AM NOT ABLE TO LOAD ITEMS IN THE DROPDOWN LIST
0
 
LVL 25

Accepted Solution

by:
DBAduck - Ben Miller earned 2000 total points
ID: 33614843
Well, first of all you need to get things completely in place.  there are many articles on the web about filling a dropdownlist in a repeater.

The first thing is to differentiate the items that are being thrown at you in the repeater_itemdatabound.

if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
 
     myDropDownList =  (DropDownList)e.Item.FindControl("DropDownList1");
     myDropDownList.Items.Add(new ListItem("name","value"));
}

Just put that in the repeater Item DataBound and see what you get.  This is the first key.  Then when you have it actually creating the dropdownlist, then you can extend it to do more.  You swallow the exception in your Item_DataBound and the first line will cause an exception when it is not there, also when you get controls[0] from your FindControl, that could cause an exception in the header, so put the code above in the function and see if you get 1 item and then we can move on.

Don't worry, it is not really difficult, just very particular, and you are getting there.
0
 
LVL 11

Author Comment

by:mattibutt
ID: 33616898
hi dbaduck
thanks so much for your response i have tested it and it works now the issue is i want to insert selected item inside the web user control when i try to do that i get the error
System.InvalidCastException: Unable to cast object of type 'ASP.usercontrols_dropdownlists_bcsusersegmentlist_ascx' to type 'System.Web.UI.WebControls.DropDownList'.

if i try to explain again basically when i put this inside the standard dropdownlist it works perfectly fine however when i insert inside the web user control based drop down it doesnt work

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                string vname = ((HiddenField)e.Item.FindControl("Vname")).Value;
                string ShowID = ((HiddenField)e.Item.FindControl("ShowIDs")).Value;
                DropDownList myDropDownList = (DropDownList)e.Item.FindControl("Dropdownlist1");
//i have to change the above line mydropdownlist stand alone catches error but when i put dropdownlist it works fine
               // DropDownList myDropDownLists = (DropDownList)e.Item.FindControl("SlotShow");
              myDropDownList.Items.Add(new ListItem(vname, ShowID));


            }

Open in new window

0
 
LVL 11

Author Comment

by:mattibutt
ID: 33617138
hi buddy
thanks so much for your help i have manage to sort it out and now its loading inside the web user control

0
 
LVL 11

Author Closing Comment

by:mattibutt
ID: 33617139
thanks buddy
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

722 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