Link to home
Start Free TrialLog in
Avatar of mattibutt
mattibuttFlag for United States of America

asked on

asp.net TableControlRepeater_ItemDataBound for dropdrownlist

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

Avatar of nmarun
nmarun
Flag of India image

Remove the line

SlotShow.DataValueField = ShowID;

and see what happens.

Arun
Avatar of mattibutt

ASKER

nothing happens
u mean you're still seeing only one item in the dropdownlist or even that's missing now?

Arun
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?
Avatar of DBAduck - Ben Miller
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".
Hi
Can you elaborate how I am putting datasource twice I have called it dropdownlist1 as well but it just doesn't work
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.
hi
this is the way ironspeed codes are done its for the purpose of putting datasource inside the pagination region
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.
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
ASKER CERTIFIED SOLUTION
Avatar of DBAduck - Ben Miller
DBAduck - Ben Miller
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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

thanks buddy