我有一个 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?