Webfrom 生成流水号 组合查询 Repeater中单选与复选控件的使用 JS实战应用
Default.aspx 网页界面
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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">
<div>
<asp:Label ID="Label1" runat="server" Text="流水号生成" Font-Bold="True" Font-Size="30px"></asp:Label>
<br />
代号:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="生成流水号" />
<br />
姓名:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
性别:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
民族:
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<br />
生日:
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button2" runat="server" Text="提交" />
<br />
<br />
</div>
</form>
</body>
</html>
Defaule.aspx.cs C#界面
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
private MyDBDataContext _Context = new MyDBDataContext();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string prefix = "p" + DateTime.Now.ToString("yyyyMMdd");
//1查当天最大的流水号
int maxFlow = 0;
var query = _Context.Info.Where(p=>p.Code.StartsWith(prefix));
if (query.Count() > 0)
{
query = query.OrderByDescending(p=>p.Code);//按流水号经序排列
string maxCode = query.First().Code;//获取最大流水号
maxFlow =Convert.ToInt32(maxCode.Substring(9));
}
//组合查询最新的流水号
string flowCode = prefix + (maxFlow + 1).ToString("000");
//显示在文本框中
TextBox1.Text = flowCode;
}
}
*********************************************************************************************
组合查询
*********************************************************************************************
Default.aspx 网页界面
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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">
<h1>组合查询</h1>
<div>
车名:<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
系列:<asp:DropDownList ID="ddlBrand" runat="server" AppendDataBoundItems="True">
<asp:ListItem Value="-1">==请选择==</asp:ListItem>
</asp:DropDownList>
油耗:<asp:TextBox ID="txtOil" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="查询" OnClick="Button1_Click" />
<br />
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li>
<%# Eval("Name") %>
<font color="red"><%# Eval("Brand") %></font>
<font color="blue"><%# Eval("Oil") %></font>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
Defaule.aspx.cs C#界面
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
private MyDBDataContext _Context = new MyDBDataContext();
private void FillBrand()
{
var query = _Context.Brand;
ddlBrand.DataSource = query;
ddlBrand.DataTextField = "Brand_Name";
ddlBrand.DataValueField = "Brand_Code";
ddlBrand.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillBrand();
}
}
//核心代码:
protected void Button1_Click(object sender, EventArgs e)
{
var query1 = _Context.Car.AsQueryable(); //根据名称查 默认情况下应当是全集
var query2 = _Context.Car.AsQueryable();//根据系列查
var query3 = _Context.Car.AsQueryable();//根据油耗查
//根据输入框的填写情况来完善上面三个查询条件
if (txtName.Text.Trim().Length > 0)
{
query1 = query1.Where(p => p.Name.Contains(txtName.Text));
}
if (ddlBrand.SelectedValue != "-1")
{
query2 = query2.Where(p => p.Brand == ddlBrand.SelectedValue);
}
if (txtOil.Text.Trim().Length > 0)
{
query3 = query3.Where(p => p.Oil == Convert.ToDecimal(txtOil.Text));
}
//三个查询条件是逻辑与的关系,可以使用集合操作的交集来实现
var query = query1.Intersect(query2).Intersect(query3);
//绑定显示
Repeater1.DataSource = query;
Repeater1.DataBind();
}
}
*********************************************************************************************
Repeater中单选与复选控件的使用 JS实战应用
*********************************************************************************************
Default.aspx 网页界面
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!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>
<script language="javascript">
function checkAll(ckall) {
//找所有的每一项左侧的复选框。
var cks = document.getElementsByName("ck");
//遍历每个复选框,把每个复选框的选中状态设置得和chkall一样
for (var i = 0; i < cks.length; i++) {
cks[i].checked = ckall.checked;
}
}
function setCheckAll() {
//思路:遍历所有项的复选框,判断是否全选。是全选的话,把ckall设为选中,否则把它设为不选中。
//1.找到所有的项的复选框。
var cks = document.getElementsByName("ck");
//2.遍历每个复选框,记录是否被全选了。
var selectedall = true;
for (var i = 0; i < cks.length; i++) {
selectedall = selectedall && cks[i].checked;
}
//3.根据是否被全选的状态,确定ckall的选中状态
var ckall = document.getElementById("ckall");
ckall.checked = selectedall;
}
function dodelete() {
//1.找到所有的项的复选框。
var cks = document.getElementsByName("ck");
//2.判断是否有选中项
var selecteditem = false;
for (var i = 0; i < cks.length; i++) {
selecteditem = cks[i].checked || selecteditem;
}
//3.根据选中项的情况给出不同的对话框提示
if (selecteditem == false) {
alert("请选择要删除的项");
return false;
}
else {
var del = confirm("确认要删除选中的项吗?");
return del;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table width="100%" border="0" cellspacing="5">
<tr>
<td>
<input id="ckall" name="ckall" type="checkbox" onclick="checkAll(this)" />
</td>
<td>名称</td>
<td>系列</td>
<td>厂商</td>
<td>价格</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<input id="ck_<%# Eval("Code") %>" name="ck" type="checkbox" value="<%# Eval("Code") %>" onclick="setCheckAll()" />
</td>
<td><%# Eval("Name") %></td>
<td><%# Eval("BrandName") %></td>
<td><%# Eval("ProdName") %></td>
<td><%# Eval("Price") %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
<asp:Button ID="Button1" runat="server" OnClientClick="return dodelete()" OnClick="Button1_Click" Text="删除" />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
</form>
</body>
</html>
Defaule.aspx.cs C#界面
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default3 : System.Web.UI.Page
{
private MyDBDataContext _Context = new MyDBDataContext();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Show();
}
}
private void Show()
{
var query = _Context.Car;
Repeater1.DataSource = query;
Repeater1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
if (Request["ck"] != null)
{
//获取选中项的主键值
string s = Request["ck"].ToString(); //选中项的value值,使用,隔开的。
string[] keys = s.Split(','); //劈开上面的字符串,形成主键值的数组。
//根据上面的主键值删除数据
foreach (string key in keys)
{
var query = _Context.Car.Where(p=>p.Code == key);
if (query.Count() > 0)
{
Car data = query.First();
_Context.Car.DeleteOnSubmit(data);
_Context.SubmitChanges();
}
}
//刷新显示
Show();
Literal1.Text = "<script language='javascript'>alert('删除成功')</script>";
}
}
}