0

我有一个 ASP 页面,其中包含一个与选择查询和网格视图相关的 DDL。在pageLoad我可以选择一个值并毫无问题地填充网格视图之后。

<asp:DropDownList ID="DropDownList3" runat="server" OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged" AutoPostBack="True">
 <asp:ListItem>Select an entry</asp:ListItem>
 <asp:ListItem>Requested</asp:ListItem>
 <asp:ListItem>Requested and Closed</asp:ListItem>
 <asp:ListItem>Ordered</asp:ListItem>
 <asp:ListItem>Working</asp:ListItem> 
 <asp:ListItem>Warranty Parts</asp:ListItem> 
 <asp:ListItem>Warranty Service</asp:ListItem>
 <asp:ListItem>Filled</asp:ListItem>
</asp:DropDownList>

protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
    //string choice = DropDownList3.SelectedItem.Text;
    int index = DropDownList3.SelectedIndex;
    string query;
    switch (index)
    {
        case 0:
            {
                query = "SELECT TOP 100 [last_name], [zpart_used_manual], [zpart_used_SN], [serial_number], [ref_num], [status], [summary], [id], [zpart_used_completed] FROM [VIEW_USS_ICG_Parts_Requested] ORDER BY [last_name]";
                break;
            }
        case 1:
            {
                query = "SELECT TOP 100 [last_name], [zpart_used_manual], [zpart_used_SN], [serial_number], [ref_num], [status], [summary], [id], [zpart_used_completed] FROM [VIEW_USS_ICG_Parts_Requested] WHERE [status]='WAIT-PR' ORDER BY [last_name]";
                break;
            }
        case 2:
            {
                query = "SELECT [last_name], [zpart_used_manual], [zpart_used_SN], [serial_number], [ref_num], [status], [summary], [id], [zpart_used_completed] FROM [VIEW_USS_ICG_Parts_Requested] WHERE ([zpart_used_completed] IS NULL) AND ([status]='WAIT-PR' OR [status]='CLO')  ORDER BY [last_name]";
                break;
            }
        case 3:
            {
                query = "SELECT TOP 100 [last_name], [zpart_used_manual], [zpart_used_SN], [serial_number], [ref_num], [status], [summary], [id], [zpart_used_completed] FROM [VIEW_USS_ICG_Parts_Requested] WHERE [status]='WAIT-PO' ORDER BY [last_name]";
                break;
            }
        case 4:
            {
                query = "SELECT TOP 100 [last_name], [zpart_used_manual], [zpart_used_SN], [serial_number], [ref_num], [status], [summary], [id], [zpart_used_completed] FROM [VIEW_USS_ICG_Parts_Requested] WHERE [status]='WORK' ORDER BY [last_name]";
                break;
            }
        case 5:
            {
                query = "SELECT TOP 100 [last_name], [zpart_used_manual], [zpart_used_SN], [serial_number], [ref_num], [status], [summary], [id], [zpart_used_completed] FROM [VIEW_USS_ICG_Parts_Requested] WHERE [status]='WAIT - WP' OR [status]='WAIT-W' ORDER BY [last_name]";
                break;
            }
        case 6:
            {
                query = "SELECT [last_name], [zpart_used_manual], [zpart_used_SN], [serial_number] , [ref_num], [status], [summary], [id], (SELECT [DDvalue] FROM zpartStatuses WHERE [code] = [zpart_used_completed] ) AS [zpart_used_completed] FROM View_USS_ICG_Parts_Requested INNER JOIN zpartStatuses ON [zpart_used_completed] = zpartStatuses.code WHERE ([status] = 'WAIT - WS')ORDER BY [last_name]";
                break;
            }
        case 7:
            {
                query = "SELECT [last_name], [zpart_used_manual], [zpart_used_SN], [serial_number], [ref_num], [status], [summary], [id], [zpart_used_completed] FROM [VIEW_USS_ICG_Parts_Requested] WHERE [zpart_used_completed] = '1' ORDER BY [last_name]";
                //query = "SELECT TOP 100 [last_name], [zpart_used_manual], [zpart_used_SN], [serial_number], [ref_num], [status], [summary], [id], [zpart_used_completed] FROM [VIEW_USS_ICG_Parts_Requested] ORDER BY [last_name]";
                break;
            }
        default:
            {
                query = "SELECT TOP 100 [last_name], [zpart_used_manual], [zpart_used_SN], [serial_number], [ref_num], [status], [summary], [id], [zpart_used_completed] FROM [VIEW_USS_ICG_Parts_Requested] ORDER BY [last_name]";
                break;
            }

    }
    SqlConnection sqlConnection = new SqlConnection(@"Data Source=XXX.XXX.XXX.XXX;Initial Catalog=mdb;Persist Security Info=True;User ID=user;Password=pass");
    SqlCommand sqlCommand = new SqlCommand(query, sqlConnection);
    sqlConnection.Open();
    SqlDataReader reader = sqlCommand.ExecuteReader();
    GridView1.DataSource = reader;
    GridView1.DataBind();

}

