【原创】 Ajax之ModalPopup编程实例

来源:互联网 发布:郑州办公软件培训班 编辑:程序博客网 时间:2024/04/30 10:59

 

以下是我最近做项目的时候遇到了关于ModalPopup的使用实例,解决了一些技术难题,详细见源代码
解决了打开网页的时候ModalPopup闪烁出现的现象,以及TargetControlID后台代码不被执行的问题。

1.jpg

2.jpg 

前台代码:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <style type="text/css">        
            .PanelBorder {
                        border: 3px solid #0000CC;
                }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <br />
        <ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="Button1"
            PopupControlID="pnlDepart" CancelControlID="imgClose">
        </ajaxToolkit:ModalPopupExtender>
        <center>
            <div>
                <div>
                    <div style="width: 680px; height: 30px; background-color: #507CD1; padding-top: 8px;
                        padding-left: 20px; text-align: left">
                        <strong><font color="white">查询条件</font></strong></div>
                    <div style="width: 700px; height: auto">
                        <table>
                            <tr>
                                <td style="width: 80px; height: 26px;" align="right">
                                    科室编码</td>
                                <td style="width: 100px; height: 26px;" align="left">
                                    <asp:TextBox ID="txtDepartIDFind" runat="server" Width="80px"></asp:TextBox></td>
                                <td style="width: 80px; height: 26px;" align="right">
                                    科室名称</td>
                                <td style="width: 120px; height: 26px;" align="left">
                                    <asp:TextBox ID="txtDepartNameFind" runat="server" Width="100px"></asp:TextBox></td>
                                <td style="width: 320px; height: 26px;" align="center">
                                    <asp:Button ID="btnAdd" runat="server" Text="添加科室" OnClick="btnAdd_Click" /><asp:Button
                                        ID="btnQuery" runat="server" Text="查询" OnClick="btnQuery_Click" />
                                    <asp:Button ID="Button1" runat="server" Text="Button" Style="display: none" /></td>
                            </tr>
                        </table>
                    </div>
                </div>
                <div>
                    <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                        <ContentTemplate>
                            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
                                ForeColor="#333333" GridLines="None" Width="700px">
                                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                                <RowStyle BackColor="#EFF3FB" />
                                <Columns>
                                    <asp:BoundField DataField="DepartID" HeaderText="科室编码">
                                        <HeaderStyle Width="80px" />
                                    </asp:BoundField>
                                    <asp:BoundField DataField="DepartName" HeaderText="科室名称">
                                        <HeaderStyle HorizontalAlign="Left" Width="200px" />
                                        <ItemStyle HorizontalAlign="Left" />
                                    </asp:BoundField>
                                    <asp:BoundField DataField="SortID" HeaderText="排序编码">
                                        <HeaderStyle Width="80px" />
                                    </asp:BoundField>
                                    <asp:BoundField DataField="State" HeaderText="状态">
                                        <HeaderStyle Width="80px" />
                                    </asp:BoundField>
                                    <asp:TemplateField>
                                        <ItemTemplate>
                                            <asp:Button ID="btnModify" runat="server" Text="修改" OnClick="btnModify_Click" />
                                        </ItemTemplate>
                                        <HeaderStyle Width="80px" />
                                    </asp:TemplateField>
                                    <asp:CommandField ShowDeleteButton="True">
                                        <HeaderStyle Width="80px" />
                                    </asp:CommandField>
                                </Columns>
                                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                                <EditRowStyle BackColor="#2461BF" />
                                <AlternatingRowStyle BackColor="White" />
                            </asp:GridView>
                        </ContentTemplate>
                    </asp:UpdatePanel>
                     
                    <asp:Panel ID="pnlDepart" runat="server" Height="150px" Width="400px" CssClass="PanelBorder"
                        Style="display: none" BackColor="White">
                        <div style="width: 100%; height: 30px; background-image: url(Images/Title.jpg)">
                            <div style="float: left; padding-top: 5px; padding-left: 10px">
                                <asp:UpdatePanel ID="UpdatePanel3" runat="server">
                                    <ContentTemplate>
                                        <asp:Label ID="lblCaption" runat="server" Text="Label" Font-Bold="True" ForeColor="White"></asp:Label>
                                    </ContentTemplate>
                                </asp:UpdatePanel>
                            </div>
                            <div style="float: right; padding-top: 2px; padding-right: 3px">
                                <asp:ImageButton ID="imgClose" runat="server" ImageUrl="~/Images/Close.JPG" /></div>
                        </div>
                        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                            <ContentTemplate>
                                <div style="width: 400px; height: 120px; vertical-align: middle;">
                                    <br />
                                    <table>
                                        <tr>
                                            <td>
                                                科室编码</td>
                                            <td>
                                                <asp:TextBox ID="txtDepartID" runat="server" Width="100px"></asp:TextBox></td>
                                            <td>
                                                科室名称</td>
                                            <td>
                                                <asp:TextBox ID="txtDepartName" runat="server" Width="100px"></asp:TextBox></td>
                                        </tr>
                                        <tr>
                                            <td>
                                                排序编码</td>
                                            <td>
                                                <asp:TextBox ID="txtSortID" runat="server" Width="100px"></asp:TextBox></td>
                                            <td>
                                                状态</td>
                                            <td>
                                                <asp:DropDownList ID="ddlState" runat="server" Width="105px">
                                                    <asp:ListItem>使用</asp:ListItem>
                                                    <asp:ListItem>关闭</asp:ListItem>
                                                </asp:DropDownList></td>
                                        </tr>
                                        <tr>
                                            <td align="center" colspan="4">
                                                <asp:Button ID="btnSave" runat="server" Text="保存" OnClick="btnSave_Click" /></td>
                                        </tr>
                                    </table>
                                </div>
                            </ContentTemplate>
                        </asp:UpdatePanel>
                    </asp:Panel>
                       
                </div>
            </div>
        </center>
    </form>
