Wrox Programmer Forums
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 16th, 2008, 11:26 AM
Authorized User
 
Join Date: Mar 2005
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Default DropDown Within Repeater

I have a dropdown inside a repeater control that I cannot for the life of me figure out how to populate.
There are also a couple of other fields displayed in the repeater. These are bound by a datareader.
Although I would like to bind that dropdown from a value in the datareader right now I'd like to just understand how to bind the dropdown to some values I want to set on the onload event.

This is what I have so far:
Code:
    <asp:Repeater ID="Repeater1" runat="server">
          <HeaderTemplate>
            <table  border='0' width='100%' cellspacing='0' cellpadding='2'
                style='background:#DDDDDD; border-top: black 0.3pt solid; border-left: black 0.3pt solid; 
                        border-bottom: black 0.3pt solid; border-right: black 0.3pt solid'>
                <tr>
                    <th align='left' class='TableHeader'>&nbsp;</th>
                    <th align='left' class='TableHeader'>&nbsp;</th>
                    <th align='left' class='TableHeader'>Question</th>
                    <th align='left' class='TableHeader'>Type</th>
                    <th align='left' class='TableHeader'>Presentation</th> 
                    <th align='left' class='TableHeader'>Ordinal Number</th>                                        
                </tr>
        </HeaderTemplate>
        <ItemTemplate>
                <tr>                    
                    <td class='BaseText' style="width:20px"><asp:CheckBox ID="CheckBox1" runat="server" /><asp:TextBox ID="TextBox1" runat="server" Visible="false" Text='<%#DataBinder.Eval(container.DataItem , "ID")%>'></asp:TextBox></td>
                    <td class='BaseText' style="width:30px"><a href ='MapResponses.aspx?ProjectID=<%=ProjectID%>&SurveyID=<%=SurveyID2%>&QuestionID=<%#DataBinder.Eval(container.DataItem , "ID")%>' class="BaseText">Edit</a></td>
                    <td class='BaseText'><%#DataBinder.Eval(container.DataItem , "ShortQuestion")%></td>
                    <td class='BaseText'><%#DataBinder.Eval(container.DataItem , "TypeDescription")%></td>
                    <td class='BaseText'><%#DataBinder.Eval(Container.DataItem, "PresentationDescr")%></td>
                    <td class='BaseText'><asp:DropDownList ID="ddlOrdinal"  DataTextField="OrdinalNumber" DataValueField="ID" runat="server" width="300px" ></asp:DropDownList></td>

                </tr>
        </ItemTemplate>
        <AlternatingItemTemplate>
                <tr bgcolor='#ffffff'>                    
                    <td class='BaseText' style="width:20px"><asp:CheckBox ID="CheckBox1" runat="server" /><asp:TextBox ID="TextBox1" runat="server" Visible="false" Text='<%#DataBinder.Eval(container.DataItem , "ID")%>'></asp:TextBox></td>
                    <td class='BaseText' style="width:30px" ><a href ='MapResponses.aspx?ProjectID=<%=ProjectID%>&SurveyID=<%=SurveyID2%>&QuestionID=<%#DataBinder.Eval(container.DataItem , "ID")%>' class="BaseText">Edit</a></td>
                    <td class='BaseText'><%#DataBinder.Eval(container.DataItem , "ShortQuestion")%></td>
                    <td class='BaseText'><%#DataBinder.Eval(container.DataItem , "TypeDescription")%></td>
                    <td class='BaseText'><%#DataBinder.Eval(Container.DataItem, "PresentationDescr")%></td>
                    <td class='BaseText'><asp:DropDownList ID="ddlOrdinal" DataTextField="OrdinalNumber" DataValueField="ID" runat="server" width="300px" ></asp:DropDownList></td>
                  </tr>        
        </AlternatingItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
</asp:Repeater>
I read somewhere that I have to use the ItemCreated event of the repeater, but it's not working. What am I doing wrong?:
Code:
Sub Repeater1_ItemCreated(ByVal Sender As Object, ByVal e As RepeaterItemEventArgs)

        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim objDDL As DropDownList = CType(e.Item.FindControl("ddlOrdinal"), DropDownList)
            'objDDL.SelectedIndex = objDDL.Items.IndexOf(objDDL.Items.FindByValue(e.Item.DataItem.ToString))
            objDDL.Items.Add(New ListItem("SELECT", "-1"))
            objDDL.Items.Add(New ListItem("1", "1"))
            objDDL.Items.Add(New ListItem("2", "2"))
            objDDL.Items.Add(New ListItem("3", "3"))
            objDDL.Items.Add(New ListItem("4", "4"))
            objDDL.Items.Add(New ListItem("5", "5"))
            objDDL.Items.Add(New ListItem("6", "6"))
            objDDL.Items.Add(New ListItem("7", "7"))
            objDDL.Items.Add(New ListItem("2", "2"))

        End If
    End Sub
