C# 在网格视图中获取下拉菜单的选定索引和行
我有一个gridview,其中如果满足某些条件,单元格将填充一个下拉列表:C# 在网格视图中获取下拉菜单的选定索引和行,c#,asp.net,C#,Asp.net,我有一个gridview,其中如果满足某些条件,单元格将填充一个下拉列表: protected void viewThemeTypeAssociationsGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e) { if (e.Row.Cells[1].Text == " ") { if (e.Row.RowType == DataControlRowType.DataRow)
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;
}
}
}
这是一张照片:
这是下拉列表的多个副本,在某些行中可见。如何访问已更改下拉列表的选定索引及其所在的行
我在想这样的事情,但不起作用:
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>
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
}
}
您确实需要将if(e.Row.RowType==DataControlRowType.DataRow)移动到条件in
OnRowDataBound
的外部
现在,如果将chooseThemeTypeDropDown.AutoPostBack
设置为true,并将其设置为处理onselectedexchanged
事件,则可以通过将对象发送器
转换为DropDownList
来找到chooseThemeTypeDropDown
的正确实例
要获取行索引,这更困难。最快但有点老套的方法是将行索引附加到GridView的RowDataBound
事件上的每个chooseThemeTypeDropDown
ID
choosetheMetypropdown.ID=选择SetHementPedPropDown.ID+e.Row.RowIndex代码>我已将自动回邮设置为true,并且OnSelectedIndex已更改。System.Diagnostics.Debug.WriteLine(选择Sethemetypedropdown.SelectedItem.Value.ToString());结果是没有输出,当我更改索引时,它会在回发时重新更改。我做错了什么?您是将发送方强制转换为DDL,还是仍然以问题代码中的错误方式进行转换?在该行上放置断点,并在运行时检查SelectedItem的值。我希望您仍然在这里回答问题。我正在使用chooseThemeTypeDropDown.ID=chooseThemeTypeDropDown.ID+e.Row.RowIndex;将行索引附加到ID,但在调用它时它不会显示:System.Diagnostics.Debug.WriteLine(chooseThemeTypeDropDown.ID.ToString());结果:chooseThemeTypeDropDown,没有附加值。嗯,我不确定这是否有效。这一点甚至更难理解,但是如果您今天需要让它工作,您可以使用CssClass
来存储rowindex属性。这不是一个我会觉得舒服的投入生产,但它会工作。
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
}
}