前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >12-WebForm轻量版增删改查

12-WebForm轻量版增删改查

作者头像
静心物语313
发布2020-03-24 10:52:48
8010
发布2020-03-24 10:52:48
举报

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

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

代码语言:javascript
复制
<%@ 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;了,

代码语言:javascript
复制
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、首先绘制“编辑”“新增”界面

代码语言:javascript
复制
<%@ 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、以及保存功能

代码语言:javascript
复制
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");
        }
    }
} 
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云开发 CLI 工具
云开发 CLI 工具(Cloudbase CLI Devtools,CCLID)是云开发官方指定的 CLI 工具,可以帮助开发者快速构建 Serverless 应用。CLI 工具提供能力包括文件储存的管理、云函数的部署、模板项目的创建、HTTP Service、静态网站托管等,您可以专注于编码,无需在平台中切换各类配置。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档