Results 1 to 6 of 6

Thread: [RESOLVED] access multiple controls in gridview rowupdating

Threaded View

  1. #1

    Thread Starter
    Fanatic Member davebat's Avatar
    Join Date
    Dec 2002
    Posts
    727

    Resolved [RESOLVED] access multiple controls in gridview rowupdating

    I was asked to change a textbox for date of birth into three seperate dropdowns.

    Now I can only find the first one. here is a sample of code

    Code:
    <asp:TemplateField HeaderText="Date of Birth">
          <EditItemTemplate>
            <asp:DropDownList runat="server" ID="ddlDOBday" />
            <asp:DropDownList runat="server" ID="ddlDOBmonth" />
            <asp:DropDownList runat="server" ID="ddlDOByear" />
            <asp:Literal runat="server" ID="litDateError" />  
          </EditItemTemplate>
          <ItemTemplate>
            <asp:Label ID="lblDOB" runat="server" Text='<&#37;# Bind("dob") %>' />
          </ItemTemplate>
        </asp:TemplateField>
    Code:
     Protected Sub grdFamilyMembers_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs)
        Try
          Dim surname, forename As TextBox
          Dim leadapp As CheckBox
          Dim relationship, title, day, month, year As DropDownList
          Dim gender As RadioButtonList
          Dim appID As Integer = Request.QueryString("AppID")
          Dim dateofBirth As String
          Dim dateError As Literal
    
          leadapp = CType(grdFamilyMembers.Rows(e.RowIndex).Cells(0).Controls(1), CheckBox)
          surname = CType(grdFamilyMembers.Rows(e.RowIndex).Cells(1).Controls(1), TextBox)
          forename = CType(grdFamilyMembers.Rows(e.RowIndex).Cells(2).Controls(1), TextBox)
          title = CType(grdFamilyMembers.Rows(e.RowIndex).Cells(3).Controls(1), DropDownList)
    
          day = CType(grdFamilyMembers.Rows(e.RowIndex).Cells(4).Controls(1), DropDownList)
          month = CType(grdFamilyMembers.Rows(e.RowIndex).Cells(4).Controls(2), DropDownList)
          year = CType(grdFamilyMembers.Rows(e.RowIndex).Cells(4).Controls(3), DropDownList)
          dateError = CType(grdFamilyMembers.Rows(e.RowIndex).Cells(4).Controls(4), Literal)
    
          relationship = CType(grdFamilyMembers.Rows(e.RowIndex).Cells(5).Controls(1), DropDownList)
          gender = CType(grdFamilyMembers.Rows(e.RowIndex).Cells(6).Controls(1), RadioButtonList)
          appCustID = grdFamilyMembers.DataKeys(e.RowIndex).Value
    
          Dim relationshipInsert As String
    
           dateofBirth = day.SelectedValue & "/" & month.SelectedValue & "/" & year.SelectedValue
    
          If IsDate(dateofBirth) Then
            dateError.Text = ""
            If leadapp.Checked = False Then
              relationshipInsert = ""
            Else
              relationshipInsert = relationship.SelectedValue
            End If
    
            Try
              _CBLService = New CBLService.CBLService()
              _CBLService.Credentials = System.Net.CredentialCache.DefaultCredentials
              _CBLService.AppCustUpdate(appCustID, appID, forename.Text, surname.Text, title.SelectedValue, leadapp.Checked, dateofBirth, relationshipInsert, gender.SelectedValue)
              grdFamilyMembers.EditIndex = -1
              loadData(True)
            Catch ex As Exception
              Response.Write(ex)
            End Try
    
            grdFamilyMembers.EditIndex = -1
            loadData(True)
          Else
            dateError.Text = "<span style=""color:red;"">Please enter a valid date</span>"
          End If
    
    
    
        Catch ex As Exception
          Response.Write(ex)
        End Try
      End Sub
    It manages to get the day fine, but when it tries to find month it fails with error:

    System.InvalidCastException: Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.DropDownList'

    oN LINE:

    Code:
    month = CType(grdFamilyMembers.Rows(e.RowIndex).Cells(4).Controls(2), DropDownList)
    I assume if the first dropdownlist is control(1), then surely the next one is control(2)?

    Help appreciated as always.
    Last edited by davebat; Feb 20th, 2012 at 11:52 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width