2012-04-01 57 views
0

因此,這是發生了什麼事情。我有一個在代碼隱藏創建像這樣的表:針對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(); 
} 
+0

爲什麼不顯示相關部分:GridView和表? – 2012-04-01 22:05:36

+0

據此編輯。 – 2012-04-01 22:26:50

回答

0

如果我理解正確的話,你要創建一個表動態。必須在每次最後一次在Page_load上發送回傳。

因此,您只需綁定GridView if(!IsPostBack),但每次回發時都會將表格彼此分開。

當你編輯你的問題後,我知道你的意思。因此,您需要根據用戶輸入創建GridView的數據源。

您需要DataBind您的GridView從Page_Load只有if(!IsPostBack)(與默認數據)。然後,您可以處理SearchBoxTextChanged事件(或應用搜索的按鈕)。從那裏你需要DataBind GridView相應的搜索參數。

+0

所以我需要首先提供一些虛擬默認數據的GridView?然後用另一種方法調用GetData()? – 2012-04-01 22:40:58

+0

@vegetas_angel:如果你不想要,你根本不需要加載它。但是,包含頁腳的網格未顯示。我不確定這是否可以。 – 2012-04-01 22:43:16

+0

帶有頁腳的網格是唯一以這種方式完成的,因此需要顯示它。 我嘗試過使用dummy數據並使用textchanged。它正確加載了表格,但是當我嘗試點擊'InsertOP'按鈕(這應該使頁腳可見並且使用GridView8.DataBind()綁定數據表格),表格似乎消失了。任何想法爲什麼? – 2012-04-01 23:14:35

相關問題