GridView实现先执行 后绑定问题

前台页面

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="PModel" onrowcommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="操作">
  
<ItemTemplate>
    <asp:LinkButton ID="lbtn_delete" CommandName="GvDel" runat="server">删除</asp:LinkButton>
  
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

后台页面

public partial class guanli_ListProduct : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}

}

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "GvDel")
{
GridViewRow drv = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;

string orderNum = this.GridView1.Rows[drv.RowIndex].Cells[1].Text.ToString();
SqlParameter[] para = new SqlParameter[] { new SqlParameter("@ordernum",orderNum )};
ExecutionADO exe = new ExecutionADO();
exe.ExecutionParameter("IsDelete", para);

bind();
ClientScript.RegisterStartupScript(this.GetType(), "", "
<script>alert('删除成功!')</script>");
}
}

public void bind()
{
string sqlStr = "SELECT PModel,OrderNum,PTName FROM Product AS P INNER JOIN [ProductType] AS PT ON P.PTId=PT.PTID WHERE P.Isdelete=0";
DataSet myds = ExecutionADO.dataSet(sqlStr);
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "PModel" };
GridView1.DataBind();
}

}

类库代码

///<summary>
/// 存储过程执行sql语句 无返回值
///</summary>
///<param name="sqlPS">存储过程名字</param>
///<param name="para">SqlParameter对象</param>
publicvoid ExecutionParameter(string sqlPS, SqlParameter[] para)
{
using (SqlConnection conn =new SqlConnection(connectionString))
{
using (SqlCommand comm =new SqlCommand(sqlPS, conn))
{
try
{
conn.Open();

comm.CommandType
= CommandType.StoredProcedure;

comm.Parameters.AddRange(para);
comm.ExecuteNonQuery();
}
catch (SqlException e)
{
conn.Close();
thrownew Exception(e.Message);
}
finally
{
conn.Close();
comm.Parameters.Clear();
}

}
}
}

///<summary>
/// 返回一个数据集
///</summary>
///<param name="sqlStr">sql语句</param>
///<returns></returns>
publicstatic DataSet dataSet(string sqlStr)
{
SqlDataAdapter da
=new SqlDataAdapter();
DataSet ds
=new DataSet();
using (SqlConnection connection =new SqlConnection(connectionString))
{
using (SqlCommand cmd =new SqlCommand(sqlStr, connection))
{
try
{

cmd.CommandType
= CommandType.Text;
cmd.CommandText
= sqlStr;
da.SelectCommand
= cmd;
da.Fill(ds);
}
catch (Exception e)
{
thrownew Exception(e.Message);
}
finally
{
connection.Close();
}
}

return ds;
}
}
原文地址:https://www.cnblogs.com/itstone/p/2037674.html