![](/img/trans.png)
[英]DropDownList SelectedIndexChanged not firing (AutoPostBack=“true”)
[英]Selectedindexchanged for DDL is not firing (AutoPostBack = “true”)
所以这是怎么回事。 我有一个在代码隐藏中创建的表,如下所示:
protected void Page_Load(object sender, EventArgs e)
{
RetrievedValue = SearchBox.Text;
GetData(RetrievedValue); //Creates a Datatable, populates it and binds it to a GridView
}
现在,我知道使用if(!IsPostBack)是解决此问题的有效方法。 不幸的是,如果我尝试此操作,则不会创建表。 我相信这是因为我正在使用搜索框的结果来创建表,但是我不确定。 (说实话,我认为这就是问题所在,但是我会其余部分告诉您)
因此,我想使用gridview的页脚在此表中添加一行以显示三个Dropdownlists,其中一个列表项的选择将更新下一个列表。 到目前为止,我已经能够使第一个Dropdownlist工作正常,但是,当我选择一个选项时,什么也没发生。 这是我的下拉列表代码:
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems='True'
DataSourceID="SqlDataSource1" AutoPostBack = "true" EnableViewState = "true"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
DataTextField="field1" DataValueField="field1">
<asp:ListItem Value="-2" Text="--choose--" Selected="True" />
<asp:ListItem Value="-1" Text="ALL" />
</asp:DropDownList>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostbackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT DISTINCT field1 FROM table">
</asp:SqlDataSource>
这是我的C#方法:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("SelectedIndexChanged"); //not being called
}
对于这个问题的任何帮助将不胜感激! 我知道这与Page_Load有关,但是我不确定如何以其他方式进行!
谢谢堆:)
这是GridView代码:
<asp:GridView ID="GridView8" runat="server" AllowPaging="True" AutoGenerateColumns="false"
AllowSorting="True" CellPadding="4" OnRowDataBound="GridView8_RowDataBound"
ForeColor="#333333" GridLines="None" PageSize="20" Width="100%" ShowFooter="false" >
<Columns>
<asp:TemplateField HeaderText=" ">
<FooterTemplate>
<asp:LinkButton id="Insert" runat="server" CausesValidation="True" Text="Insert" OnClick="Insert_Click" /><br />
<asp:LinkButton id="Cancel" runat="server" CausesValidation="True" Text="Cancel" OnClick="Cancel_Click" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="FieldName">
<ItemTemplate>
<%# Eval("FieldName")%>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems='True' DataSourceID="SqlDataSource1" AutoPostBack = "true"
EnableViewState = "true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
DataTextField="field1" DataValueField="field1">
<asp:ListItem Value="-2" Text="--choose--" Selected="True" />
<asp:ListItem Value="-1" Text="ALL" />
</asp:DropDownList>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostbackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT DISTINCT field1 FROM table">
</asp:SqlDataSource>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="FieldName2">
<ItemTemplate>
<%# Eval("FieldName2")%>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" AppendDataBoundItems='True' Enabled="false">
<asp:ListItem Value="-2" Text="--choose--" />
<asp:ListItem Value="-1" Text="ALL" />
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="FieldName3">
<ItemTemplate>
<%# Eval("FieldName3") %>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="DropDownList3" runat="server" AppendDataBoundItems='True' Enabled="false">
<asp:ListItem Value="-2" Text="--choose--" />
<asp:ListItem Value="-1" Text="ALL" />
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
和getData方法:
protected void GetData(String str)
{
DataTable table = new DataTable();
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString))
{
string sql = "SELECT FieldName3, FieldName2, FieldName1 FROM table t WHERE field5 LIKE @field5";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.Add(new SqlParameter("@field5", str));
using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
{
ad.Fill(table);
}
}
}
/*Populate table with adjusted data*/
DataTable table2 = new DataTable();
if (str.Length != 0 && table.Rows.Count != 0)
{
String NameCodes1 = table.Rows[0]["FieldName3"].ToString();
String NameCodes2 = table.Rows[0]["FieldName2"].ToString();
String NameCodes3 = table.Rows[0]["FieldName1"].ToString();
if (String.CompareOrdinal(NameCodes1, "%") == 0) //if ALL
{
DataColumn dcol = new DataColumn("FieldName3", typeof(System.String));
table2.Columns.Add(dcol);
DataColumn dcol2 = new DataColumn("FieldName2", typeof(System.String));
table2.Columns.Add(dcol2);
DataColumn dcol3 = new DataColumn("FieldName1", typeof(System.String));
table2.Columns.Add(dcol3);
CheckPHO(NameCodes2, NameCodes3, table2, 0); //adds rows depending on whether there are multiple codes in the field
}
else //not ALL
{
pracCodes = Regex.Replace(NameCodes1, ",", "','");
pracCodes = "'" + NameCodes1 + "'";
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString))
{
string sql = "SELECT name AS FieldName3, t2.[PHO_Name] AS FieldName2, t1.field1 AS FieldName1 FROM table3 t3 " +
"LEFT JOIN table2 t2 ON t3.PHO = t2.PHO_ID " +
"LEFT JOIN table1 t1 ON t2.DHB_ID = t1.DHB_ID " +
"WHERE IDPractice IN (" + NameCodes1 + ")";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
{
ad.Fill(table2);
}
}
}
}
}
GridView8.DataSource = table2;
GridView8.DataBind();
}
如果我正确地理解了您,则说明您正在动态创建表。
最迟必须在Page_load中的每个回发上执行此操作。
因此,您只需要绑定GridView
if(!IsPostBack)
但绑定每个回发中的表,从而将两个方法分开。
在您编辑完问题后,我知道您的意思了。 因此,您需要根据用户输入创建GridView
的数据源。
仅if(!IsPostBack)
(带有默认数据)时,才需要从Page_Load
DataBind
GridView。 然后,您可以处理SearchBox
TextChanged
事件(或应用搜索的按钮)。 从那里,您需要使用搜索参数对GridView进行数据DataBind
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.