Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a gridview with an edit template. The edit template contains a drop down list which i am trying to populate when the gridview is created. However, i am unable to get hold of the drop down list. I performed the same procedure somewhere else in the project and it works fine, but its not retrieved the drop down list in this case. Kindly help:

My Grid Code:


XML
<asp:GridView ID="gvCallHistory" runat="server" AutoGenerateColumns="False"
                         CellPadding="3" Width="100%" Font-Names="Arial" Font-Size="14px"
                         DataKeyNames="LogID">
                         <RowStyle CssClass="ProfileRow" Wrap="False" />
                         <Columns>
                             <asp:TemplateField HeaderText="Time of Call">
                                 <EditItemTemplate>
                                     <asp:TextBox ID="txtCallTime" runat="server" BorderStyle="None" Height="25px"
                                         Text='<%# Bind("CallTime") %>'></asp:TextBox>
                                 </EditItemTemplate>
                                 <ItemTemplate>
                                     <asp:Label ID="Label1" runat="server" Text='<%# Bind("CallTime") %>'></asp:Label>
                                 </ItemTemplate>
                             </asp:TemplateField>
                             <asp:TemplateField HeaderText="Comments">
                                 <EditItemTemplate>
                                     <asp:TextBox ID="txtComments" runat="server" BorderStyle="None" Height="63px"
                                         MaxLength="300" Text='<%# Bind("Comments") %>' TextMode="MultiLine"
                                         Width="218px" Font-Names="Arial" Font-Size="14px"></asp:TextBox>
                                 </EditItemTemplate>
                                 <ItemTemplate>
                                     <asp:Label ID="Label2" runat="server" Text='<%# Bind("Comments") %>'></asp:Label>
                                 </ItemTemplate>
                             </asp:TemplateField>
                             <asp:TemplateField HeaderText="Attended By">
                                 <EditItemTemplate>
                                     <asp:DropDownList ID="ddlEditFaculty" runat="server" Font-Size="14px"
                                         SelectedValue='<%# Eval("FacultyID") %>' Width="150px">
                                     </asp:DropDownList>
                                 </EditItemTemplate>
                                 <ItemTemplate>
                                     <asp:Label ID="lblFname" runat="server" Text='<%# Bind("FName") %>'></asp:Label>
                                     <asp:Label ID="lblMName" runat="server" Text='<%# Bind("MName") %>'></asp:Label>
                                     <asp:Label ID="lblLName" runat="server" Text='<%# Bind("LName") %>'></asp:Label>
                                 </ItemTemplate>
                             </asp:TemplateField>
                             <asp:TemplateField HeaderText="Attended At">
                                 <EditItemTemplate>
                                     <asp:DropDownList ID="ddlEditLocation" runat="server" Font-Size="14px"
                                         SelectedValue='<%# Eval("LocationID") %>' Width="150px">
                                     </asp:DropDownList>
                                 </EditItemTemplate>
                                 <ItemTemplate>
                                     <asp:Label ID="Label3" runat="server" Text='<%# Bind("Location") %>'></asp:Label>
                                 </ItemTemplate>
                             </asp:TemplateField>
                             <asp:TemplateField ShowHeader="False">
                                 <EditItemTemplate>
                                     <asp:CheckBox ID="chkIncoming" runat="server" Checked='<%# Eval("Incoming") %>'
                                         Font-Size="14px" Text="Incoming Call" />
                                 </EditItemTemplate>
                                 <ItemTemplate>
                                     <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
                                         CommandName="Delete" Text="Delete"></asp:LinkButton>
                                 </ItemTemplate>
                             </asp:TemplateField>
                             <asp:CommandField ShowEditButton="True" />
                         </Columns>
                         <HeaderStyle CssClass="ProfileMiddle" HorizontalAlign="Left" />
                         <AlternatingRowStyle CssClass="ProfileAlternateRow" Wrap="False" />
                     </asp:GridView>




Code Behind:


VB
Protected Sub gvCallHistory_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvCallHistory.RowDataBound
    Try
        If e.Row.RowType = DataControlRowType.DataRow Then

            If gvCallHistory.DataKeys(e.Row.RowIndex).Values("Incoming") = True Then
                e.Row.ForeColor = Drawing.Color.Black
                e.Row.BackColor = Drawing.Color.FromArgb(51, 204, 51)
            ElseIf gvCallHistory.DataKeys(e.Row.RowIndex).Values("Incoming") = False Then
                e.Row.ForeColor = Drawing.Color.White
                e.Row.BackColor = Drawing.Color.FromArgb(230, 19, 19)
            End If


            Dim ddlFaculty As DropDownList = CType(e.Row.FindControl("ddlEditFaculty"), DropDownList)
            Dim ddlLocation As DropDownList = CType(e.Row.FindControl("ddlEditLocation"), DropDownList)

            Dim myUtil As New Utility
            myUtil.FillFaculty(ddlFaculty)
            myUtil.FillLocations(ddlLocation)

        End If

    Catch ex As Exception
    End Try
End Sub


ddlFaculty and ddlLocation is nothing when i try to get it from the rows.
Posted
Updated 13-Jul-23 23:56pm

You can get the controls within the EditItemTemplate in the RowDataBound event

Protected Sub gvCallHistory_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvCallHistory.RowDataBound
Try
        If e.Row.RowType = DataControlRowType.DataRow Then
          If e.Row.RowState = DataControlRowState.Edit Then
                DropDownList ddl = GridView1.Rows          [e.RowIndex].FindControl("ddlEditFaculty") as DropDownList; 
          End If
        End If
Catch ex As Exception
End Try
End Sub


If this is correct answer then Marked as Answer.
 
Share this answer
 
Comments
freefika 21-Sep-10 5:25am    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
You can add a suitable event handler that is fired when user updates the data in the edit mode (Say, RowUpdating) and find the control using the RowIndex of the corresponding row that is currently being updated. That is, something llike the following (I used C# code to demonstrate the idea) :

C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
        DropDownList ddl = GridView1.Rows[e.RowIndex].FindControl("ddlEditFaculty") as DropDownList;
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900