Thanks,
Ninel

 
Old April 16th, 2008, 12:30 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Have you tried hooking up the event handler to the event like this:

Sub Repeater1_ItemCreated(ByVal Sender As Object, ByVal e As RepeaterItemEventArgs) Handles Repeater1.ItemCreated

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
 
Old April 16th, 2008, 12:50 PM
Authorized User
 
Join Date: Mar 2005
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I actually got the dropdown to populate by adding OnItemCreated="Repeater1_ItemCreated" like this:

<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand" OnItemCreated="Repeater1_ItemCreated">

Now my problem is capturing the value of the dropdown when an item is selected.
Any ideas?

 
Old April 16th, 2008, 01:21 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You could do that as well, but the "VB style" is to use the Handles keyword.

You could hook up an handler to the SelectedIndexChanged event of the drop down and then retrieve the value by casting the Sender to a DropDownList:

Dim myList As DropDownList = CType(sender, DropDownList)
...

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
 
Old April 16th, 2008, 01:43 PM
Authorized User
 
Join Date: Mar 2005
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you Imar for your help. I got that part figured out as well.

Now what I am trying to do is remove the hardcoded values that I used to populate the dropdown and populate it with values from a stored procedure. The stored proc that I am currently using populates the other items in the repeater, but also returns the values I need for the dropdown. Can I use that same stored proc or do I need to create a new one that returns values for the dropdown only?

Thank you.

 
Old April 16th, 2008, 01:53 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

It all depends on your setup and personal preference. I prefer to have stored procedures that target one scenario, so you may be better off creating a new procedure that gets the relevant items.

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
 
Old April 16th, 2008, 02:07 PM
Authorized User
 
Join Date: Mar 2005
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Default

So I created a new stored proc that returns the values of the ID and values of field OrdinalNumber and I set it like this:
Code:
Dim dtr  As SqlDataReader = objCommand.ExecuteReader
objDDL.DataSource = dtr
objDDL.DataBind()
What should the dropdown look like? This is what I have:
Code:
<asp:DropDownList ID="ddlOrdinal" autopostback = "true" DataTextField="OrdinalNumber" DataValueField="ID" runat="server" width="100px" ></asp:DropDownList>
Is this correct?


 
Old April 16th, 2008, 02:11 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Does it work? Then I guess it is correct....

In other words: impossible for me to say without a description of the situation and relevant sample code....

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
 
Old April 16th, 2008, 02:30 PM
Authorized User
 
Join Date: Mar 2005
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Default

No, it doesn't work. Here's the code:

Code:
    <asp:Repeater ID="Repeater1" runat="server"  OnItemCreated="Repeater1_ItemCreated">
          <HeaderTemplate>
            <table  border='0' width='100%' cellspacing='0' cellpadding='2'
                style='background:#DDDDDD; border-top: black 0.3pt solid; border-left: black 0.3pt solid; 
                        border-bottom: black 0.3pt solid; border-right: black 0.3pt solid'>
                <tr>
                    <th align='left' class='TableHeader'>&nbsp;</th>
                    <th align='left' class='TableHeader'>&nbsp;</th>
                    <th align='left' class='TableHeader'>Question</th>
                    <th align='left' class='TableHeader'>Type</th>
                    <th align='left' class='TableHeader'>Presentation</th> 
                    <th align='left' class='TableHeader'>Ordinal Number</th>                                        
                </tr>
        </HeaderTemplate>
        <ItemTemplate>
                <tr>                    
                    <td class='BaseText' style="width:20px"><asp:CheckBox ID="CheckBox1" runat="server" /><asp:TextBox ID="TextBox1" runat="server" Visible="false" Text='<%#DataBinder.Eval(container.DataItem , "ID")%>'></asp:TextBox></td>
                    <td class='BaseText' style="width:30px"><a href ='MapResponses.aspx?ProjectID=<%=ProjectID%>&SurveyID=<%=SurveyID2%>&QuestionID=<%#DataBinder.Eval(container.DataItem , "ID")%>' class="BaseText">Edit</a></td>
                    <td class='BaseText'><%#DataBinder.Eval(container.DataItem , "ShortQuestion")%></td>
                    <td class='BaseText'><%#DataBinder.Eval(container.DataItem , "TypeDescription")%></td>
                    <td class='BaseText'><%#DataBinder.Eval(Container.DataItem, "PresentationDescr")%></td>
                    <td class='BaseText'><asp:DropDownList ID="ddlOrdinal"  autopostback = "true" DataTextField="OrdinalNumber" DataValueField="ID"  runat="server" width="100px" ></asp:DropDownList></td>

                </tr>
        </ItemTemplate>
        <AlternatingItemTemplate>
                <tr bgcolor='#ffffff'>                    
                    <td class='BaseText' style="width:20px"><asp:CheckBox ID="CheckBox1" runat="server" /><asp:TextBox ID="TextBox1" runat="server" Visible="false" Text='<%#DataBinder.Eval(container.DataItem , "ID")%>'></asp:TextBox></td>
                    <td class='BaseText' style="width:30px" ><a href ='MapResponses.aspx?ProjectID=<%=ProjectID%>&SurveyID=<%=SurveyID2%>&QuestionID=<%#DataBinder.Eval(container.DataItem , "ID")%>' class="BaseText">Edit</a></td>
                    <td class='BaseText'><%#DataBinder.Eval(container.DataItem , "ShortQuestion")%></td>
                    <td class='BaseText'><%#DataBinder.Eval(container.DataItem , "TypeDescription")%></td>
                    <td class='BaseText'><%#DataBinder.Eval(Container.DataItem, "PresentationDescr")%></td>
                    <td class='BaseText'><asp:DropDownList ID="ddlOrdinal"  autopostback = "true" DataTextField="OrdinalNumber" DataValueField="ID" runat="server" width="100px" ></asp:DropDownList></td>
                  </tr>        
        </AlternatingItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
    </asp:Repeater>
