`

使用强制弹出框(ModalPopupExtender)编辑GridView数据

阅读更多

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ModalPopup.aspx.cs" Inherits="ModalPopup" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>使用强制弹出框编辑GridView数据</title>
<style type="text/css">
/* 强制弹出的可拖曳处 */
.cssDragHandler
{
background-color: #e2ded6;
color: #00a;
text-align: center;
vertical-align: middle;
border: 5px double #fff;
cursor: move;
font-weight: bolder;
font-size: 15pt;
height: 40px;
}
/* 在显示‘强制弹出’时,背景项目所用的样式 */
.modalBackground
{
background-color: #777;
filter: alpha(opacity=75);
opacity: 0.75;
}

/* 强制弹出对话框所用的样式 */
.cssModalPopup
{
background-color: #ffffdd;
border-width: 3px;
border-style: solid;
border-color: #AABCDB;
width: 400px;
}
/* ‘关闭’按钮 */
.close
{
top: 7px;
right: 7px;
background: url(images/close.gif) no-repeat;
width: 34px;
height: 34px;
cursor: hand;
position: absolute;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</ajaxToolkit:ToolkitScriptManager>
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
CellPadding="3" DataKeyNames="供应商编号" DataSourceID="SqlDataSource1" PageSize="6">
<RowStyle ForeColor="#000066" />
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="lbtnSelected" runat="server" CausesValidation="False" CommandName="Select"
OnClick="lbtnSelected_Click" Text="选择"></asp:LinkButton>

</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="供应商" HeaderText="供应商" SortExpression="供应商" />
<asp:BoundField DataField="联系人" HeaderText="联系人" SortExpression="联系人" />
<asp:BoundField DataField="供应商编号" HeaderText="供应商编号" InsertVisible="False" ReadOnly="True"
SortExpression="供应商编号" Visible="False" />
<asp:BoundField DataField="城市" HeaderText="城市" SortExpression="城市" />
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="#F7F7F7" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button ID="Button1" runat="server" Text="无实际作用,仅用作PopupControlID" Style="display: none" />

<ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender1" runat="server" BackgroundCssClass="modalBackground"
Drag="true" PopupControlID="panModalPopup" PopupDragHandleControlID="panDrag"
TargetControlID="Button1" DropShadow="true" >
</ajaxToolkit:ModalPopupExtender>


<asp:Panel ID="panModalPopup" runat="server" CssClass="cssModalPopup" Style="display: none">

<%-- 强制弹出可拖曳的地方 --%>
<asp:Panel ID="panDrag" runat="server" CssClass="cssDragHandler">
详细数据
<asp:LinkButton ID="lnkbtnClose" runat="server" CssClass="close" ToolTip="关闭" OnClientClick="$find('ModalPopupExtender1').hide(); return false;" />
</asp:Panel>


<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="400px" AutoGenerateRows="False"
BorderColor="#BFBFBF" BorderStyle="Solid" BorderWidth="1px" CellPadding="4" DataKeyNames="供应商编号"
DataSourceID="SqlDataSource2" ForeColor="#333333" GridLines="None" OnItemCommand="DetailsView1_ItemCommand"
OnItemUpdated="DetailsView1_ItemUpdated">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<CommandRowStyle BackColor="#D1DDF1" Font-Bold="True" />
<RowStyle BackColor="#EFF3FB" />
<FieldHeaderStyle BackColor="#DEE8F5" Font-Bold="True" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<Fields>
<asp:BoundField DataField="供应商编号" HeaderText="供应商编号" InsertVisible="False" ReadOnly="True"
SortExpression="供应商编号" />
<asp:BoundField DataField="供应商" HeaderText="供应商" SortExpression="供应商" />
<asp:BoundField DataField="联系人" HeaderText="联系人" SortExpression="联系人" />
<asp:BoundField DataField="联系人职务" HeaderText="联系人职务" SortExpression="联系人职务" />
<asp:BoundField DataField="地址" HeaderText="地址" SortExpression="地址" />
<asp:BoundField DataField="城市" HeaderText="城市" SortExpression="城市" />
<asp:BoundField DataField="区" HeaderText="区" SortExpression="区" />
<asp:BoundField DataField="邮政编码" HeaderText="邮政编码" SortExpression="邮政编码" />
<asp:BoundField DataField="国家地区" HeaderText="国家地区" SortExpression="国家地区" />
<asp:BoundField DataField="电话" HeaderText="电话" SortExpression="电话" />
<asp:BoundField DataField="传真" HeaderText="传真" SortExpression="传真" />
<asp:BoundField DataField="公司网站" HeaderText="公司网站" SortExpression="公司网站" />
<asp:CommandField ShowEditButton="True" />
</Fields>
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:DetailsView>
<asp:Label ID="lblMsg" runat="server" Text=""></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
<!--GridView1数据源-->
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:chtNorthwind %>"
SelectCommand="SELECT [供应商], [联系人], [供应商编号], [城市] FROM [供应商]"></asp:SqlDataSource>
<!--DetailsView1数据源-->
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:chtNorthwind %>"
SelectCommand="SELECT * FROM [供应商] WHERE ([供应商编号] = @供应商编号)" UpdateCommand="UPDATE [供应商] SET [供应商] = @供应商, [联系人] = @联系人, [联系人职务] = @联系人职务, [地址] = @地址, [城市] = @城市, [区] = @区, [邮政编码] = @邮政编码, [国家地区] = @国家地区, [电话] = @电话, [传真] = @传真, [公司网站] = @公司网站 WHERE [供应商编号] = @供应商编号">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="供应商编号" PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="供应商" Type="String" />
<asp:Parameter Name="联系人" Type="String" />
<asp:Parameter Name="联系人职务" Type="String" />
<asp:Parameter Name="地址" Type="String" />
<asp:Parameter Name="城市" Type="String" />
<asp:Parameter Name="区" Type="String" />
<asp:Parameter Name="邮政编码" Type="String" />
<asp:Parameter Name="国家地区" Type="String" />
<asp:Parameter Name="电话" Type="String" />
<asp:Parameter Name="传真" Type="String" />
<asp:Parameter Name="公司网站" Type="String" />
<asp:Parameter Name="供应商编号" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>

*******************CS CODE*********************************
protected void lbtnSelected_Click(object sender, EventArgs e)
{//GridView选择事件
lblMsg.Text = "";
ModalPopupExtender1.Show();
}
protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
{
if (e.Exception!=null || e.AffectedRows<=0)
{
lblMsg.Text = "更新失败,请检查各项是否正确输入!";
}
else
{
lblMsg.Text = "更新成功!";
}
GridView1.DataBind();
//UpdatePanel2.Update();
}
protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
{
//  在按下‘编辑’、‘更新’与‘取消’按钮时,清除原先的信息。
lblMsg.Text = "";
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics