简体   繁体   English

ASP.NET控件后面的代码未执行

[英]ASP.NET controls code behind is not executing

I'm displaying JQuery Modal Window in ASP.NET . 我在ASP.NET显示JQuery Modal Window I'm using the modal windows as registration form. 我正在使用模式窗口作为注册表格。 I'm using .NET controls such as checkbox and buttons . 我正在使用.NET控件,例如checkboxbuttons However, the issue I'm running into is that even though I have codebehind for both controls, it never reaches the code behind. 但是,我遇到的问题是,即使两个控件都有代码codebehind ,但也永远不会到达后面的代码。 For example, I have a Check All checkbox called checkAll . 例如,我有一个名为checkAll的Check All复选框。 In the code behind I have the following code to check all: 在后面的代码中,我有以下代码可用于全部检查:

protected void checkAll_CheckedChanged(object sender, EventArgs e)
    {
        if (checkAll.Checked == true)
        {
            foreach (ListItem li in cbList.Items)
            {
                li.Selected = true;
            }
        }
        else
        {
            foreach (ListItem li in cbList.Items)
            {
                li.Selected = false;
            }
        }
    }

However, when I debug and put stop in the first if statement it never comes there. 但是,当我调试并在第一个if语句中放置stop时,它永远不会出现。

Also, in the addNewUser_Click event of the button I would like to load the checkboxList which I now do in Page_Load . 另外,在按钮的addNewUser_Click事件中,我想加载checkboxList ,该操作现在在Page_Load However, when I put the same code in the code behind of a button it never loads. 但是,当我将相同的代码放在按钮后面的代码中时,它永远不会加载。

Here's the aspx code: 这是aspx代码:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master"  AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TAP._Default" %>
<%@ Register Assembly="Trirand.Web" Namespace="Trirand.Web.UI.WebControls"   TagPrefix="cc1" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
 <header>

 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
 <script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/jquery-ui.js" type="text/javascript"></script>
 <link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/themes/start/jquery-ui.css"
rel="stylesheet" type="text/css" />
<script type="text/javascript">
       $("[id*=addNewUser]").live("click", function () {
    $("#adduser").dialog({
        title: "Add User",
        resizable: false,
        buttons: {
            Save: function () {
                $(this).dialog('save');
            },
            Cancel: function () {
                $(this).dialog('close');
            }
        }
    });
    return false;
});
</script>
</header>
<br />
 <asp:Label ID="lblNoRole" runat="server" Text="No role has been assigned yet." Visible="False" CssClass="alert-link"></asp:Label>
<div id="clientManager" runat="server">
 <div class="text">Clients: <asp:DropDownList ID="ddclients" runat="server" BackColor="#F6F1DB" ForeColor="#7d6754" CssClass="ddl" Height="30px"></asp:DropDownList> <asp:Button ID="addNewUser" runat="server" Text="Add user" CssClass="btn-danger" Height="25px" Width="98px" OnClick="addNewUser_Click" />
     <div id="adduser" style="display: none">
     First Name:
         <asp:TextBox ID="firstName" runat="server"></asp:TextBox><br />
     Last Name:
         <asp:TextBox ID="lastName" runat="server"></asp:TextBox><br />
     Email address:
         <asp:TextBox ID="email" runat="server" TextMode="Email">  </asp:TextBox><br />
Phone Number:
         <asp:TextBox ID="phone" runat="server" TextMode="Phone"></asp:TextBox>
Password:
         <asp:TextBox ID="pwd" runat="server" TextMode="Password"></asp:TextBox><br />
Confirm password:
         <asp:TextBox ID="pwd2" runat="server" TextMode="Password"></asp:TextBox><br />
Client(s): Select all <asp:CheckBox ID="checkAll" runat="server" OnCheckedChanged="checkAll_CheckedChanged" /><br /><br />
         <asp:Panel ID="Panel1" runat="server" ScrollBars="Auto" Height="150px"> <asp:CheckBoxList ID="cbList" runat="server" Font-Size="10px" ></asp:CheckBoxList></asp:Panel>

 </div>
 </div>
</div><hr id="h2" runat="server" />
<div id="clientUser" runat="server">
    <div class="text">Client User DIV</div>
</div><hr id="h1" runat="server" />
<div id="administrator" runat="server">
    <div class="text">Administrator DIV</div>
</div>

Please note that the JavaScript function only shows the modal window right now. 请注意, JavaScript函数现在仅显示模式窗口。 It doesn't do anything else. 它什么也没做。

Any suggestions on why I can't get to the code behind of the asp.net controls? 关于为什么我无法获取asp.net控件背后的代码的任何建议?

Thanks 谢谢

The checkbox doesn't automatically cause a postback when the value is changed, therefore your server side code won't be reached. 更改值时,该复选框不会自动导致回发,因此将无法访问服务器端代码。 You can make it initiate a postback when the value is changed by setting the AutoPostBack property to true. 您可以通过将AutoPostBack属性设置为true来使它在值更改时启动回发。

However I do not recommend you do this. 但是,我建议您这样做。 Think about it, why should you involve the server just to check some checkboxes on the client? 考虑一下,为什么要让服务器只检查客户机上的某些复选框? Instead, you should use some JavaScript to handle it on the client. 相反,您应该使用一些JavaScript在客户端上进行处理。 jQuery is quite capable of that. jQuery非常有能力做到这一点。

$('#checkAll').change(function(){
    //use jQuery to change the checkbox values here
});

Make sure you sent the ClientIdMode="static" on `checkAll' so that the ID on the client will match the ID on the server. 确保在“ checkAll ClientIdMode="static"上发送了ClientIdMode="static" ,以便客户端上的ID与服务器上的ID匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM