Question

ive had a few questions on this subject, still having problems.

I want to find the values from a number of dropdown and textbox controls inside a repeater control.

db.ConnectionString = SystemConnString
    db.Open()

    Dim selectedAdTitle As String = ""
    Dim enteredAdFullName As String = ""

    cmd.Parameters.Add(New SqlParameter("@TransactionID", TransactionID))
    cmd.Parameters.Add(New SqlParameter("@AdTitle", selectedAdTitle))
    cmd.Parameters.Add(New SqlParameter("@AdFullName", enteredAdFullName))

    For i As Integer = 0 To myRepeater.Items.Count - 1

        Dim AdTitle As DropDownList = DirectCast(myRepeater.Items(i).FindControl("AdTitle"), DropDownList)
        Dim AdFullName As TextBox = DirectCast(myRepeater.Items(i).FindControl("AdFullName"), TextBox)

        selectedAdTitle = AdTitle.Text
        enteredAdFullName = AdFullName.Text

        cmd.Parameters("@AdTitle").Value = selectedAdTitle
        cmd.Parameters("@AdFullName").Value = enteredAdFullName

        SQL = ""
        SQL = SQL & "INSERT INTO InsuredPersons (TransactionID,Title,FullName) VALUES ("
        SQL = SQL & "@TransactionID,"
        SQL = SQL & "@AdTitle,"
        SQL = SQL & "@AdFullName"
        SQL = SQL & ")"

        cmd.CommandText = SQL
        cmd.Connection = db
        cmd.ExecuteNonQuery()
    Next

AdTitle and AdFullName dont seem to be bringing across the values. There is no error so they have found the control ok. Below is the ASPX file code.

<asp:Repeater ID="myRepeater" runat="server">
    <ItemTemplate>
        <asp:DropDownList ID="AdTitle" runat="server">
            <asp:ListItem Selected="True" Value="" Text=""/>
            <asp:ListItem Selected="False" Value="Miss" Text="Miss"/>
            <asp:ListItem Selected="False" Value="Ms" Text="Ms"/>
            <asp:ListItem Selected="False" Value="Mrs" Text="Mrs"/>
            <asp:ListItem Selected="False" Value="Mr" Text="Mr"/>
            <asp:ListItem Selected="False" Value="Other" Text="Other"/>
        </asp:DropDownList>

       <asp:TextBox ID="AdFullName" runat="server"></asp:TextBox>
   </ItemTemplate>

Edit:

Repeater is constructed on page load

    Dim repeatTimes((TotalAdInsured - 1)) As Integer

    myRepeater.DataSource = repeatTimes
    myRepeater.DataBind()

DirectCast is done on button click

Protected Sub continueButtonDetails_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles continueButtonDetails.Click

Answer: Had to put IsPostback around Repeater Construction.

If Not IsPostBack() Then

        Dim repeatTimes((TotalAdInsured - 1)) As Integer

        myRepeater.DataSource = repeatTimes
        myRepeater.DataBind()

    End If
Was it helpful?

Solution

First off I think you want:

myDropDown.SelectedItem.Text

Rather Than

myDropDown.Text

Also why do you have two ItemTemplates? I didn't know you could even do that...

Are you interacting with any of the TextBoxes or DropDowns at any other point during the page lifecycle?

Try putting a PostBack check around the repeater databinding. I think whats happening is your loading the controls dynamically, therefore they have no viewstate, therefore the values will always be empty.

OTHER TIPS

Make sure your are running your code at the correct point in the page life cycle. If you are doing it too early (for example in the OnInit) then it won't have the values from the client yet. Try moving it to your OnCommand()/OnSubmit() event and see what happens. Here are some references on ASP.NET Page Life Cycle:

  1. http://msdn.microsoft.com/en-us/library/ms178472.aspx
  2. http://www.15seconds.com/issue/020102.htm
  3. http://www.beansoftware.com/ASP.NET-Tutorials/Page-Life-Cycle.aspx
  4. Picture of asp.net page life cycle
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top