在 gridview 中,我有一个包含另一个 DDL 的项目模板,该 DDL 在该行上触发更新查询。

 <asp:DropDownList ID="DropDownList4" runat="server" DataSourceID="SqlDataSource1"
 DataTextField="DDvalue" DataValueField="code" Height="20px" Width="150px" 
 AutoPostBack="True" CommandName="DD4" OnSelectedIndexChanged = "DDL4_SelectedIndexChanged">
 </asp:DropDownList>        

protected void DDL4_SelectedIndexChanged(object sender, EventArgs e)
{
    string sqlQueryString2 = "UPDATE [act_log] SET [zpart_used_completed]= @status, [zpart_used_status] = @date  WHERE [id] = @id";
    SqlConnection sqlConnectionCmdString2 = new SqlConnection(@"Data Source=XXX.XXX.XXX.XXX;Initial Catalog=mdb;Persist Security Info=True;User ID=user;Password=pass");
    int status;
    GridViewRow gr = (GridViewRow)((DataControlFieldCell)((DropDownList)sender).Parent).Parent;
    DropDownList d4 = (DropDownList)gr.FindControl("DropDownList4");
    status = d4.SelectedIndex;
    string id = GridView1.Rows[gr.RowIndex].Cells[10].Text;             
    sqlConnectionCmdString2.Open();
    SqlCommand sqlQueryCmd2 = new SqlCommand(sqlQueryString2, sqlConnectionCmdString2);
    sqlQueryCmd2.Parameters.Add("@id", id);
    sqlQueryCmd2.Parameters.Add("@status", status);
    sqlQueryCmd2.Parameters.Add("@date", DateTime.Now.ToShortDateString());
    sqlQueryCmd2.ExecuteNonQuery();
    sqlQueryCmd2.Dispose();
    sqlConnectionCmdString2.Close();
    GridView1.DataBind();

}

两个 DDL 都为 AutoPostBack 设置。问题是当OnSelectedIndexChanged事件被触发时,DropDownList4它不会使页面返回到DropDownList3. 相反,页面拉的是:

 <EmptyDataTemplate>
 No records could be retrieved from the database.  We apologize for the invonvenience.
 </EmptyDataTemplate>

如何让页面在每次修改时刷新 gridview?

4

1 回答 1

0

AutoPostBack属性导致回发(显然)。从this.Databind();您的!IsPostBack.

if (!IsPostBack) {
    this.DataBind();
}

变成:

this.DataBind();

更新:

您是否在 GridView 上启用了 ViewState?

更新:

好的,我想现在看到问题了。在处理程序中DropDownList3_SelectedIndexChanged,您设置DataSourceGridView1

GridView1.DataSource = reader;
GridView1.DataBind();

然后,在DDL4_SelectedIndexChanged回发处理程序中,您尝试重新绑定GridView1而不指定另一个数据源。

GridView1.DataBind();

如果GridView1DataSource声明方式设置(在 ASPX 标记中),这将不是问题。但是,根据我的经验,当DataSource属性被动态设置时(在代码隐藏中),它永远不会自动持久化。

解决方法是在重新Select绑定之前重新运行查询并重新设置数据源,或者手动持久化数据源,然后您可以随意重新绑定。

要手动持久化数据源,我通常会使用存储在 ViewState 中的受保护页面属性。

protected SqlDataReader GridView1DataSource {
    get {
        SqlDataReader reader = null;

        if (ViewState("SomeStringUniqueToThePageThatICanUseAsAKeyAndRemainsDescriptive") != null) {
            reader = ViewState("SomeStringUniqueToThePageThatICanUseAsAKeyAndRemainsDescriptive");
        }

        return reader;
    }
    set { ViewState("SomeStringUniqueToThePageThatICanUseAsAKeyAndRemainsDescriptive") = value; }
}

然后

  1. 设置页面属性

    this.GridView1DataSource = reader;

  2. 将数据源设置为页面属性

    GridView1.DataSource = this.GridView1DataSource;

  3. 需要时重新绑定

    GridView1.DataBind();

没有理由不能同时使用这两种技术,但是如果您需要数据源进行实际更新,则需要重新运行查询,然后再将其重新保留在页面属性中。

另一方面,如果您不需要数据源来实际更新,您甚至可能不需要在回发处理程序中重新绑定GridView数据源。DDL4_SelectedIndexChanged

于 2012-10-01T22:08:04.967 回答