在网格视图中获取选定的索引和下拉菜单行

时间:2013-07-29 20:05:43

标签: c# asp.net

我有一个gridview,如果符合某些条件,则会在下拉列表中填充一个单元格:

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;
        }
    }
}

这是一张图片:

enter image description here

这是下拉列表的许多副本,这些副本在某些行中可见。如何访问已更改的下拉列表的选定索引以及它所在的行?

我在想这样的事情,但它不起作用:

protected void chooseThemeTypeDropDown_OnSelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList chooseThemeTypeDropDown = (DropDownList)e.Row.FindControl("chooseThemeTypeDropDown");
}

编辑:如果它有帮助,那就是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>

编辑2:当前代码

protected void chooseThemeTypeDropDown_OnSelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList chooseThemeTypeDropDown = sender as DropDownList;
    if (chooseThemeTypeDropDown != null)
    {
        System.Diagnostics.Debug.WriteLine(chooseThemeTypeDropDown.SelectedItem.Value.ToString());
    }
}

2 个答案:

答案 0 :(得分:1)

sender将为您提供启动事件的控件,如下所示:

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
    }
}

答案 1 :(得分:1)

您确实需要将if (e.Row.RowType == DataControlRowType.DataRow)移至OnRowDataBound条件的外部。

现在,如果您将chooseThemeTypeDropDown.AutoPostBack设置为true,并将其设置为处理OnSelectedIndexChanged事件,则可以通过强制转换{{1来查找chooseThemeTypeDropDown的正确实例} {} object sender

要获得行索引,那就更难了。最快,但有点hacky的方法是将rowindex附加到GridView的DropDownList事件上的每个chooseThemeTypeDropDown ID。

RowDataBound