2014-01-10 45 views
0

更新我有一個GridView與下面的屬性Gridview1不會回發

 AllowSorting="true" 
     AutoGenerateColumns="false" 
     BorderStyle="None" 
     CellPadding="3" 
     CssClass="inboundTbl" 
     EnableModelValidation="True" 
     PageSize="2" 
     OnPageIndexChanging="GridView1_PageIndexChanging" 
     OnRowDataBound="GridView1_RowDataBound" 
     OnSorting="GridView1_Sorting" 
     Width="100%" 
     > 

和一些奇怪的原因,它不喜歡更新,當我試圖某人的排序教程。那麼,這次我正在嘗試一個過濾器教程,我對這個問題非常困惑。

這是有問題的templatefield。

  <asp:TemplateField HeaderText="Current Terminal ID" SortExpression="VUCTID"> 
       <HeaderTemplate> 
        Terminal: 
        <asp:DropDownList ID="ddlTerminal" runat="server" 
        AppendDataBoundItems="true" 
        AutoPostBack="true" 
        OnSelectedIndexChanged="TerminalChanged" 
        > 
         <asp:ListItem Text="ALL" Value="ALL"></asp:ListItem> 
        </asp:DropDownList> 
       </HeaderTemplate> 
       <ItemTemplate> 
        <%# Eval("VUCTID") %> 
       </ItemTemplate> 
      </asp:TemplateField> 

我要求幫助,爲什麼它不會更新,他說,在GridView的動態填充,這會導致數據表返回空值。我不確定那是什麼意思,但自那以後我還沒有收到他的消息。我會發布函數進行過濾,以防出現問題。

protected void Page_Load(object sender, EventArgs e) 
    { 
     if(!IsPostBack){ 
      ViewState["Filter"]="ALL"; 
      BindGridView(); 
      lastUpdated.Text = "Load: " + DateTime.Now; 
     } 
    } 

protected void BindGridView() 
{ 
    string query = @"censored"; 
    using (iDB2Connection conn = new iDB2Connection(connect)) 
    { 
     using (iDB2Command cmd = new iDB2Command(query, conn)) 
     { 
      conn.Open(); 
      iDB2DataAdapter da = new iDB2DataAdapter(cmd); 
      DataSet ds = new DataSet(); 
      da.Fill(ds); 
      DataTable dt = ds.Tables[0]; 
      GridView1.DataSource = ds; 
      //iDB2DataReader reader; 
      //reader = cmd.ExecuteReader(); 
      //GridView1.DataSource = reader; 
      GridView1.DataBind(); 
      GridView1.GridLines = GridLines.Horizontal; 
      DropDownList ddlTerminal = (DropDownList)GridView1.HeaderRow.FindControl("ddlTerminal"); 
      this.BindTerminalList(ddlTerminal); 

     } 
    } 
} 

protected void TerminalChanged(object sender, EventArgs e) 
{ 
    DropDownList ddlTerminal = (DropDownList)sender; 
    ViewState["Filter"] = ddlTerminal.SelectedValue; 
    this.BindGridView(); 
} 

private void BindTerminalList(DropDownList ddlTerminal) 
{ 
    string query = "censored"; 
    using (iDB2Connection conn = new iDB2Connection(connect)) 
    { 
     using (iDB2Command cmd = new iDB2Command(query, conn)) 
     { 
      conn.Open(); 
      iDB2DataAdapter da = new iDB2DataAdapter(); 
      ddlTerminal.DataSource = cmd.ExecuteReader(); 
      ddlTerminal.DataTextField = "VUCTID"; 
      ddlTerminal.DataValueField = "VUCTID"; 
      ddlTerminal.DataBind(); 
     } 
     ddlTerminal.Items.FindByValue(ViewState["Filter"].ToString()).Selected = true; 
    } 
} 

編輯:啊,我很抱歉。通過更新我的意思是它不會過濾gridview。模板中的下拉框將從數據庫中填充,一旦點擊,gridview應該根據選定的下拉項目進行更新和過濾。但是當我點擊它時,頁面會加載,但gridview看起來不受影響。

+0

你能說清楚你的意思是「不會更新」嗎?我沒有關注。你是否試圖更新數據庫中的數據,這不起作用?你是否試圖改變客戶端的某些東西,以迴應PostBack,這不起作用? – jadarnel27

+0

如果你在TerminalChanged()中拋出一個斷點,它是否曾經打過它? –

+0

@KevinDeVoe是的。 – Emwat

回答

0

對於初始回發,濾波器應該是所有:

if(!IsPostBack){ 
    ViewState["Filter"]="ALL"; 
    BindGridView(); 
    lastUpdated.Text = "Load: " + DateTime.Now; 
} 

對於其他回傳,過濾值應設置。查詢應該相應地更新。

