<%@Page Language="VB" Inherits="BaseClass" Src="masters3.vb" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<html>
  <script language="VB" runat="server">
    Sub Grid1_Update(sender As Object, e As DataGridCommandEventArgs)
      Dim sql As string
      Dim code_display As String 
      Dim code_category As String 
      Dim type As String 
      Dim opening As String 
      Dim closing As String 
      Dim myTextBox As TextBox
      '主要区域：只读区
      Dim code_value as string   = Grid1.DataKeys.Item(E.Item.ItemIndex).ToString
      myTextBox = E.Item.FindControl("edit_name")
      code_display = mytextbox.text
      myTextBox = E.Item.FindControl("edit_group")
      code_category = mytextbox.text
      myTextBox = E.Item.FindControl("edit_type")
      type = mytextbox.text
      myTextBox = E.Item.FindControl("edit_opening")
      opening = mytextbox.text
      myTextBox = E.Item.FindControl("edit_closing")
      closing = mytextbox.text
      '执行存储过程
      sql = "Execute p_masters " + code_value + ", '" + code_display + "'," 
      sql = sql + code_category + ", '" + type +"' ,"  + opening + "," + closing
      RunSql(sql)
    End Sub
    Sub add_click(Source As Object, E As EventArgs) 
      Dim sql As string  
      If acode_display.text = "" or acode_category.SelectedItem.Text = "" then
        response.write("信息不完全")
        exit sub
      end if
      SQL = "Execute p_masters @code_value=NULL,"
      SQL = SQL + " @code_display = '" +  acode_display.text + "' , @group_name = '" 
      SQL = SQL +  acode_category.SelectedItem.Text + "' ,  @type = NULL ,"
      SQL = SQL + " @opening =" + aopening.text + ", @closing = 0" 
      RunSql(sql) 
      'reset values
      acode_display.text = ""
      aopening.text  = ""
      hidePanel()
    End Sub
    Sub add_show(Source As Object, E As EventArgs) 
      AddPanel.visible = true
    End Sub
    Sub Grid1_delete(sender As Object, e As DataGridCommandEventArgs)
      Dim code_value as string   = Grid1.DataKeys.Item(E.Item.ItemIndex).ToString
      Dim sql As string
      sql = "Delete from masters where code_value = " + cstr(code_value)
      RunSql(sql)
    End Sub
  </script>
  <body style="font: 10pt verdana">
    <form runat="server">
    <asp:ValidationSummary runat=server headertext="页面上有错误:" />
      
      <asp:HyperLink runat="server" Text="Trial Balance" NavigateUrl="TrialBalance.aspx">
      </asp:HyperLink>&nbsp
      <asp:HyperLink runat="server" Text="Transactions" NavigateUrl="selection.aspx">
      </asp:HyperLink>&nbsp
      <asp:HyperLink runat="server" Text="Home" NavigateUrl="default.aspx">
      </asp:HyperLink>
      <h3><font face="Verdana"> Accounts表 </font></h3>
      <asp:Label id="Message" runat="server"/>
      <asp:Button id="Addshow" text="添加记录" onclick="add_show" runat="server" />
      <table width="95%">
        <tr>
        <td valign="top">
          <asp:DataGrid id="Grid1" runat="server"
            AutoGenerateColumns="false"
            BackColor="White"
            BorderWidth="1px" BorderStyle="Solid" BorderColor="Tan"
            CellPadding="2" CellSpacing="0"
            Font-Name="Verdana" Font-Size="8pt"
            OnEditCommand="Grid1_Edit"
            OnCancelCommand="Grid1_Cancel"
            OnUpdateCommand="Grid1_Update"
            OnDeleteCommand = "Grid1_delete"
            DataKeyField="code_value">
            <Columns>
            <asp:EditCommandColumn
              EditText="编辑"
              CancelText="取消"
              UpdateText="确定"
              ItemStyle-Wrap="false"
              HeaderText="编辑"
              HeaderStyle-Wrap="false"/>
            <asp:ButtonColumn Text="删除" CommandName="删除"
              HeaderText="Delete"/>
            <asp:BoundColumn HeaderText="Account #" ReadOnly="true"
              DataField="code_value"/>
            <asp:TemplateColumn HeaderText="Name" >
              <ItemTemplate>
                <asp:Label Text='<%# Container.DataItem("code_display") %>' 
                  runat="server"/>
              </ItemTemplate>
              <EditItemTemplate>
                <asp:RequiredFieldValidator runat=server 
                  controltovalidate=edit_Name
                  errormessage="Name is required.">*
                </asp:RequiredFieldValidator>
                <asp:TextBox id="edit_name" 
                  Text='<%# Container.DataItem("code_display") %>'
                  runat="server"/>
              </EditItemTemplate>
            </asp:TemplateColumn>
            <asp:TemplateColumn HeaderText="Group" >
              <ItemTemplate>
                <asp:Label Text='<%# Container.DataItem("category") %>'
                  runat="server" />
              </ItemTemplate>
              <EditItemTemplate>
                <asp:TextBox id="edit_group" BorderStyle="None" 
                  Readonly="True" Text='<%# Container.DataItem("code_category") %>'
                  runat="server"/>
              </EditItemTemplate>
            </asp:TemplateColumn>
            <asp:TemplateColumn HeaderText="Type" >
              <ItemTemplate>
                <asp:Label Text='<%# Container.DataItem("type") %>' 
                  runat="server"/>
              </ItemTemplate>
              <EditItemTemplate>
                <asp:TextBox id="edit_type" BorderStyle="None"
                  Readonly="True" Text='<%# Container.DataItem("type") %>'
                  runat="server"/>
              </EditItemTemplate>
            </asp:TemplateColumn>
            <asp:TemplateColumn HeaderText="Opening" >
              <ItemTemplate>
                <asp:Label Text='<%# Container.DataItem("opening") %>' 
                  runat="server"/>
              </ItemTemplate>
              <EditItemTemplate>
                <asp:TextBox id="edit_opening"  
                  Text='<%# Container.DataItem("opening") %>'
                  runat="server"/>    
              </EditItemTemplate>
            </asp:TemplateColumn>
            <asp:TemplateColumn HeaderText="Closing" >
              <ItemTemplate>
                <asp:Label Text='<%# Container.DataItem("closing") %>' 
                  runat="server"/>
              </ItemTemplate>
              <EditItemTemplate>
                <asp:TextBox id="edit_closing" BorderStyle="None" 
                  Readonly="True" Text='<%# Container.DataItem("closing") %>'
                  runat="server"/>
              </EditItemTemplate>
            </asp:TemplateColumn>
            </Columns>
            <HeaderStyle BackColor="DarkRed" ForeColor="White" Font-Bold="true">
            </HeaderStyle >
            <ItemStyle ForeColor="DarkSlateBlue">
            </ItemStyle>
            <AlternatingItemStyle BackColor="Beige">
            </AlternatingItemStyle>
          </asp:DataGrid>
        </td>
        <td valign="top">
          <!---- 插入行 逻辑-------------->
          <asp:Panel id="AddPanel" runat="server" Visible="false">
            <table style="font: 8pt verdana">
              <tr>
                <td colspan="2" bgcolor="#aaaadd" style="font:10pt verdana">
                  添加一个新记录:</td>
              </tr>
              <tr>
                <td nowrap>Name: </td>
                <td><asp:TextBox id="acode_display" runat="server" /></td>
                <td> <asp:RequiredFieldValidator runat=server 
                  controltovalidate=acode_display
                 errormessage="姓名不能为空.">*
                </asp:RequiredFieldValidator></td>
              </tr>
              <tr>
                <td nowrap>Group: </td>
                <td><asp:DropDownList DataTextField = "code_display"
                DataValueField = "code_value" id="acode_category" 
                  runat="server" /></td>
              </tr>
              <tr>
                <td nowrap>Opening Value: </td>
                <td><asp:TextBox id="aopening" value = "0" runat="server" /></td>
              </tr>
              <tr>
                <td style="padding-top:15">
                <asp:Button id="SubmitDetailsBtn" text="提交" onclick="add_Click" 
                  runat="server" />
                </td>
              </tr>
            </table>
          </asp:Panel>
          <!-------------插入逻辑 结束 -------->
        </td>
        </tr>
      </table>
    </form>
  </body>
</html>
