12-WebForm轻量版增删改查

1、Repeater 控件可以直接放在 body 中! Form 去掉!(这里只是展示的功能)

2、列表展示页面,在本页禁止viewstate

<%@ Page Language="C#" AutoEventWireup="true" EnableViewStateMac="false" CodeBehind="EmpList.aspx.cs" Inherits="Web1.WebFormTestZSGC.EmpList" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <title></title>

</head>

<body>

    <a href="EmpAddNewEdit.aspx?action=addnew">新增</a>

    <table style="border:solid 1px ;border-color:blue; ">

        <thead>

            <tr><th>公司名称</th><th>公司地址</th><th>公司经理</th><th>编辑</th></tr>

        </thead>

        <tbody>

            <asp:Repeater ID="RepeaterEmps" runat="server">

                <ItemTemplate><tr><td><%#Eval("Name") %></td>

                                  <td><%#Eval("Address") %></td>

                                  <td><%#Eval("MangerName") %></td>

                    <td><a href="EmpAddNewEdit.aspx?action=edit&id=<%#Eval("id") %>">编辑</a></td>

                              </tr></ItemTemplate>

            </asp:Repeater>

        </tbody>

    </table>

</body>

</html>

3、由于禁用了viewstate。所以不用写IsPostBack;了,

using System;

using System.Collections.Generic;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

namespace Web1.WebFormTestZSGC

{

    public partial class EmpList : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            DataTable td = SqlHelper.ExecuteQuery

        ("select a.id id, a.Name Name,a.Address Address,b.Name MangerName from Companys a left join Managers b on a.ManagerId=b.id;");

            RepeaterEmps.DataSource = td;

            RepeaterEmps.DataBind();

        

        }

    }

}

 

4、做编辑和新增功能:

5、首先绘制“编辑”“新增”界面

<%@ Page Language="C#" EnableViewStateMac="false" AutoEventWireup="true" CodeBehind="EmpAddNewEdit.aspx.cs" Inherits="Web1.WebFormTestZSGC.EmpAddNewEdit" %>

 

<!DOCTYPE html>

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

           公司名称:<asp:TextBox ID="txtName" runat="server"></asp:TextBox>

           公司地址:<asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>

           公司经理:<asp:DropDownList ID="ddlManager" runat="server" 

               DataTextField="Name" DataValueField="id"></asp:DropDownList>

            <asp:Button ID="btnSave" runat="server" Text="保存" OnClick="btnSave_Click"/>

        <asp:Label ID="LabelMsg" runat="server" ></asp:Label>

         </form>

</body>

</html>

 

6、逻辑实现功能如下

7、以及保存功能

using System;

using System.Collections.Generic;

using System.Data;

using System.Data.SqlClient;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

namespace Web1.WebFormTestZSGC

{

    public partial class EmpAddNewEdit : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            //1、判断是不是保存回来的,保存会来的就是,点击了保存按钮的

            if (!IsPostBack)

            {

                //4、在这里首先查询,经理表,加载页面就填充下拉列表(必须写在这个位置)

                DataTable dtManager = SqlHelper.ExecuteQuery("select * from Managers");

                //绑定到DropDownList中

                ddlManager.DataSource = dtManager;

                ddlManager.DataBind();

                //5、在aspx中配置DropDownList 的字段

                //   DataTextField="Name" DataValueField="id">

 

                //2、判断是新增保存,还是编辑保存

                string action = Request["action"];

                if (action == "addnew")

                {

                    txtName.Text = "有限责任公司";

                }

                else if (action == "edit")

                {

                    int id = Convert.ToInt32(Request["id"]);

                    DataTable td = SqlHelper.ExecuteQuery("select * from Companys where id=@id;"

                        , new SqlParameter("@id", id));

                    if (td.Rows.Count <= 0)

                    {

                        //没有站到

                        LabelMsg.Text = "找不到您要的信息!";

                        return;

                    }

                    DataRow row = td.Rows[0];

                    txtName.Text = (string)row["Name"];

                    txtAddress.Text = (string)row["Address"];

                    //3、接下来,将经理的名字与DropDownList进行绑定

 

                    //6、通过下拉列表进行 选值 与 赋值

                    //   ddlManager.SelectedValue是string类型的。报文都是string类型的

                    //  int 不能显示转化  string 需要 Convert.ToString 这样

 

                    ddlManager.SelectedValue = Convert.ToString(row["ManagerId"]);

                }

                else

                {  //7、

                    LabelMsg.Text = "action错误!";

                    return;

                }

            }

        }

 

        //8、双击保存按钮到这里

        protected void btnSave_Click(object sender, EventArgs e)

        {

            //9、保存分两种清苦是“编辑”“新增”

            //通过请求url中的action 判断

            // 最后解释一下:为什么 action 和id 没有自己使用添加的隐藏字段呢?

            //回答:因为webform自动把action=edit&id=2放在了formde action中

            //所以,就可以通过QureyString拿到action和id,故不粗要单独的hidden和input

            string action=Request["action"];//通过开发者工具查看,知道action是保存在了form 表单的action中,这个request是从QueryString中取的

            string name = txtName.Text;

            string address = txtAddress.Text;

 

            int managerId = Convert.ToInt32(Request["ddlManager"]);//----方法1:直接从报文中取的下拉列表对应的value值

            //int managerId = Convert.ToInt32(ddlManager.SelectedValue);//方法2:这种写法是需要viewState的支持的

            if (action=="addnew")

            {

                

                //10、执行插入

                SqlHelper.ExecuteNonQuery("insert into Companys(Name,Address,ManagerId) values(@Name,@Address,@ManagerId)"

                    ,new SqlParameter("@Name",name)

                    , new SqlParameter("@Address",address)

                    , new SqlParameter("@managerId", managerId));

            }

            else if (action == "edit")

            {

                int id = Convert.ToInt32(Request["id"]);

                SqlHelper.ExecuteNonQuery("Update Companys set Name=@Name,Address=@Address,ManagerId=@ManagerId where id=@id"

                   , new SqlParameter("@Name", name)

                   , new SqlParameter("@Address", address)

                   , new SqlParameter("@managerId", managerId)

                   , new SqlParameter("@id", id));

            }

            else

            {

                throw new Exception("action错误!");

            }

            Response.Redirect("EmpList.aspx");

        }

    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

静心物语313

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值