string query = @"SELECT * FROM Table1 WHERE Terminal = '" + ViewState["Filter"] + "'"; 
+0

謝謝你試圖提供幫助,但我故意遺漏了查詢以避免不必要的閱讀。我碰巧評論了我最後一次嘗試調試問題時的if(!postback),認爲這是一個回發問題。 – Emwat

+0

我在另一個論壇尋求幫助,並回答了他們的問題,雖然他們方面的信息量更大,但與您的答案相似,所以我將其標記爲答案。 – Emwat

0

我想你只是刪除「// if(!IsPostBack){」和「//}」中的註釋,並且您將輸出所需的輸出。

檢查下面的代碼

protected void Page_Load(object sender, EventArgs e) 
{ 
    if(!IsPostBack){ 
    ViewState["Filter"] = "ALL"; 
    BindGridView(); 
    lastUpdated.Text = "Load: " + DateTime.Now; 
    } 
} 
protected void BindGridView() 
{ 
    string query = @""; 
    DataTable dt = new DataTable(); 
    dt.Columns.Add("VUCTID"); 
    if(Convert.ToString(ViewState["Filter"])=="1") 
    { 
     dt.Rows.Add(); 
     dt.Rows[dt.Rows.Count - 1]["VUCTID"] = 1; 
    } 
    if (Convert.ToString(ViewState["Filter"]) == "2") 
    { 
     dt.Rows.Add(); 
     dt.Rows[dt.Rows.Count - 1]["VUCTID"] = 2; 
    } 
    if (Convert.ToString(ViewState["Filter"]) == "3") 
    { 
     dt.Rows.Add(); 
     dt.Rows[dt.Rows.Count - 1]["VUCTID"] = 3; 
    } 
    if (Convert.ToString(ViewState["Filter"]) == "4") 
    { 
     dt.Rows.Add(); 
     dt.Rows[dt.Rows.Count - 1]["VUCTID"] = 4; 
    } 
    if (Convert.ToString(ViewState["Filter"]) == "5") 
    { 
     dt.Rows.Add(); 
     dt.Rows[dt.Rows.Count - 1]["VUCTID"] = 5; 
    } 
    if (Convert.ToString(ViewState["Filter"]) == "ALL") 
    { 
     dt.Rows.Add(); 
     dt.Rows[dt.Rows.Count - 1]["VUCTID"] = 1; 
     dt.Rows.Add(); 
     dt.Rows[dt.Rows.Count - 1]["VUCTID"] = 2; 
     dt.Rows.Add(); 
     dt.Rows[dt.Rows.Count - 1]["VUCTID"] = 3; 
     dt.Rows.Add(); 
     dt.Rows[dt.Rows.Count - 1]["VUCTID"] = 4; 
     dt.Rows.Add(); 
     dt.Rows[dt.Rows.Count - 1]["VUCTID"] = 5; 
    } 
    GridView1.DataSource = dt; 
    GridView1.DataBind(); 
    GridView1.GridLines = GridLines.Horizontal; 
    DropDownList ddlTerminal = (DropDownList)GridView1.HeaderRow.FindControl("ddlTerminal"); 
    this.BindTerminalList(ddlTerminal); 
} 

protected void TerminalChanged(object sender, EventArgs e) 
{ 
    DropDownList ddlTerminal = (DropDownList)sender; 
    ViewState["Filter"] = ddlTerminal.SelectedValue.ToString(); 
    this.BindGridView(); 
} 

private void BindTerminalList(DropDownList ddlTerminal) 
{ 
    string query = ""; 
    DataTable dt = new DataTable(); 
    dt.Columns.Add("VUCTID"); 

    dt.Rows.Add(); 
    dt.Rows[dt.Rows.Count - 1]["VUCTID"] = 1; 
    dt.Rows.Add(); 
    dt.Rows[dt.Rows.Count - 1]["VUCTID"] = 2; 
    dt.Rows.Add(); 
    dt.Rows[dt.Rows.Count - 1]["VUCTID"] = 3; 
    dt.Rows.Add(); 
    dt.Rows[dt.Rows.Count - 1]["VUCTID"] = 4; 
    dt.Rows.Add(); 
    dt.Rows[dt.Rows.Count - 1]["VUCTID"] = 5; 
    ddlTerminal.DataSource = dt; 
    ddlTerminal.DataTextField = "VUCTID"; 
    ddlTerminal.DataValueField = "VUCTID"; 
    ddlTerminal.DataBind(); 
    ddlTerminal.Items.FindByValue(ViewState["Filter"].ToString()).Selected = true; 
} 
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) 
{ 

} 
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 

} 
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) 
{ 

} 
+0

如果你的代碼是我認爲的,我認爲我不能使用它。在每次加載頁面和我的經驗時,行數都會有所不同,但我無法獲得正確的gridview長度。 – Emwat