asp:复选框总是发送false

时间:2015-07-11 19:38:54

标签: asp.net vb.net checkbox

我有以下问题,我想通过使用asp:复选框禁用或启用我的asp窗体中的控件但是当我单击复选框将选中状态更改为true或false时,它将始终发送&#34假#34;

  <%@ Page Language="VB" MasterPageFile="~/Site.master" AutoEventWireup="false" CodeFile="Registration.aspx.vb" Inherits="Default2" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <style type="text/css">
        .auto-style2 {
            text-align: left;
        }
        .auto-style3 {
            font-family: "Times New Roman", Times, serif;
        }
        .auto-style4 {
            font-family: "Times New Roman", Times, serif;
            font-size: medium;
        }
        .auto-style5 {
            font-family: "Times New Roman", Times, serif;
            font-weight: bold;
            font-size: x-large;
        }
        .auto-style6 {
            font-size: medium;
        }
        .auto-style7 {
            font-family: "Times New Roman", Times, serif;
            font-size: x-large;
            text-transform: capitalize;
            border-style: outset;
            border-width: 3px;
        }
        .auto-style8 {
            width: 351px;
        }
        .auto-style9 {
            width: 344px;
        }
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">



     <div id="registration" class="auto-style2">
          <asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>
          <br />
          <table>
              <tr>
                  <td>Employees in the database:</td>
                  <td>

                    <asp:UpdatePanel ID="upddlEmployee" runat="server">
                    <ContentTemplate>
                       <asp:DropDownList 
                           ID="drpEmployee" 
                           runat="server" 
                           Height="30px" 
                           Width="175px" 
                           BackColor="White" 
                           CssClass="auto-style2" 
                           ForeColor="#000099"
                           AutoPostBack="true">
                        </asp:DropDownList>
                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID ="drpEmployee" EventName="SelectedIndexChanged" />
                    </Triggers>
                </asp:UpdatePanel>


                  </td>
              </tr>

          </table>
          <br />
          <br />
          <table>
              <tr>
                  <td class="auto-style8">

        <asp:Label ID="Label1" runat="server" Text="Employee ID:*" Width="300px" 
            Font-Bold="True" CssClass="auto-style7" Height="31px" BorderStyle="None"></asp:Label>
                  </td>
                  <td class="auto-style9">
                      <asp:UpdatePanel ID="UpdatePanel5" runat="server">
                          <ContentTemplate>
                              <asp:TextBox ID="txtEmployee_id" runat="server" CssClass="auto-style5" Height="30px" Width="200px" AutoPostBack ="true" OnTextChanged ="txtEmployeeUser_id_TextChanged"></asp:TextBox>
                          </ContentTemplate>
                      </asp:UpdatePanel>
                  </td>
                  <td>
                      <asp:UpdatePanel ID="UpdatePanel7" runat="server">
                          <ContentTemplate>
                              <span class="auto-style6">
                              <asp:label 
                                  ID="lblEmployeeID" 
                                  runat="server" 
                                  Text =""
                                  InitialValue="" Width="229px"></asp:label>
                              </span>
                          </ContentTemplate>
                      </asp:UpdatePanel>
                  </td>
              </tr>
              <tr>
                  <td class="auto-style8"><span class="auto-style6">
          <asp:Label ID="Label4" runat="server" Text="First Name:*" Width="300px" 
            Font-Bold="True" CssClass="auto-style7" height="30px" BorderStyle="None"></asp:Label>
         </span>
                  </td>
                  <td class="auto-style9">
        <asp:TextBox ID="txtFirstName" runat="server" Width="200px" CssClass="auto-style5" height="30px"></asp:TextBox>
                  </td>
                  <td>
         <span class="auto-style6">
        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
            ControlToValidate="txtFirstName" ErrorMessage="First Name Required" CssClass="auto-style3" Width="300px"></asp:RequiredFieldValidator>
         </span>
                  </td>
              </tr>
              <tr>
                  <td class="auto-style8">

          <span class="auto-style6">

        <asp:Label ID="Label5" runat="server" Text=" Last Name:*" Width="300px" 
            Font-Bold="True" CssClass="auto-style7" height="30px" BorderStyle="None"></asp:Label>
         </span>
                  </td>
                  <td class="auto-style9">
        <asp:TextBox ID="txtLastName" runat="server" Width="200px" CssClass="auto-style5" height="30px"></asp:TextBox>
                  </td>
                  <td>
         <span class="auto-style6">
        <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" 
        ControlToValidate="txtLastName" ErrorMessage="Last Name Required" CssClass="auto-style3" Width="300px"></asp:RequiredFieldValidator>  
         </span>
                  </td>
              </tr>
              <tr>
                  <td class="auto-style8">

          <span class="auto-style6">


              <asp:Label ID="lblUsername" runat="server" Text=" User Name:*" Width="300px" 
            Font-Bold="True" CssClass="auto-style7" height="30px" BorderStyle="None"></asp:Label>
         </span></td>
                  <td class="auto-style9">
                      <asp:UpdatePanel ID="UpdatePanel6" runat="server">
                          <ContentTemplate>
                              <asp:TextBox ID="txtUserName" runat="server" CssClass="auto-style5" height="30px" Width="200px" AutoPostBack ="True" OnTextChanged="txtEmployeeUser_id_TextChanged"></asp:TextBox>
                          </ContentTemplate>
                          <Triggers>
                              <asp:AsyncPostBackTrigger ControlID="txtUserName" EventName="TextChanged" />
                          </Triggers>
                      </asp:UpdatePanel>
                  </td>
                  <td>
         <span class="auto-style6">
                              <asp:label 
                                  ID="lblUsernameCheck" 
                                  runat="server" 
                                  Text =""
                                  InitialValue="" Width="229px"></asp:label>
         </span>
                  </td>
              </tr>
              <tr>
                  <td class="auto-style8">
         <span class="auto-style6">
        <asp:Label ID="Label6" runat="server" Text="Regular Time:" Width="300px" 
            Font-Bold="True" CssClass="auto-style7" height="30px" BorderStyle="None"></asp:Label>
         </span>
                  </td>
                  <td class="auto-style9">
                      <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                          <ContentTemplate>
                              <asp:TextBox ID="txtRegularTime" runat="server" CssClass="auto-style5" height="30px" TextMode="Number" Width="200px" AutoPostBack ="True" OnTextChanged ="txtRegularTime_TextChanged"></asp:TextBox>
                          </ContentTemplate>
                          <Triggers>
                              <asp:AsyncPostBackTrigger ControlID="txtRegularTime" EventName="TextChanged" />
                          </Triggers>
                      </asp:UpdatePanel>
                  </td>
                  <td>&nbsp;</td>
              </tr>
              <tr>
                  <td class="auto-style8">
         <span class="auto-style6">
        <asp:Label ID="Label7" runat="server" Text="Double Time:" Width="300px" 
            Font-Bold="True" CssClass="auto-style7" height="30px" BorderStyle="None"></asp:Label>
         </span>
                  </td>
                  <td class="auto-style9">
                      <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                          <ContentTemplate>
                              <asp:TextBox ID="txtDoubleTime" runat="server" CssClass="auto-style5" height="30px" TextMode="Number" Width="200px"></asp:TextBox>
                          </ContentTemplate>
                      </asp:UpdatePanel>
                  </td>
                  <td>&nbsp;</td>
              </tr>
              <tr>
                  <td class="auto-style8">
         <span class="auto-style6">
        <asp:Label ID="Label8" runat="server" Text="Part Time:" Width="300px" 
            Font-Bold="True" CssClass="auto-style7" height="30px" BorderStyle="None"></asp:Label>
         </span>
                  </td>
                  <td class="auto-style9">
                      <asp:UpdatePanel ID="UpdatePanel3" runat="server">
                          <ContentTemplate>
                              <asp:TextBox ID="txtPartTime" runat="server" Width="200px" TextMode="Number" CssClass="auto-style5" height="30px" AutoPostBack ="True" OnTextChanged ="txtPartTime_TextChanged"></asp:TextBox>
                          </ContentTemplate>
                          <Triggers>
                              <asp:AsyncPostBackTrigger ControlID="txtPartTime" EventName="TextChanged" />
                          </Triggers>
                      </asp:UpdatePanel>
                  </td>
                  <td>&nbsp;</td>
              </tr>
              <tr>
                  <td class="auto-style8">
         <span class="auto-style6">
        <asp:Label ID="Label9" runat="server" Text="Over time:" Width="300px" 
            Font-Bold="True" CssClass="auto-style7" height="30px" BorderStyle="None"></asp:Label>
         </span>
                  </td>
                  <td class="auto-style9">
                      <asp:UpdatePanel ID="UpdatePanel4" runat="server">
                          <ContentTemplate>
                              <asp:TextBox ID="txtOverTime" runat="server" Width="200px" TextMode="Number" CssClass="auto-style5" height="30px"></asp:TextBox>
                          </ContentTemplate>
                      </asp:UpdatePanel>
                  </td>
                  <td>&nbsp;</td>
              </tr>
              <tr>
                  <td class="auto-style8">
                    <asp:Label ID="Label2" runat="server" Text=" Password:*" Width="231px" Font-Bold="True" CssClass="auto-style7" height="30px" BorderStyle="None"></asp:Label>


                      <asp:UpdatePanel ID="UpdatePanel8" runat="server">
                        <ContentTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="chkChecked"  />
                        </ContentTemplate>
                        <Triggers>
                            <asp:AsyncPostBackTrigger EventName="CheckedChanged" ControlID ="CheckBox1" />
                        </Triggers>
                      </asp:UpdatePanel>

                 </td>

                  <td class="auto-style9">

                      <asp:UpdatePanel ID="UpdatePanel9" runat="server">
                          <ContentTemplate>
                               <asp:TextBox ID="txtPassword" runat="server" Width="200px" TextMode="Password" CssClass="auto-style5" height="30px"></asp:TextBox>
                          </ContentTemplate>
                      </asp:UpdatePanel>

                  </td>
                  <td>
         <span class="auto-style6">
             <asp:CustomValidator ID="txtpasswordValidator" runat="server" 
                 ControlToValidate="txtPassword" ErrorMessage="Password Required" CssClass="auto-style3" Width="300px"
                 OnServerValidate="txtpassword_ServerValidate" >
             </asp:CustomValidator>
         </span></td>
              </tr>
              <tr>
                  <td class="auto-style8">
         <span class="auto-style6">
        <asp:Label ID="Label3" runat="server" Text=" Confirm Password:*" Width="300px" 
            Font-Bold="True" CssClass="auto-style7" height="30px" BorderStyle="None"></asp:Label>
         </span>
                  </td>
                  <td class="auto-style9">
                                            <asp:UpdatePanel ID="UpdatePanel10" runat="server">
                          <ContentTemplate>
                                <asp:TextBox ID="txtConfirmpassword" runat="server" Width="200px" TextMode="Password" CssClass="auto-style5" height="30px"></asp:TextBox> 
                          </ContentTemplate>
                      </asp:UpdatePanel>

                  </td>
                  <td> 
         <span class="auto-style6"> 
        <asp:CustomValidator 
            ID="CustomValidator1" 
            runat="server" 
            ControlToValidate="txtConfirmpassword"
            ErrorMessage="Password mismatch" 
            OnServerValidate="txtConfirmpassword_ServerValidate"
            CssClass="auto-style3" 
            Width="300px"></asp:CustomValidator>
         </span></td>
              </tr>
              <tr>
                  <td class="auto-style8">


                      <asp:Label ID="Label10" runat="server" Text="Label" Visible="False"></asp:Label>
                  </td>
                  <td class="auto-style9">&nbsp;</td>
                  <td>&nbsp;</td>
              </tr>
              <tr>
                  <td class="auto-style8">&nbsp;</td>
                  <td class="auto-style9">&nbsp;</td>
                  <td>&nbsp;</td>
              </tr>
          </table>
         <br />

          <span class="auto-style6">
             &nbsp;<br class="auto-style3" />
         </span><span class="auto-style4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="auto-style6">
        <br class="auto-style3" />



        <Table runat="server" >
            <tr>
                <td>
                    <asp:Button 
                        ID="btnConfirmPassword" 
                        runat="server" 
                        Text="Confirm Registration" 
                        onclick="Button1_Click" 
                        Font-Bold="True" 
                        Font-Size="X-Large" 
                        Height="50px"
                        Width="250px" />
                </td>
                <td>
                    <asp:Button 
                        ID="btnClear" 
                        runat="server" 
                        Text="ClearData" 
                        onclick="btnClear_Click" 
                        Font-Bold="True" 
                        Font-Size="X-Large" 
                        Height="50px"
                        Width="250px" />
                </td>
            </tr>
        </Table>



             </span>


    </div> 

