cb.aspx

来自「ASP.NET开发实战详解」· ASPX 代码 · 共 291 行

ASPX
291
字号
<%@ Page Language="VB" Debug="true" %>
<%@ import Namespace="system.data" %>
<%@ import Namespace="system.data.sqlclient" %>
<script runat="server">

    dim Cnn as sqlconnection
    dim cmd as sqlcommand
    Sub Page_Load(Sender As Object, E As EventArgs)
      If Not IsPostBack Then
         If Session("curruser") = "" Then
            Response.Redirect("../index.aspx")
         End If
        bindgrid()
        cbtime.text=year(now) & "-" & month(now) & "-" & day(now)  
      end if
    End Sub
    
    Sub save_click(Sender As Object, E As EventArgs)
      dim sql as string
      dim datar as sqldatareader
      dim dycode as string
      dim dystr as string=dyname.text
      dim biaostr as string=selbiao.selecteditem.value
      dim datestr as string=cbtime.text
      dim yfstr as string=cbyf.selecteditem.value
      dim cbnumstr as string=cbnum.text
      dim cbrstr as string=cbr.text
      dim memostr as string=memo.text
      if trim(dystr)="" then
         page.registerstartupscript("",getalertinfo("请输入单元名称!"))
         exit sub
      end if
      if trim(datestr)="" or cbnumstr="" then
         page.registerstartupscript("",getalertinfo("抄表日期及抄表数量均不可为空!"))
         exit sub
      end if
      cnn=new sqlconnection(configurationsettings.appsettings("connection"))
      sql="select * from fc_cell where name='" & trim(dystr) & "'"
      cmd=new sqlcommand(sql,cnn)
      cnn.open
      datar=cmd.executereader()
      if datar.read() then
         dycode=datar("code")
      else
         page.registerstartupscript("",getalertinfo("该单元不存在,数据无法保存!"))
         exit sub
      end if
      datar.close()
      sql="select * from cw_cb where id=" & trim(biaostr) & " and sf=" & yfstr
      cmd=new sqlcommand(sql,cnn)
      datar=cmd.executereader()
      if datar.read() then
         page.registerstartupscript("",getalertinfo("该表该月份已进行抄表操作,数据无法保存!"))
         exit sub
      end if
      datar.close()

      sql="insert into cw_cb(biaocode,type,sf,ddate,num,cbr,memo) values(" & biaostr & ",'用户表'," & yfstr & ",'" & datestr & "'," & cbnumstr & ",'" & cbrstr & "','" & memostr & "')"
      cmd=new sqlcommand(sql,cnn)
      cmd.executenonquery
      bindgrid()
      cnn.close()
      cbnum.text=""
      memo.text=""
      page.registerstartupscript("",getalertinfo("数据保存成功!"))
    End Sub
    
    Function GetAlertInfo(byval str1 as string) as String
        dim Str2 as String
        str2="<script language='javascript'>alert('" & str1 & "')<"
        str2+="/"
        str2+="script>"
        return trim(str2)
    End Function

         Sub BindGrid()
            dim dataR as sqldatareader
            dim dtcmd as sqldataadapter
            dim dt as new datatable
            dim MaxPage as integer  '计算最大页数
            dim str2 as string
            dim Str1 as string
            dim dystr as string=query_dy.text
            dim yfstr as string=query_yf.text
            if dystr<>"" then
               str1=" and name='" & dystr & "' "
            end if
            if yfstr<>"" then
               str1=str1 & " and sf=" & yfstr & " "
            end if
            cnn=new sqlconnection(configurationsettings.appsettings("connection"))
            str2="select count(*) as nums from cw_cb a,cw_biao_user b,cw_biao_info c,fc_cell d where a.biaocode=b.id and b.cellcode=d.code and b.biaocode=c.id " & str1
            cmd=new sqlcommand(str2,cnn)
            cnn.open()
            datar=cmd.executereader()
            if datar.read() then
               if datar("nums").tostring()="0" then
                  msg.text=""
               else
                  msg.text="合计:记录数 " & datar("nums").tostring()
               end if
            end if
            datar.close()
            str2="select a.id,d.name,c.code,sf,num,a.ddate,cbr,a.memo from cw_cb a,cw_biao_user b,cw_biao_info c,fc_cell d where a.biaocode=b.id and b.cellcode=d.code and b.biaocode=c.id " & str1 & " order by a.id desc"
            dtcmd=new sqldataadapter(str2,cnn)
            dtcmd.fill(dt)
            if (dt.defaultview.count mod datagrid1.pagesize)>0 then
               maxpage=dt.defaultview.count \datagrid1.pagesize+1
            else
               maxpage=dt.defaultview.count \datagrid1.pagesize
            end if
            do while datagrid1.currentpageindex>0
               if maxpage<datagrid1.currentpageindex+1 then
                 datagrid1.currentpageindex=datagrid1.currentpageindex-1
               else
                 exit do
              end if
            loop
            datagrid1.datasource=dt
            datagrid1.databind()
            cnn.close()
         End Sub

         Sub Grid1_Mod(Sender as object,E as DataGridCommandEventArgs)
             dim codestr as string=E.Item.Cells(0).Text
             dim sql as string
                if CType(e.CommandSource,LinkButton).CommandName="Delete" then
                      cnn=new sqlconnection(configurationsettings.appsettings("connection"))
                      cnn.open()
                      sql="delete from cw_cb where id=" & codestr
                      cmd=new sqlcommand(sql,cnn)
                      cmd.executeNonQuery
                      cnn.close()
                      bindgrid()
                end if
         End Sub

         Sub Grid1_Update(Sender as object,E as DataGridCommandEventArgs)
             dim NumStr as String=CType(E.Item.Cells(4).Controls(0),TextBox).Text
             dim MemoStr As string=CType(E.Item.Cells(7).Controls(0),TextBox).Text
             dim CodeStr as String=E.Item.Cells(0).Text
             dim sql as string
             cnn=new sqlconnection(configurationsettings.appsettings("connection"))
             cnn.open()
             sql="update cw_cb set num=" & numstr & ",memo='" & memostr & "' where id=" & codestr
             cmd=new sqlcommand(sql,cnn)
             cmd.executeNonQuery
             cnn.close()
             DataGrid1.EditItemIndex=-1
             BindGrid()
         End Sub
    
         Sub Grid1_Cancel(Sender as object,E as DataGridCommandEventArgs)
             datagrid1.EditItemIndex=-1
             BindGrid()
         End Sub
    
         Sub Grid1_Edit(Sender as object,E as DataGridCommandEventArgs)
             DataGrid1.EditItemIndex=E.Item.ItemIndex
             BindGrid()
         End Sub

    Sub query_click(Sender As Object, E As EventArgs)
       bindgrid()
    End Sub

	      Function CreateDataSource() As ICollection
		dim dtcmd as sqldataadapter
	        dim dset as dataset
                dim Str1 as string=dyname.text
        	cnn=new sqlconnection(configurationsettings.appsettings("connection"))
                dtcmd=new sqldataadapter("select a.id,(b.code+' '+b.type) as biao from cw_biao_user a,cw_biao_info b,fc_cell c where a.biaocode=b.id and a.cellcode=c.code  and name='" & str1 & "' order by b.type,b.id",cnn)
        	dset=new dataset
	        dtcmd.fill(dset)
        	Dim dv As New DataView(dset.tables(0))
	        Return dv
        	cnn.close()
	    End Function

              Sub OnChangedDY(Sender As Object, E As EventArgs)
	            Selbiao.DataSource = CreateDataSource()
        	    Selbiao.DataTextField = "biao"
	            Selbiao.DataValueField = "id"
        	    Selbiao.DataBind()
              End Sub    	

         Sub Grid1_PageIndexChanged(Sender As Object, E As DataGridPageChangedEventArgs)
             datagrid1.CurrentPageIndex = e.NewPageIndex
             BindGrid()
         End Sub
