[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='<%# 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.
Re: access multiple controls in gridview rowupdating
Hello,
Have you tried:
month = CType(grdFamilyMembers.Rows(e.RowIndex).FindControl("ddlDOBmonth"), DropDownList)
Looks like your second control is the Literal Control, not the DropDownList. I think I am right in saying that the order of the controls that are added to the cell is not guaranteed. Hence why looking for the control by name, is a better approach.
The other idea would be to create a UserControl, that contains all of the controls that you require, and have that expose properties that you are looking for.
Hope that helps!
Gary
Re: access multiple controls in gridview rowupdating
Thanks once again Gep, I used the find control method, I should have thought of that!
Re: access multiple controls in gridview rowupdating
Not a problem at all, glad to hear that it worked!
Gary
Re: [RESOLVED] access multiple controls in gridview rowupdating
Good to hear, based on your photo i always imagine you getting quite exasperated answering all these stupid questions lol.
Cheers again.
Re: [RESOLVED] access multiple controls in gridview rowupdating
Ha ha, no no, not at all! I like to help out if I can.
That photo was the result of far too many drinks on a lads holiday in Cancun :)
Gary