为什么点击按钮后GridView会消失?
本文关键字:消失 GridView 按钮 为什么 | 更新日期: 2023-09-27 18:12:05
<asp:UpdatePanel ID="upExec" runat="server" ClientIDMode="Static" UpdateMode="Conditional">
<ContentTemplate>
<button type="button" runat="server" id="btnExec" onserverclick="btnExec_Click" class="btnAll btnExec">Execute SQL Job</button>
<asp:Label ID="lblEMsg" runat="server" ClientIDMode="Static" Text=""></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="upGV" runat="server" ClientIDMode="Static" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView EmptyDataText="No Provider Exists" ID="gvData" runat="server" ClientIDMode="Static" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderStyle-Width="5%" DataField="Name" HeaderText="Name" />
<asp:BoundField HeaderStyle-Width="5%" DataField="ID" HeaderText="ID" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
if (!Page.IsPostBack)
{
ViewState["sortOrder"] = "Asc";
ViewState["sortExp"] = "Due Date";
ShowGridView("Name", "Asc");
}
public void ShowGridView(string sortExp, string sortDir)
{
using (SqlConnection sc = new SqlConnection(gloString))
{
try
{
sc.Open();
SqlDataAdapter sda = new SqlDataAdapter(strQuery, sc);
DataSet ds = new DataSet();
sda.Fill(ds);
DataView dv = new DataView();
dv = ds.Tables[0].DefaultView;
gvData.DataSource = dv;
gvData.DataBind();
}
catch (SqlException)
{
}
finally
{
sc.Close();
}
}
upGV.Update();
}
public void btnExec_Click(object sender, EventArgs e)
{
using (SqlConnection sc = new SqlConnection(gloString))
{
try
{
sc.Open();
using (SqlCommand scd = new SqlCommand())
{
scd.CommandText = "MySP";
scd.CommandType = CommandType.StoredProcedure;
scd.Connection = sc;
scd.ExecuteNonQuery();
}
}
catch (SqlException)
{
}
finally
{
sc.Close();
}
}
using (SqlConnection sc = new SqlConnection(floString))
{
try
{
sc.Open();
SqlCommand scd = new SqlCommand();
scd.CommandType = CommandType.StoredProcedure;
scd.Connection = sc;
scd.CommandText = "msdb.dbo.sp_start_job";
scd.Parameters.AddWithValue("@job_name", "JobName");
using (scd)
{
scd.ExecuteNonQuery();
ShowGridView("Name", "Asc");
}
}
catch (SqlException)
{
}
finally
{
sc.Close();
}
}
upExec.Update();
}
当页面首次加载时,我可以看到GridView。当我单击btnExec
时,GridView消失,并显示"No Provider Exists"。当我检查页面的来源时,数据仍然在那里。
如何解决这个问题,所以当按钮被单击时,它执行存储过程并重新加载GridView与新的数据
看起来您应该将btnExec_Click()方法中对ShowGridView()的调用移到外部"using"语句之外,就在调用upExec.Update()之前。
在我的例子中,我必须将这个属性添加到包装GridView的面板中,以保持它的可见性和工作,每次我点击一行来选择它时,它都会消失:
<asp:Panel EnableViewState="True">