</body>
</html>


后台代码:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) BindData();
    }

    //GridView绑定
    private void BindData()
    {
        this.GridView1.DataSource = RunQuery("Select * From DepartInfo Where 1=1 "+GetCon()+" Order by SortID").DefaultView;
        this.GridView1.DataBind();
    }

    private string GetCon()
    {
        string sCon = "";
        if (!this.txtDepartIDFind.Text.Trim().Equals(""))
        {
            sCon = " And DepartID Like '%" + this.txtDepartIDFind.Text + "%' ";
        }

        if (!this.txtDepartNameFind.Text.Trim().Equals(""))
        {
            sCon = " And DepartName Like '%" + this.txtDepartNameFind.Text + "%' ";
        }

        return sCon;
    }


    //查询方法
    private DataTable RunQuery(string sSQL)
    {
        SqlConnection con = new SqlConnection("server=.;uid=sa; pwd=; database=pubs");
        SqlDataAdapter da = new SqlDataAdapter(sSQL, con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        if (ds.Tables.Count > 0)
        {
            return ds.Tables[0];
        }
        else
        {
            return null;
        }
    }

    //执行SQL语句
    private void RunExec(string sSQL)
    {
        SqlConnection con = new SqlConnection("server=.;uid=sa; pwd=; database=pubs");
        con.Open();
        SqlCommand cmd = con.CreateCommand();
        cmd.CommandText = sSQL;
        cmd.CommandType = CommandType.Text;
        cmd.ExecuteNonQuery();
        con.Close();
    }
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        BindData();        
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (this.txtDepartID.ReadOnly)
        {
            string sSQL = "UpDate DepartInfo Set DepartName='"+this.txtDepartName.Text+"', SortID="+
                           this.txtSortID.Text + ",State='" + this.ddlState.SelectedValue +
                           "' Where DepartID='"+this.txtDepartID.Text+"'";
            RunExec(sSQL);

            BindData();
            this.ModalPopupExtender1.Hide();
        }
        else
        {
            string sSQL = "Insert DepartInfo(DepartID, DepartName, SortID, State) " +
                        "Values('" + this.txtDepartID.Text + "','" + this.txtDepartName.Text + "'," +
                        this.txtSortID.Text + ",'" + this.ddlState.SelectedValue + "')";
            RunExec(sSQL);

            BindData();

            this.txtDepartID.Text = "";
            this.txtDepartName.Text = "";
            this.txtSortID.Text = "";
            this.ddlState.SelectedIndex = 0;
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        this.lblCaption.Text = "添加科室";
        this.txtDepartID.Text = "";
        this.txtDepartName.Text = "";
        this.txtSortID.Text = "";
        this.ddlState.SelectedIndex = 0;
        this.txtDepartID.ReadOnly = false;

        this.ModalPopupExtender1.Show();
    }
    protected void btnModify_Click(object sender, EventArgs e)
    {
        this.lblCaption.Text = "修改科室";
        GridViewRow gvr = ((Button)sender).Parent.Parent as GridViewRow;

        this.txtDepartID.Text = gvr.Cells[0].Text;
        this.txtDepartName.Text = gvr.Cells[1].Text;
        this.txtSortID.Text = gvr.Cells[2].Text;
        this.ddlState.SelectedIndex = this.ddlState.Items.IndexOf(this.ddlState.Items.FindByText(gvr.Cells[3].Text));
        this.txtDepartID.ReadOnly = true;

        this.ModalPopupExtender1.Show();
    }
}


数据库建库脚本


use pubs
if exists (select * from sysobjects where name='DepartInfo')
drop table DepartInfo
GO
Create Table DepartInfo(
DepartID varchar(20),
DepartName varchar(60),
SortID int,
State varchar(4))
GO

以上只是一个能解决实际技术问题的演示实例,当然还有好多功能没有一一实现(例如删除),当然那些都很简单了,有兴趣的朋友可以完善一下。

 


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 安置房怎么办房产证吗 美甲后指甲长了怎么办 在菲律宾怎么办持枪证? 绝地求生打不准怎么办 身份证号码变更后社保怎么办 社保与身份不符怎么办 年龄改了学籍怎么办 结婚证信息错误怎么办 六级身份证过期怎么办 身份号泄露了怎么办 身体证信息泄露怎么办 手机号被泄漏了怎么办 姓名和电话泄露怎么办 个人身份证信息泄露怎么办 身份号码泄露了怎么办 我身份证泄露了怎么办 身份证信息泄漏了怎么办 无锡身份证丢了怎么办 人在外地怎么办身份证 欠空放公司不还怎么办 兼职要身份证照片怎么办 身份证刷不了磁怎么办 身份证不能刷了怎么办 身份证指纹错了怎么办 指纹手机丢了怎么办 异地办理临时身份证怎么办 杭州办理外地身份证怎么办 办理身份证没有户口本怎么办 2018身份证掉了怎么办 双户口注销社保怎么办 常用户口被注销怎么办 刚到厦门怎么办身份证 新疆身份证丢了怎么办 技能证书丢了怎么办 16岁拍身份证怎么办 16岁以下怎么办身份证 办身份证要证明怎么办 15岁怎么办银行卡淘宝 满16岁怎么办银行卡 电脑最小化后不见了怎么办 满16周岁怎么办银行卡