Solved

asp.net TableControlRepeater_ItemDataBound for dropdrownlist

Posted on 2010-09-05
14
334 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
  • 8
  • 4
  • 2
14 Comments
 
LVL 27

Expert Comment

by:nmarun
Comment Utility
Remove the line

SlotShow.DataValueField = ShowID;

and see what happens.

Arun
0
 
LVL 11

Author Comment

by:mattibutt
Comment Utility
nothing happens
0
 
LVL 27

Expert Comment

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

Arun
0
 
LVL 11

Author Comment

by:mattibutt
Comment Utility
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 24

Expert Comment

by:DBAduck - Ben Miller
Comment Utility
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
Comment Utility
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 24

Expert Comment

by:DBAduck - Ben Miller
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 11

Author Comment

by:mattibutt
Comment Utility
hi
this is the way ironspeed codes are done its for the purpose of putting datasource inside the pagination region
0
 
LVL 24

Expert Comment

by:DBAduck - Ben Miller
Comment Utility
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
Comment Utility
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 24

Accepted Solution

by:
DBAduck - Ben Miller earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks buddy
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

743 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