因此,這是發生了什麼事情。我有一個在代碼隱藏創建像這樣的表:針對DDL的Selectedindexchanged沒有觸發(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
}
現在,我明白,如果使用是解決這個問題的有效解決方案(的IsPostBack!)。不幸的是,如果我嘗試這個表格不會被創建。我相信這是因爲我使用我的搜索框的結果來創建表格,但我不確定。 (說實話,我認爲這是問題所在,但我會告訴你其他的)
所以我想添加一行到這個表格使用gridview的頁腳來顯示三個Dropdownlists,其中選擇一個列表項目會更新下一個列表。到目前爲止,我已經能夠得到第一個Dropdownlist正常工作,但是當我選擇一個選項時,什麼也沒有發生。這裏是我的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();
}
爲什麼不顯示相關部分:GridView和表? – 2012-04-01 22:05:36
據此編輯。 – 2012-04-01 22:26:50