• 在GridView中自定按钮,实现删除和更新


    用的都是笨办法,看了二个小时看搞明白
    本来算删除和更新都用RowCommand来实现的,后来发现,在RowCommand中无法取到自定的输入框中的值,只得把更新换到RowUpdating中去实现了,但RowUpdating中又没有CommandArgument属性,只好放一个HiddenField来做参数存储。实属无奈,不知道好用的方法应该是怎么样,有没有高手可以指点指点
    在GridView中操作数据方法:
    1.删除:

         在模版中建一个Button,设置其CommandType="del"、CommandArgument="<%#Eval("ID")%>"
         在GridView的RowCommand事件中判断CommandType,以CommandArgument为参数删除数据

    2.更新:

         在模版中建一个Button,设置其CommandType="update"
         建一个HiddenField,设置为Value="<%#Eval("ID")%>"
         在GridView的Rowupdating事件中,先用e.RowIndex找到当前行的索引,然后用FindControl取到值,更新

    3.更新2:

         另一个方法:在“编辑”按钮的CommandArgument中写<%# ((GridViewRow) Container).RowIndex %>
         在GridView的RowCommand事件中,就可以使用CommandArgument来获取当前行的索引,然后处理。
         以下代码中还没有使用这种方法来实现

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        
    {
            
    if (e.CommandName == "del")
            
    {
                
    int index = Convert.ToInt32(e.CommandArgument);
                
    string sqlcmd = "delete from Admin where id =" + index;
                userSql.db.ExecuteNonQuery(sqlcmd);
                DataBinded();
            }

        }

        
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        
    {
            
    int index2 = e.RowIndex;
            
    string txtName = ((TextBox)this.GridView1.Rows[index2].Cells[2].FindControl("txtAdminname")).Text;
            
    string txtPass = ((TextBox)this.GridView1.Rows[index2].Cells[2].FindControl("txtAdminPassword")).Text;
            
    int id = Convert.ToInt32(((HiddenField)this.GridView1.Rows[index2].Cells[2].FindControl("HiddenID")).Value);
            
    string sqlcmd = "update [Admin] set [Adminname]=@Adminname,[AdminPassword]=@AdminPassowrd where [id]=@id";
            SqlParameter[] param 
    = {
                
    new SqlParameter("@Adminname",txtName),
                
    new SqlParameter("@AdminPassowrd",txtPass),
                
    new SqlParameter("@id",id),
            }
    ;
            userSql.db.ExecuteNonQuery(sqlcmd, param);
            DataBinded();
        }
  • 相关阅读:
    Oracle GoldenGate
    net core 2.0 web api + Identity Server 4 + angular 5
    NET Core Web API下事件驱动型架构CQRS架构中聚合与聚合根的实现
    Angular 5项目
    Scala语言
    jQuery 仿百度输入标签插件
    vs2010 mvc3安装时报错
    快递查询SDK
    天气查询SDK
    OAthe2 Login use OkHttpClient and OAuth2RestTemplate
  • 原文地址:https://www.cnblogs.com/yeagen/p/1331475.html
Copyright © 2020-2023  润新知