I have nested gridviews with the child inside an update panel. I have loaded a DropDownList in the first row of the child gridview and have loaded checklists in all other rows of the child gridview (I hid the checklists in the first row and the dropdownlist in all other rows). When the User changes the dropdownlist, the SelectedIndexChanged is not firing for that dropdownlist. Aslo, if I use the button, a post-back occurs and resets the DropDownList (I don't know how this is occuring). Any help will be well appreciated. Following is my code.
Thank you in advance for your help.
<asp:Button ID="btnApplyFilter" runat="server" Text="Apply Filter" ViewStateMode="Enabled"
OnClick="btnApplyfilter_Click" />
<asp:GridView ID="gvFilterParent" runat="server" ShowFooter="True" AutoGenerateColumns="false" CssClass="Grid" ShowHeader="False"
DataKeyNames="filID" AlternatingRowStyle-BackColor="silver"
EnableViewState="true" OnRowDataBound="gvFilterParent_OnRowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img alt = "" style="cursor: pointer" src="images/plus.png" />
<asp:UpdatePanel ID="pnFilter"
ChildrenAsTriggers="true"
EnableViewState="true"
runat="server"
Style="display: none">
<ContentTemplate>
<asp:GridView ID="gvFilter" ShowHeader="False" runat="server"
AlternatingRowStyle-BackColor="silver" EnableViewState="true"
AutoGenerateColumns="false" CssClass = "ChildGrid">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:UpdatePanel ID="ddlUpdate" runat="server" >
<ContentTemplate>
<aspropDownList ID="ddlFunCat" runat="server"
DataTextField="Filter"
DataValueField="FiltersId"
OnSelectedIndexChanged="ddlFunCat_SelectedIndexChanged
visible="false" AutoPostBack="true"
EnableViewState="true"></aspropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlFunCat"
EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkselect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField visible="false" DataField="FiltersId" />
<asp:BoundField ItemStyle-Width="150px" DataField="Filter" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ItemStyle-Width="150px" DataField="filId" visible="false" />
<asp:BoundField ItemStyle-Width="150px" DataField="filname" />
</Columns>
</asp:GridView>
CODE BEHIND
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Using conn As New OleDbConnection(ConfigurationManager.ConnectionStrings("TestCases").ToString())
conn.Open()
'Load Gridview
gvFilterParent.DataSource = GetData("Select * from Filters where filParentID = " & 0)
gvFilterParent.DataBind()
conn.Close()
End Using
End If
End Sub
Private Shared Function GetData(query As String) As DataTable
Dim strConnString As String = ConfigurationManager.ConnectionStrings
("TestCases").ConnectionString
Using con As New OleDbConnection(strConnString)
Using cmd As New OleDbCommand()
cmd.CommandText = query
Using sda As New OleDbDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using ds As New DataSet()
Dim dt As New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Using
End Function
Protected Sub gvFilterParent_OnRowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
'Bind Filters
Dim gvChild As GridView = TryCast(e.Row.FindControl("gvFilter"), GridView)
Dim filId As String = gvFilterParent.DataKeys(e.Row.RowIndex).Value.ToString()
If gvChild IsNot Nothing Then
gvChild.DataSource = GetData(String.Format("Select * from qFilters where filters_1.filId={0}", filId))
gvChild.DataBind()
If filId = "1" Then
For Each childrow As GridViewRow In gvChild.Rows
If childrow.RowType = DataControlRowType.DataRow Then
Dim drplist As DropDownList = DirectCast(childrow.FindControl("ddlFunCat"), DropDownList)
If drplist IsNot Nothing Then
Dim ds As DataSet = New DataSet()
ds = ViewState("ds")
drplist.DataSource = GetData(String.Format("SELECT FiltersId, Filter FROM qFilterFunCat", filId))
drplist.DataTextField = "Filter"
drplist.DataValueField = "FiltersId"
drplist.Visible = "True"
drplist.DataBind()
End If
childrow.Cells(1).Visible = "False"
childrow.Cells(2).Visible = "False"
childrow.Cells(3).Visible = "False"
End If
Next
End If
End If
End If
End Sub
Public Sub btnApplyfilter_Click(sender As Object, e As System.EventArgs)
'I Tried using the button but a postback occurs and the SelectedIndexChanged is reset
Dim SQLCmd As String = "Select * FROM qTestCases "
Dim childrow As GridViewRow
For Each row As GridViewRow In gvFilterParent.Rows
If row.RowType = DataControlRowType.DataRow Then
Dim gvChild As GridView = DirectCast(row.FindControl("gvFilter"), GridView)
Dim filId As String = gvFilterParent.DataKeys(row.RowIndex).Value.ToString()
If gvChild IsNot Nothing Then
For Each childrow In gvChild.Rows
If childrow.RowType = DataControlRowType.DataRow Then
If filId = "1" Then
Dim drplist As DropDownList = DirectCast(childrow.FindControl("ddlFunCat"), DropDownList)
If drplist ISNOT Nothing then
‘do something with drplist – THIS IS ALWAYS NOTHING!!!!!
End if
Else
Dim chk As CheckBox = DirectCast(childrow.FindControl("chkselect"), CheckBox)
If chk.Checked Then
‘Do Something with chk – This works!
End If
End If
End If
Next
End If
End If
Next
End Sub
Public Sub ddlFunCat_SelectedIndexChanged(sender As Object, e As System.EventArgs)
'Do something - The DropDownList doesn't see this
End Sub
Thank you in advance for your help.
<asp:Button ID="btnApplyFilter" runat="server" Text="Apply Filter" ViewStateMode="Enabled"
OnClick="btnApplyfilter_Click" />
<asp:GridView ID="gvFilterParent" runat="server" ShowFooter="True" AutoGenerateColumns="false" CssClass="Grid" ShowHeader="False"
DataKeyNames="filID" AlternatingRowStyle-BackColor="silver"
EnableViewState="true" OnRowDataBound="gvFilterParent_OnRowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img alt = "" style="cursor: pointer" src="images/plus.png" />
<asp:UpdatePanel ID="pnFilter"
ChildrenAsTriggers="true"
EnableViewState="true"
runat="server"
Style="display: none">
<ContentTemplate>
<asp:GridView ID="gvFilter" ShowHeader="False" runat="server"
AlternatingRowStyle-BackColor="silver" EnableViewState="true"
AutoGenerateColumns="false" CssClass = "ChildGrid">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:UpdatePanel ID="ddlUpdate" runat="server" >
<ContentTemplate>
<aspropDownList ID="ddlFunCat" runat="server"
DataTextField="Filter"
DataValueField="FiltersId"
OnSelectedIndexChanged="ddlFunCat_SelectedIndexChanged
visible="false" AutoPostBack="true"
EnableViewState="true"></aspropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlFunCat"
EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkselect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField visible="false" DataField="FiltersId" />
<asp:BoundField ItemStyle-Width="150px" DataField="Filter" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ItemStyle-Width="150px" DataField="filId" visible="false" />
<asp:BoundField ItemStyle-Width="150px" DataField="filname" />
</Columns>
</asp:GridView>
CODE BEHIND
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Using conn As New OleDbConnection(ConfigurationManager.ConnectionStrings("TestCases").ToString())
conn.Open()
'Load Gridview
gvFilterParent.DataSource = GetData("Select * from Filters where filParentID = " & 0)
gvFilterParent.DataBind()
conn.Close()
End Using
End If
End Sub
Private Shared Function GetData(query As String) As DataTable
Dim strConnString As String = ConfigurationManager.ConnectionStrings
("TestCases").ConnectionString
Using con As New OleDbConnection(strConnString)
Using cmd As New OleDbCommand()
cmd.CommandText = query
Using sda As New OleDbDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using ds As New DataSet()
Dim dt As New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Using
End Function
Protected Sub gvFilterParent_OnRowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
'Bind Filters
Dim gvChild As GridView = TryCast(e.Row.FindControl("gvFilter"), GridView)
Dim filId As String = gvFilterParent.DataKeys(e.Row.RowIndex).Value.ToString()
If gvChild IsNot Nothing Then
gvChild.DataSource = GetData(String.Format("Select * from qFilters where filters_1.filId={0}", filId))
gvChild.DataBind()
If filId = "1" Then
For Each childrow As GridViewRow In gvChild.Rows
If childrow.RowType = DataControlRowType.DataRow Then
Dim drplist As DropDownList = DirectCast(childrow.FindControl("ddlFunCat"), DropDownList)
If drplist IsNot Nothing Then
Dim ds As DataSet = New DataSet()
ds = ViewState("ds")
drplist.DataSource = GetData(String.Format("SELECT FiltersId, Filter FROM qFilterFunCat", filId))
drplist.DataTextField = "Filter"
drplist.DataValueField = "FiltersId"
drplist.Visible = "True"
drplist.DataBind()
End If
childrow.Cells(1).Visible = "False"
childrow.Cells(2).Visible = "False"
childrow.Cells(3).Visible = "False"
End If
Next
End If
End If
End If
End Sub
Public Sub btnApplyfilter_Click(sender As Object, e As System.EventArgs)
'I Tried using the button but a postback occurs and the SelectedIndexChanged is reset
Dim SQLCmd As String = "Select * FROM qTestCases "
Dim childrow As GridViewRow
For Each row As GridViewRow In gvFilterParent.Rows
If row.RowType = DataControlRowType.DataRow Then
Dim gvChild As GridView = DirectCast(row.FindControl("gvFilter"), GridView)
Dim filId As String = gvFilterParent.DataKeys(row.RowIndex).Value.ToString()
If gvChild IsNot Nothing Then
For Each childrow In gvChild.Rows
If childrow.RowType = DataControlRowType.DataRow Then
If filId = "1" Then
Dim drplist As DropDownList = DirectCast(childrow.FindControl("ddlFunCat"), DropDownList)
If drplist ISNOT Nothing then
‘do something with drplist – THIS IS ALWAYS NOTHING!!!!!
End if
Else
Dim chk As CheckBox = DirectCast(childrow.FindControl("chkselect"), CheckBox)
If chk.Checked Then
‘Do Something with chk – This works!
End If
End If
End If
Next
End If
End If
Next
End Sub
Public Sub ddlFunCat_SelectedIndexChanged(sender As Object, e As System.EventArgs)
'Do something - The DropDownList doesn't see this
End Sub