zoukankan      html  css  js  c++  java
  • 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;
    }
    }
  • 相关阅读:
    HDU 5918 SequenceI (2016 CCPC长春站 KMP模版变形)
    HDU 4585 Shaolin (set的应用)
    HDU 4329 MAP(stringstream的用法)
    CodeForces 698B Fix a Tree (并查集应用)
    UVALive 2520 Holedox Moving(BFS+状态压缩)
    UVA
    毛竹
    kmp
    博弈论
    最长回文子串
  • 原文地址:https://www.cnblogs.com/itstone/p/2037674.html
Copyright © 2011-2022 走看看