电脑故障

位置:IT落伍者 >> 电脑故障 >> 浏览文章

GridView实现删除时弹出确认对话框


发布日期:2021/11/28
 

效果图

实现方法

双击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>

上一篇:Windows Communication Foundation介绍(一)(1)

下一篇:WPF的三维变换应用