Custom GridView delete button

后端 未结 3 887
挽巷
挽巷 2021-01-25 07:47

How can I customize automatically generated command button, e.g. Delete?

I want to add a client confirmation on deleting and in the same moment I want this

3条回答
  •  被撕碎了的回忆
    2021-01-25 08:12

    You can probably do it by implementing the PreRender event for the grid.

    Here is some basic psuedo code:

    protected void yourGrid_PreRender(object sender, EventArgs e)
    {
        GridView grd = (GridView)(sender);
    
        // iterate through all your rows and look for the button
        // make sure to add code to verify your rows, columns, and control bounds are valid
        for (int rowIndex = 0; rowIndex < grd.Rows.Count; rowIndex++)
        {
            LinkButton btn = grd.Rows[rowIndex].Cells[deleteButtonColumnIndex].Controls[0] as LinkButton;
    
            // Here you have access to the button so change it to do what you need.
            btn.OnClientClick = string.Format("return confirm('{0}?')", btn.Text);
        }
    }
    

    Also if you want it baked in you will probably need to extend the GridView and implement your own code. See the following thread:

    http://forums.asp.net/p/1396268/3011988.aspx#3011988

提交回复
热议问题