【发布时间】:2011-11-06 22:36:10
【问题描述】:
我在获取我们在 aspx Gridview 和其中的下拉列表中拥有的下拉列表选择值时遇到了一些麻烦:
<asp:GridView ID="grid1" runat="server" autogeneratecolumns="true" >
<Columns>
<asp:BoundField HeaderText="something" />
<asp:TemplateField HeaderText="filtras">
<HeaderTemplate>
<asp:DropDownList ID="dropdown1" runat="server"
OnLoad="dropdownLoad"
OnSelectedIndexChanged="updatetable" />
</HeaderTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
我们使用OnLoad 事件为DropDownList 填充值,然后当我们从DropDownList 中选择某些内容时,OnSelectedIndexChange 事件应该允许我们获取选定的值并使用它做我们想做的事情(在这种情况下过滤网格),但 OnSelectedIndexChange 永远不会被执行。
protected void Page_Load(object sender, EventArgs e)
{
//Create Gridview + fill with values
if (IsPostBack)
{
return;
}
ArrayList mycountries = new ArrayList();
mycountries.Add("Norway");
mycountries.Add("Sweden");
mycountries.Add("France");
mycountries.Add("Italy");
mycountries.TrimToSize();
mycountries.Sort();
rb.DataSource = mycountries;
rb.DataBind();
grid1.DataSource = mycountries;
grid1.DataBind();
}
protected void dropdownLoad(object sender, EventArgs e)
{ // fill dropDownList in GridView with data
DropDownList dropdown = sender as DropDownList;
if (dropdown != null)
{
ArrayList mycountries = new ArrayList();
mycountries.Add("Norway");
mycountries.Add("Sweden");
mycountries.Add("France");
mycountries.Add("Italy");
mycountries.TrimToSize();
mycountries.Sort();
dropdown.DataSource = mycountries;
dropdown.DataBind();
TextBox1.Text = dropdown.SelectedIndex.ToString();
}
}
protected void updatetable(object sender, EventArgs e)
{// after dropDownList element was selected change dropdownlist values/or filter the table...
//this part is never executed ! Why?
DropDownList dropdown = sender as DropDownList;
if (dropdown != null)
{
ArrayList mycountries = new ArrayList();
mycountries.Add("UK");
mycountries.Add("USA");
mycountries.Add("Sweden");
mycountries.Add("Hungary");
mycountries.TrimToSize();
mycountries.Sort();
dropdown.DataSource = mycountries;
dropdown.DataBind();
}
}
如何获取DropDownList 的选定值?我的调试器显示 OnSelectedIndexChange 永远不会执行。
我尝试按照这个问题Setting selectedvalue for a dropdownlist in GridView 中的建议进行操作,但没有奏效。
【问题讨论】:
标签: asp.net gridview drop-down-menu