</asp:Content>

以及asp发出的代码如下:

   Imports System.Data
Imports System.Configuration
Imports MySql.Data.MySqlClient

Partial Class Default2
    Inherits System.Web.UI.Page
    Dim gclass As New GlobalClass
    Dim employeeData As New Employee
    Dim intEmployee() As Integer
    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles btnConfirmPassword.Click
        If drpEmployee.SelectedIndex = -1 Then
            ExeQuery("I")
        Else
            ExeQuery("U")
        End If
        clearFields()
    End Sub

    Sub ExeQuery(strMode As String)
        If txtRegularTime.Text <> "" Then
            Dim query As String = gclass.SPEmployeeCall(strMode, 1, txtUserName.Text, txtPassword.Text, txtConfirmpassword.Text, _
                                                txtEmployee_id.Text, txtFirstName.Text, txtLastName.Text, _
                                                 Convert.ToDouble(txtOverTime.Text), Convert.ToDouble(txtDoubleTime.Text), _
                                                 Convert.ToDouble(txtPartTime.Text), Convert.ToDouble(txtRegularTime.Text))
            gclass.ExecuteSP(query)
        End If
    End Sub



    Protected Sub txtRegularTime_TextChanged(sender As Object, e As EventArgs) Handles txtRegularTime.TextChanged
        If Convert.ToDecimal(txtRegularTime.Text) > 0 Then
            txtDoubleTime.Text = Convert.ToDecimal(txtRegularTime.Text * 2)
            txtOverTime.Text = Convert.ToDecimal(txtRegularTime.Text * 1.5)
            txtPartTime.Text = 0
        End If
        txtPassword.Focus()
    End Sub

    Protected Sub txtPartTime_TextChanged(sender As Object, e As EventArgs) Handles txtPartTime.TextChanged
        If Convert.ToDecimal(txtPartTime.Text) > 0 Then
            txtDoubleTime.Text = 0
            txtOverTime.Text = 0
            txtRegularTime.Text = 0
        End If
        txtPassword.Focus()
    End Sub
    Sub clearFields()
        drpEmployee.SelectedIndex = 0
        checkboxDRPemployee()
        txtEmployee_id.Text = Nothing
        txtFirstName.Text = Nothing
        txtLastName.Text = Nothing
        txtUserName.Text = Nothing
        txtDoubleTime.Text = Nothing
        txtOverTime.Text = Nothing
        txtRegularTime.Text = Nothing
        txtPartTime.Text = Nothing
    End Sub

    Sub clearFields(dt As DataTable)
        txtEmployee_id.Text = dt.Rows(0)(1)
        txtFirstName.Text = dt.Rows(0)(2)
        txtLastName.Text = dt.Rows(0)(3)
        txtUserName.Text = dt.Rows(0)(8)
        txtDoubleTime.Text = dt.Rows(0)(5)
        txtOverTime.Text = dt.Rows(0)(4)
        txtRegularTime.Text = dt.Rows(0)(7)
        txtPartTime.Text = dt.Rows(0)(6)
        txtPassword.Text = dt.Rows(0)(9)
        txtConfirmpassword.Text = dt.Rows(0)(10)
    End Sub




    Protected Sub txtEmployeeUser_id_TextChanged(sender As Object, e As EventArgs) Handles txtEmployee_id.TextChanged, txtUserName.TextChanged
        Dim intMode As Integer

        If sender Is txtEmployee_id Then
            intMode = 2
        ElseIf sender Is txtUserName Then
            intMode = 3
        End If

        Dim query As String = gclass.SPEmployeeCall("Q", intMode, txtUserName.Text, Nothing, Nothing, _
                                                    txtEmployee_id.Text, Nothing, Nothing, _
                                                    Nothing, Nothing, _
                                                    Nothing, Nothing)
        lblEmployeeID.Visible = True
        lblEmployeeID.Text = gclass.DatabaseMessages(query)
    End Sub



    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Session.Contents("Employee") Is Nothing Then
            Response.Redirect("Login.aspx")
        Else
            employeeData = Session("Employee")
        End If
        If Not IsPostBack And drpEmployee.SelectedIndex = -1 Then
            Session.Contents("EmployeeDDL") = gclass.LoadEmployeeAbsent(drpEmployee, 99)
        End If
        checkboxDRPemployee()
    End Sub


    Protected Sub drpEmployee_SelectedIndexChanged(sender As Object, e As EventArgs) Handles drpEmployee.SelectedIndexChanged
        intEmployee = Session.Contents("EmployeeDDL")
        If drpEmployee.SelectedIndex <> 0 Then
            Dim query As String = gclass.SPEmployeeCall("Q", 7, Nothing, Nothing, Nothing, intEmployee(drpEmployee.SelectedIndex), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing)
            Dim dt As DataTable = gclass.Mysql_CreateDataTable(query)
            clearFields(dt)
        Else
            clearFields()
        End If



    End Sub

    Sub checkboxDRPemployee()
        If drpEmployee.SelectedIndex <> 0 Then
            CheckBox1.Enabled = True
            CheckBox1.Checked = False
        Else
            CheckBox1.Enabled = False
            CheckBox1.Checked = False
        End If
    End Sub

    Protected Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        clearFields()
    End Sub

    Protected Sub txtpassword_ServerValidate(source As Object, args As ServerValidateEventArgs)
        If Not CheckBox1.Checked Then
            args.IsValid = (txtPassword.Text <> "")
        End If
    End Sub

    Protected Sub txtConfirmpassword_ServerValidate(source As Object, args As ServerValidateEventArgs)
        If Not CheckBox1.Checked Then
            args.IsValid = (txtPassword.Text = txtConfirmpassword.Text)
        End If
    End Sub

    Protected Sub chkChecked(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
        Dim thisValue As Boolean = CheckBox1.Checked


        txtPassword.Enabled = CheckBox1.Checked
        txtConfirmpassword.Enabled = CheckBox1.Checked
    End Sub

End Class

提前谢谢你 最亲切的问候 Douglas Leekam

2 个答案:

答案 0 :(得分:0)

这肯定与AutoPostBack="true"

有关

尝试通过更改为:

进行调试
<asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="chkChecked"  />

不确定代码的功能是什么,但您可能希望将页面加载代码包装在

 If Not Page.IsPostBack Then
// code currently in page load
 End If

答案 1 :(得分:0)

您可以使用javascript检查复选框是否已根据您可以从javascript本身禁用控件的值进行选中。您可以使用javascript onchange事件来检查复选框值。