Code:
  Sub Repeater1_ItemCreated(ByVal Sender As Object, ByVal e As RepeaterItemEventArgs)
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim objDDL As DropDownList = CType(e.Item.FindControl("ddlOrdinal"), DropDownList)

            objDDL.SelectedIndex = objDDL.Items.IndexOf(objDDL.Items.FindByValue(e.Item.DataItem.ToString))

            Dim objConn As nsSqlClient.SqlConnection
            Dim returnValue As Int32
            Dim objCommand As nsSqlClient.SqlCommand

            objConn = New nsSqlClient.SqlConnection
            objConn.ConnectionString = m_strConnection
            objConn.Open()

            objCommand = New nsSqlClient.SqlCommand("rspGetOrdinalAllQuestionsBySurvey", objConn)
            objCommand.CommandType = nsData.CommandType.StoredProcedure
            objCommand.CommandTimeout = 1320

            'Creating parameter for Procedure
            objCommand.Parameters.Add(CreateParam("@RETURN_VALUE", nsData.SqlDbType.Int, 0, nsData.ParameterDirection.ReturnValue, returnValue))
            objCommand.Parameters.Add(CreateParam("@ClientID", nsData.SqlDbType.VarChar, 10, nsData.ParameterDirection.Input, ClientID))
            objCommand.Parameters.Add(CreateParam("@SurveyID", nsData.SqlDbType.VarChar, 20, nsData.ParameterDirection.Input, surveyID))
            objCommand.ExecuteNonQuery()

            returnValue = DirectCast(objCommand.Parameters("@RETURN_VALUE").Value, Int32)
            If returnValue <> 0 Then
                Exit Sub
            Else
                Dim dtr As SqlDataReader = objCommand.ExecuteReader
                'Repeater1.DataSource = dtr
                'Repeater1.DataBind()
                objDDL.DataSource = dtr
                objDDL.DataBind()
            End If
            AddHandler objDDL.SelectedIndexChanged, AddressOf ddlordinal_SelectedIndexChanged

        End If
end sub
The stored proc returns the following:
ID OrdinalNumber --> This is the text I want to see in the dropdown
140 NULL
103 1
5 1
4 4
100 6



 
Old April 16th, 2008, 02:56 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

And what exactly isn't working?

This would be so much easier if you:

a) Posted only the relevant pieces of code. No need to post long blocks of HeaderTemplates for example

b) Added a few line breaks in your code so the code doesn't get 10 feet wide forcing me to scroll in all directions to see what's going on.

c) Added a specific description of "does not work", including any error messages you get, the values that your variables contain during debugging and so on.

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Dropdown in repeater (Moved From C# Forum) rodmcleay ASP.NET 1.0 and 1.1 Basics 14 June 20th, 2008 07:11 AM
Dropdown in Repeater kylemccullough ASP.NET 1.0 and 1.1 Basics 7 June 17th, 2004 01:04 PM
Repeater collie VB.NET 2002/2003 Basics 1 December 2nd, 2003 05:30 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.