简体   繁体   English

在GridView单元格中获取所选DropDownList的行

[英]Getting row of selected DropDownList in GridView cells

I have a gridview that is populated with dropdownlists in one of it's cells. 我有一个gridview,其中一个单元格中装有下拉列表。 Here is the gridview code: 这是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="chooseThemeTypeDropDown_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>

Here is the code to populate the cells with the dropdownlist: 这是用下拉列表填充单元格的代码:

protected void viewThemeTypeAssociationsGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (e.Row.Cells[1].Text == "&nbsp;")
        {
            e.Row.Cells[1].Text = "Not Assigned";
            DropDownList chooseThemeTypeDropDown = (DropDownList)e.Row.FindControl("chooseThemeTypeDropDown");
            chooseThemeTypeDropDown.Visible = true;
        }
    }
}

I need access to not only the data key value for the selected dropdownlist but also which row it is on. 我不仅需要访问所选下拉列表的数据键值,还需要访问它所在的行。 Here is what I have so far: 这是我到目前为止的内容:

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

    }
}

THe above method is not doing anything specifically let I am just trying to get the values. 上面的方法没有做任何特别的事情,让我只是试图获取值。 I am getting the data key value but how do I get the row it is on? 我正在获取数据键值,但是如何获取它所在的行?

You can find the grid view row by finding the grandparent ( .Parent.Parent ) of your drop down list, because the drop down list is within a cell of the row, which is a part of the row itself, so you need to go two levels up, like this: 您可以通过找到下拉列表的祖父母( .Parent.Parent )来找到网格视图行,因为下拉列表位于该行的一个单元格内,该单元格是该行本身的一部分,因此您需要转到两个级别,像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM