我在aspx格式视图和下拉列表中获取下拉列表选择值时遇到了一些麻烦:
<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>
我们使用DropDownList
事件向OnLoad
填充值,然后当我们从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中的建议进行操作,但这不起作用。
答案 0 :(得分:0)
您是否尝试将下拉控件的AutoPostBack设置为true,以便导致回发?