===========================aspx
<asp:GridView runat="server" ID="grvMobile" AutoGenerateColumns="False" Width="100%"
OnRowCancelingEdit="grvMobile_RowCancelingEdit" OnRowDeleting="grvMobile_RowDeleting"
OnRowEditing="grvMobile_RowEditing" OnRowUpdating="grvMobile_RowUpdating" OnRowDataBound="grvMobile_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="姓名">
<ItemStyle Width="100" />
<ItemTemplate>
<asp:Label ID="userName" runat="server" Text='<%#Eval("姓名") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_Name" runat="server" Text='<%#Eval("姓名") %>' Width="90%" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="单位">
<ItemTemplate>
<%#Eval("单位")%></ItemTemplate>
<EditItemTemplate>
<asp:HiddenField ID="Hidd_Comp" runat="server" Value='<%#Eval("单位")%>' />
<asp:DropDownList ID="ddl_Comp" runat="server" Width="80%">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="号码">
<ItemStyle Width="100" />
<ItemTemplate>
<asp:Label ID="phoneNum" runat="server" Text='<%#Eval("手机号")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:HiddenField ID="Hidd_OldPhoneNum" runat="server" Value='<%#Eval("手机号") %>' />
<asp:TextBox ID="txt_PhoneNum" runat="server" Text='<%#Eval("手机号") %>' Width="90%"
onkeyup="this.value=this.value.replace(/\D/g,'')" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="状态">
<ItemStyle Width="60" />
<ItemTemplate>
<%#Eval("状态")%></ItemTemplate>
<EditItemTemplate>
<%#Eval("状态")%></EditItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="操作" ItemStyle-Width="100" ShowEditButton="true" UpdateText="确认"
CancelText="取消" DeleteText="<span onclick="JavaScript:return confirm('是否确定删除该数据?')">删除</span> " />
<asp:CommandField HeaderText="操作" ItemStyle-Width="100" ShowDeleteButton="true" UpdateText="确认"
CancelText="取消" DeleteText="<span onclick="JavaScript:return confirm('是否确定删除该数据?')">删除</span> " />
</Columns>
</asp:GridView>
============================cs
protected void grvMobile_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.grvMobile.EditIndex = -1;
this.grvMobile.DataSource = ViewState["dt"] as DataTable;
this.grvMobile.DataBind();
}
protected void grvMobile_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
this.grvMobile.EditIndex=-1;
MobileBLL bll = new MobileBLL();
DataStruct ds = new DataStruct();
string phoneNum = (this.grvMobile.Rows[e.RowIndex].FindControl("phoneNum") as Label).Text;
ds = bll.DeleteMobile(phoneNum);
if (ds.IsSuc)
{
string userName = (this.grvMobile.Rows[e.RowIndex].FindControl("userName") as Label).Text;
PagerMobile.CurrentPageIndex = 1;
BindMobile();
showMessageBox("用户" + userName + "已成功删除");
}
else
{
showMessageBox("用户" + (this.grvMobile.Rows[e.RowIndex].FindControl("userName") as Label).Text + "删除失败");
}
ds = null;
bll = null;
}
protected void grvMobile_RowEditing(object sender, GridViewEditEventArgs e)
{
this.grvMobile.EditIndex = e.NewEditIndex;
this.grvMobile.DataSource = ViewState["dt"] as DataTable;
this.grvMobile.DataBind();
}
protected void grvMobile_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
MobileBLL bll = new MobileBLL();
DataStruct ds = new DataStruct();
string userName = (this.grvMobile.Rows[e.RowIndex].FindControl("txt_Name") as TextBox).Text;
string uintName = (this.grvMobile.Rows[e.RowIndex].FindControl("ddl_Comp") as DropDownList).SelectedValue;
string phoneNum = (this.grvMobile.Rows[e.RowIndex].FindControl("txt_PhoneNum") as TextBox).Text;
string oldPhoneNum = (this.grvMobile.Rows[e.RowIndex].FindControl("Hidd_OldPhoneNum") as HiddenField).Value.Trim();
ds = bll.ModifyMobile(userName, uintName, phoneNum, oldPhoneNum);
if (ds.IsSuc)
{
e.Cancel = true;
this.grvMobile.EditIndex = -1;
this.grvMobile.DataSource = ViewState["dt"] as DataTable;
this.grvMobile.DataBind();
showMessageBox(ds.Message);
}
else
{
showMessageBox(ds.Message);
}
BindMobile();
}
protected void grvMobile_RowDataBound(object sender, GridViewRowEventArgs e)
{
CommonBLL bll = new CommonBLL();
DataStruct ds = bll.QueryAllMobileUnit();
DropDownList list = e.Row.FindControl("ddl_Comp") as DropDownList;
if (ds.IsSuc && list != null)
{
list.DataSource = ds.Data;
list.DataTextField = "单位";
list.DataValueField = "单位";
list.DataBind();
list.SelectedValue = (e.Row.FindControl("Hidd_Comp") as HiddenField).Value;
}
}