【复习计划6】三层架构1(UI1)

UI层需要调用model层+bll层

<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td>
                编号:
                </td>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>产品名称:</td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                        ControlToValidate="TextBox1" Display="Dynamic" ErrorMessage="不能为空!" 
                        ForeColor="#FF3300"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td>售价:</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                    <asp:CompareValidator ID="CompareValidator1" runat="server" 
                        ControlToValidate="TextBox2" Display="Dynamic" ErrorMessage="输入数字!" 
                        ForeColor="Red" Operator="DataTypeCheck" Type="Double"></asp:CompareValidator>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" 
                        ControlToValidate="TextBox2" Display="Dynamic" ErrorMessage="不能为空!" 
                        ForeColor="#FF3300"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td class="style1">市场价:</td>
                <td class="style1">
                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" 
                        ControlToValidate="TextBox2" Display="Dynamic" ErrorMessage="不能为空!" 
                        ForeColor="#FF3300"></asp:RequiredFieldValidator>
                    <asp:CompareValidator ID="CompareValidator2" runat="server" 
                        ControlToValidate="TextBox3" Display="Dynamic" ErrorMessage="输入数字!" 
                        ForeColor="Red" Operator="DataTypeCheck" Type="Double"></asp:CompareValidator>
                </td>
            </tr>
            <tr>
                <td>产品类别:</td>
                <td>
                    <asp:DropDownList ID="DropDownList1" runat="server">
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td>简介:</td>
                <td>
                    <asp:TextBox ID="TextBox4" runat="server" TextMode=MultiLine Height="83px"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" 
                        ControlToValidate="TextBox4" Display="Dynamic" ErrorMessage="不能为空!" 
                        ForeColor="#FF3300"></asp:RequiredFieldValidator>
                    </td>
            </tr>
            <tr>
                <td>是否上架:</td>
                <td>
                    <asp:CheckBox ID="CheckBox1" runat="server" />
                </td>
            </tr>
            <tr>
                <td colspan = 2 align ="center">
                    <asp:Button ID="button1" runat="server" Text="提交" onclick="button1_Click" />
                </td>
            </tr>

        </table>
    </div>
    </form>
</body

然后写好UI,UI需要调用的一点css

 <style type="text/css">
        #TextArea1 {
            height: 97px;
        }
        #ta1
        {
            height: 85px;
        }
        .style1
        {
            height: 25px;
        }
    </style>

其中验证控件需要设置判断选项

RequiredFieldValidator 的display为Dynamic

ControlToValidate值为需要判断的text文本框

字体颜色和属性都可以更改


UI界面写完以后打开后台

在Page_Load窗体加载时

//跳转的时候从前台获取pid然后进行判断是否为空值,然后使用querystring 来get传过来的值
            var pd = Request.QueryString["pid"];
            if (string.IsNullOrEmpty(pd))
            {
                //进入if后若判断条件为空则跳转回原页面
                Response.Redirect("Index.aspx");
            }
            if (!IsPostBack)
            {
                int ID = Convert.ToInt32(pd);
                var product = ProductM.Select(ID);
                if (product != null)
                {

                    DropDownList1.DataSource = ProductCategoryM.Select();
                    DropDownList1.DataValueField = "id";
                    DropDownList1.DataTextField = "Name";
                    DropDownList1.DataBind(); 

                    Label1.Text = product.PID+"";
                    TextBox1.Text = product.ProductName;
                    TextBox2.Text = product.MarketPrice+"";
                    TextBox3.Text = product.SellingPrice+"";
                    TextBox4.Text = product.Introduction + "";
                    DropDownList1.SelectedValue = product.CategoryId + "";
                    CheckBox1.Checked = product.IsOnSale == 1;
                }

            }

然后给提交按钮添加事件:

var product = new Product()
            {
                PID = Convert.ToInt32(Label1.Text),
                ProductName = TextBox1.Text,
                MarketPrice = Convert.ToDecimal(TextBox2.Text),
                SellingPrice = Convert.ToDecimal(TextBox3.Text),
                Introduction = TextBox4.Text,
                CategoryId = Convert.ToInt32(DropDownList1.SelectedValue),
                IsOnSale = CheckBox1.Checked ? 1 : 0

                
            };
            if (ProductM.Update(product)>0)
            {
                Response.Write("<script>alert('修改成功');location.href= 'Index.aspx' </script>");
            }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值