熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

GridView實現刪除時彈出確認對話框

2022-06-13   來源: .NET編程 

  效果圖

實現方法
雙擊GridView的OnRowDataBound事件
在後台的GridView_RowDataBound()方法添加代碼最後代碼如下所示

  protected void GridView_RowDataBound(object sender GridViewRowEventArgs e)
    {
        //如果是綁定數據行
        if (eRowRowType == DataControlRowTypeDataRow)
        {
             if (eRowRowState == DataControlRowStateNormal || eRowRowState == DataControlRowStateAlternate)
            {
                ((LinkButton)eRowCells[]Controls[])AttributesAdd(onclick javascript:return confirm(你確認要刪除\ + eRowCells[]Text + \嗎?));
            }
        }

  }

  GridView實現自動編號

  效果圖

  
實現方法

雙擊GridView的OnRowDataBound事件
在後台的GridView_RowDataBound()方法添加代碼最後代碼如下所示
    protected void GridView_RowDataBound(object sender GridViewRowEventArgs e)
    {
        //如果是綁定數據行 //清清月兒
        if (eRowRowType == DataControlRowTypeDataRow)
        {
            ////鼠標經過時行背景色變
            //eRowAttributesAdd(onmouseover thisstylebackgroundColor=#EFFA);
            ////鼠標移出時行背景色變
            //eRowAttributesAdd(onmouseout thisstylebackgroundColor=#FFFFFF);

  ////當有編輯列時避免出錯要加的RowState判斷
            //if (eRowRowState == DataControlRowStateNormal || eRowRowState == DataControlRowStateAlternate)
            //{
            //    ((LinkButton)eRowCells[]Controls[])AttributesAdd(onclick javascript:return confirm(你確認要刪除\ + eRowCells[]Text + \嗎?));
            //}

  }
        if (eRowRowIndex != )
        {
            int id = eRowRowIndex + ;
            eRowCells[]Text = idToString();
        }

  }

  注意這時最好把前台的第一列的表頭該為編號

  因為以前的第一列被吃掉
<asp:GridView ID=GridView runat=server AutoGenerateColumns=False CellPadding= OnRowDeleting=GridView_RowDeleting OnRowEditing=GridView_RowEditing
                        OnRowUpdating=GridView_RowUpdating OnRowCancelingEdit=GridView_RowCancelingEdit BackColor=White BorderColor=#CCCCCC BorderStyle=None BorderWidth=px FontSize=px OnRowDataBound=GridView_RowDataBound>
                        <FooterStyle BackColor=White ForeColor=# />
                        <Columns>
                            <asp:BoundField DataField=身份證號碼 HeaderText=編號 ReadOnly=True />
                            <asp:BoundField DataField=姓名 HeaderText=用戶姓名 />
                            <asp:BoundField DataField=員工性別 HeaderText=性別 />
                            <asp:BoundField DataField=家庭住址 HeaderText=家庭住址 />
                            <asp:CommandField HeaderText=選擇 ShowSelectButton=True />
                            <asp:CommandField HeaderText=編輯 ShowEditButton=True />
                            <asp:CommandField HeaderText=刪除 ShowDeleteButton=True />
                        </Columns>
                        <RowStyle ForeColor=# />
                        <SelectedRowStyle BackColor=# FontBold=True ForeColor=White />
                        <PagerStyle BackColor=White ForeColor=# HorizontalAlign=Left />
                        <HeaderStyle BackColor=# FontBold=True ForeColor=White />
                    </asp:GridView>


From:http://tw.wingwit.com/Article/program/net/201311/12456.html
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.