</script>
<html>
<head>
    <title>抄表设置</title>
    <link href="../Main.css" type="text/css" rel="stylesheet" />
</head>
<body background="../images/1.jpg">
    <form runat="server">
        <table id="search" cellspacing="1" cellpadding="0" width="80%" align="center" border="0">
            <tbody>
                <tr>
                    <td width="34%" height="30">
                        单元名称
                        <asp:TextBox class="input_text" id="dyname" runat="server" width="110"  OnTextChanged="OnChangedDY" AutoPostBack=True></asp:TextBox>
                    </td>
                    <td colspan=2 height="30">
                        该单元所装表 
                        <asp:DropDownList class="input_text" id="selbiao" runat="server" width="190"></asp:dropdownlist>
                    </td>
                </tr>
                <tr class="tr4">
                    <td width="34%" height="30">
                        抄表月份 
                        <asp:DropDownList id="cbyf" runat="server" width="110">
	                    <asp:ListItem Value="1" selected="true">一月</asp:ListItem>
			    <asp:ListItem Value="2">二月</asp:ListItem>
			    <asp:ListItem Value="3">三月</asp:ListItem>
			    <asp:ListItem Value="4">四月</asp:ListItem>
			    <asp:ListItem Value="5">五月</asp:ListItem>
			    <asp:ListItem Value="6">六月</asp:ListItem>
			    <asp:ListItem Value="7">七月</asp:ListItem>
			    <asp:ListItem Value="8">八月</asp:ListItem>
			    <asp:ListItem Value="9">九月</asp:ListItem>
			    <asp:ListItem Value="10">十月</asp:ListItem>
			    <asp:ListItem Value="11">十一月</asp:ListItem>
			    <asp:ListItem Value="12">十二月</asp:ListItem>
			</asp:DropDownList>
                    </td>
                    <td width="33%">
                        抄表时间 
                        <asp:TextBox class="input_text" id="cbtime" runat="server" width="110"></asp:TextBox>
                    </td>
                    <td width="33%">
                        抄表数量 
                        <asp:TextBox class="input_text" id="cbnum" runat="server" width="130"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        抄&nbsp;表&nbsp;人 
                        <asp:TextBox class="input_text" id="cbr" runat="server" width="110"></asp:TextBox>
                    </td>
                    <td colspan="2" height="30">
                        备&nbsp;&nbsp;&nbsp;&nbsp;注 
                        <asp:TextBox class="input_text" id="memo" runat="server" width="215"></asp:TextBox>
                        &nbsp;&nbsp;&nbsp;&nbsp;
                        <asp:button class="input_button" id="SaveBtn" onclick="Save_Click" runat="server" text="保  存"></asp:button>
                    </td>
                </tr>
            </tbody>
        </table><hr>
                <table id="search" cellspacing="1" cellpadding="0" width="90%" align="center" border="0">
                    <tbody>
                        <tr>
                            <td style="PADDING-LEFT: 5px" height="25">
                               单元名称
                               <asp:TextBox class="input_text" id="query_dy" runat="server" width="80"></asp:TextBox>
                               &nbsp;&nbsp;&nbsp;&nbsp;
                               抄表月份
                               <asp:TextBox class="input_text" id="query_yf" runat="server" width="80"></asp:TextBox>
                               &nbsp;&nbsp;&nbsp;&nbsp;
                               <asp:button class="input_button" id="QueryBtn" onclick="Query_Click" runat="server" text="查  询"></asp:button>
                               &nbsp;&nbsp;&nbsp;&nbsp;  
                               <asp:Label id="Msg" runat="server" forecolor="red"></asp:Label></td>
                        </tr>
                        <tr>
                            <td style="PADDING-LEFT: 5px" height="25">
                                <asp:DataGrid id="DataGrid1" runat="server" width="100%" OnPageIndexChanged="Grid1_PageIndexChanged" AllowSorting="True" AllowPaging="True" Font-Names="verdana" bordercolor="Black" cellpadding="2" font-name="verdana" font-size="8pt" headerstyle-backcolor="#F8FAFC" OnEditCommand="Grid1_Edit" OnCancelCommand="Grid1_Cancel" OnupdateCommand="Grid1_Update" OnItemCommand="Grid1_Mod" AutoGenerateColumns="False" PageSize="8">
                                    <HeaderStyle font-bold="True" horizontalalign="Center" verticalalign="Middle" backcolor="#F8FAFC"></HeaderStyle>
                                    <PagerStyle nextpagetext="下一页" font-names="宋体" font-bold="True" prevpagetext="上一页" horizontalalign="Right" forecolor="Blue" mode="NumericPages"></PagerStyle>
                                    <EditItemStyle backcolor="#E9F0F8"></EditItemStyle>
                                    <Columns>
                                        <asp:BoundColumn DataField="id" visible="false" ReadOnly="True" HeaderText="序号"></asp:BoundColumn>
                                        <asp:BoundColumn DataField="name" ReadOnly="True" HeaderText="单元名称"></asp:BoundColumn>
                                        <asp:BoundColumn DataField="code" readonly="true" HeaderText="表编码"></asp:BoundColumn>
                                        <asp:BoundColumn DataField="sf" readonly="true" HeaderText="月份"></asp:BoundColumn>
                                        <asp:BoundColumn DataField="num" HeaderText="抄表数量"></asp:BoundColumn>
                                        <asp:BoundColumn DataField="ddate" readonly="true" HeaderText="抄表日期" DataFormatString="{0:yyyy-MM-dd}"></asp:BoundColumn>
                                        <asp:BoundColumn DataField="cbr" readonly="true" HeaderText="抄表人"></asp:BoundColumn>
                                        <asp:BoundColumn DataField="memo" HeaderText="备注"></asp:BoundColumn>
                                        <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" HeaderText="编辑" CancelText="取消" EditText="编辑"></asp:EditCommandColumn>
                                        <asp:ButtonColumn Text="删除" HeaderText="删除" CommandName="Delete"></asp:ButtonColumn>
                                    </Columns>
                                </asp:DataGrid>
                            </td>
                        </tr>
                    </tbody>
                </table>
    </form>
</body>
</html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?