Reputation: 7532
I have a gridview in which a cell is populated with a drop down list if certain conditions are met:
protected void viewThemeTypeAssociationsGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.Cells[1].Text == " ")
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[1].Text = "Not Assigned";
DropDownList chooseThemeTypeDropDown = (DropDownList)e.Row.FindControl("chooseThemeTypeDropDown");
chooseThemeTypeDropDown.Visible = true;
}
}
}
Here is a picture:
This is many copies of a drop down list what is made visible in certain rows. How do get access to the selected index for the changed dropdown list and which row it is on?
I was thinking something like this but it doesn't work:
protected void chooseThemeTypeDropDown_OnSelectedIndexChanged(object sender, EventArgs e)
{
DropDownList chooseThemeTypeDropDown = (DropDownList)e.Row.FindControl("chooseThemeTypeDropDown");
}
EDIT: If it helps here is the gridview:
<asp:GridView ID="viewThemeTypeAssociationsGridView" runat="server" AutoGenerateColumns="False"
BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
CellPadding="3" CellSpacing="2" DataSourceID="SqlDataSource6" OnRowDataBound="viewThemeTypeAssociationsGridView_OnRowDataBound">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />
<asp:TemplateField HeaderText="Choose Theme Type">
<ItemTemplate>
<asp:DropDownList ID="chooseThemeTypeDropDown" runat="server" DataTextField="Type" DataValueField="PK_ThemeType" AutoPostBack="true" DataSourceID="SqlDataSource9" CssClass="dropDownList" OnDataBound="chooseThemeTypeDropDown_OnDataBound" Visible="false" OnSelectedIndexChanged="viewProductAssociationsDropDown_OnSelectedIndexChanged">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource9" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="SELECT [Type], [PK_ThemeType] FROM [ThemeType] WHERE [Deleted] = 0 ORDER BY [Type] ASC">
</asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FFF1D4" />
<SortedAscendingHeaderStyle BackColor="#B95C30" />
<SortedDescendingCellStyle BackColor="#F1E5CE" />
<SortedDescendingHeaderStyle BackColor="#93451F" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource6" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="SELECT [Theme].[Name], [ThemeType].[Type] FROM [Theme] LEFT OUTER JOIN [ThemeType] ON [Theme].[ThemeTypeId] = [ThemeType].[PK_ThemeType] JOIN [ProductTheme] ON [ProductTheme].[ThemeId]=[Theme].[PK_Theme] WHERE ProductTheme.ProductID LIKE @productParam AND ProductTheme.ThemeId = Theme.PK_Theme AND COALESCE([THEME].[THEMETYPEID], 'null') LIKE @assignedParam GROUP BY [Theme].[Name], [ThemeType].[Type] ORDER BY CASE WHEN [ThemeType].[Type] IS NULL THEN 0 ELSE 1 END, [Theme].[Name]">
<SelectParameters>
<asp:QueryStringParameter Name="productParam" Type="String" />
<asp:QueryStringParameter Name="assignedParam" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
EDIT 2: Current code
protected void chooseThemeTypeDropDown_OnSelectedIndexChanged(object sender, EventArgs e)
{
DropDownList chooseThemeTypeDropDown = sender as DropDownList;
if (chooseThemeTypeDropDown != null)
{
System.Diagnostics.Debug.WriteLine(chooseThemeTypeDropDown.SelectedItem.Value.ToString());
}
}
Upvotes: 0
Views: 3294
Reputation: 3289
You really need to move the if (e.Row.RowType == DataControlRowType.DataRow)
to the outside of your conditional in OnRowDataBound
.
Now, if you set chooseThemeTypeDropDown.AutoPostBack
to true, and set it to handle the OnSelectedIndexChanged
event, you'll be able to find the correct instance of chooseThemeTypeDropDown
by casting object sender
as a DropDownList
.
To get the row index, that is more difficult. The quickest, but kinda hacky way, is to append the rowindex to each chooseThemeTypeDropDown
ID on the GridView's RowDataBound
event.
chooseThemeTypeDropDown.ID = chooseThemeTypeDropDown.ID + e.Row.RowIndex;
Upvotes: 1
Reputation: 34846
The sender
will give you the control that initiated the event, like this:
protected void chooseThemeTypeDropDown_OnSelectedIndexChanged(object sender, EventArgs e)
{
DropDownList chooseThemeTypeDropDown = sender as DropDownList;
// Check to make sure the drop down exists before we try to work with it
if(chooseThemeTypeDropDown != null)
{
// Put logic here to work with drop down list
}
}
Upvotes: 1