所以这就是发生了什么。我有一个在代码隐藏中创建的表,如下所示:
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的页脚向此表添加一行以显示三个下拉列表,其中一个列表项的选择更新下一个列表。到目前为止,我已经能够让第一个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();
}
答案 0 :(得分:0)
如果我理解正确,你就是动态创建一个表。这必须最迟在Page_load中的每个回发中完成。
所以你只需要在每次回发时绑定GridView if(!IsPostBack)
但表格,从而将两个指点彼此分开。
在您编辑问题后,我知道您的意思。因此,您需要根据用户输入创建GridView
的DataSource。
您需要DataBind
仅来自Page_Load
if(!IsPostBack)
的{{1}}(使用默认数据)。然后,您可以处理SearchBox
TextChanged
事件(或应用搜索的按钮)。从那里你需要DataBind
GridView相应的搜索参数。