首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

为啥这段代码的的checkbox没有设置为autopostback=true却可以改变checkbox.checked属性

2013-11-13 
为什么这段代码的的checkbox没有设置为autopostbacktrue却可以改变checkbox.checked属性我觉得很奇怪:为

为什么这段代码的的checkbox没有设置为autopostback=true却可以改变checkbox.checked属性
我觉得很奇怪:
为什么这段代码的的checkbox没有设置为autopostback=true却可以改变checkbox.checked属性???
代码里面的 <asp:CheckBox ID="chkItem" runat="server"   /> 明明就没有autopostback,但为什么
后台的
 for (int i = 0; i < this.Repeater1.Items.Count; i++)
            {
                CheckBox cbox = (CheckBox)this.Repeater1.Items[i].FindControl("chkItem");
                if (cbox.Checked == true)
..................................             
}
却可以读出checked==true
好奇怪,求解释!!



/////////////////////前台



<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="PagedDataSourceTest.WebForm2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Repeater控件使用</title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="margin-bottom: 20px; text-align: center; width: 1006px;">
        Repeater控件使用</div>
    <asp:Repeater ID="Repeater1" runat="server">
        <HeaderTemplate>
            <table border="1" cellpadding="0" cellspacing="0" style="width: 1006px; border-collapse: collapse;
                text-align: center;">
                <tr>
                    <td style="background-color: #cccccc; font-weight: bold; height: 25px;">
                        选择
                    </td>
                    <td style="background-color: #cccccc; font-weight: bold; height: 25px;">
                        id隐藏
                    </td>
                    <td style="background-color: #cccccc; font-weight: bold; height: 25px;">
                        货物名
                    </td>
                    <td style="background-color: #cccccc; font-weight: bold; height: 25px;">
                        价格
                    </td>
                </tr>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td>
                    <asp:CheckBox ID="chkItem" runat="server"   />
                </td>
                <td style="display: none">
                    <asp:Label ID="lblID" Text='<%# DataBinder.Eval(Container.DataItem, "id")%>' runat="server"></asp:Label>
                </td>
                <td>
                    <%# DataBinder.Eval(Container.DataItem, "goodsName")%>


                </td>
                <td>
                    <%# DataBinder.Eval(Container.DataItem, "goodsPrice")%>
                </td>
            </tr>
        </ItemTemplate>
        <%--AlternatingItemTemplate描述交替输出行的另一种外观--%>
        <AlternatingItemTemplate>
            <tr bgcolor="#e8e8e8">
                <td>
                    <asp:CheckBox ID="chkItem" runat="server"   />
                </td>
                <td style="display: none">
                    <asp:Label ID="lblID" Text='<%# DataBinder.Eval(Container.DataItem, "id")%>' runat="server"></asp:Label>
                </td>
                <td>
                    <%# DataBinder.Eval(Container.DataItem, "goodsName")%>
                </td>
                <td>
                    <%# DataBinder.Eval(Container.DataItem, "goodsPrice")%>
                </td>
            </tr>
        </AlternatingItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
    </asp:Repeater>
    <div style="background-color: #dedede; width: 1006px;">
        <asp:HyperLink ID="HyperLinkPre" runat="server">上页</asp:HyperLink>
        <asp:HyperLink ID="HyperLinkNext" runat="server">下页</asp:HyperLink>第
        <asp:Label ID="Label1" runat="server"></asp:Label>
        页 共
        <asp:Label ID="lblTotalPage" runat="server"></asp:Label>页
    </div>
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="btnDel_Click" />
    </div>
    
    </form>
</body>
</html>










////////////////////////////////后台


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;

namespace PagedDataSourceTest
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        MyDatabase dbHelper = new MyDatabase();
        PagedDataSource PDS = new PagedDataSource();
        // private string ConnStr = ConfigurationManager.AppSettings["dbConnectionString"];
        private string strsql = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.bind();
            }
            


        }

         

        private void bind()
        {

            DataTable myTb = dbHelper.GetDataTable("select *  from  tbGoods");
            DataView dv = myTb.DefaultView;
            PDS.DataSource = dv;
            PDS.AllowPaging = true;
            PDS.PageSize = 5;
           

            /////新的分页
            lblTotalPage.Text = PDS.PageCount.ToString();//总页数
            int currentPage;//当前页
            if (Request.QueryString["Page"] != null)
            {
                currentPage = Convert.ToInt32(Request.QueryString["Page"]);
            }
            else//没有page参数的话就是第一页
            {
                currentPage = 1;
            }
            PDS.CurrentPageIndex = currentPage - 1;
            Label1.Text = currentPage.ToString();
            if (PDS.IsFirstPage == false)//不是第一页
            {
                HyperLinkPre.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" +
                    Convert.ToString(currentPage - 1);
            }
            if (PDS.IsLastPage == false)//不是最后一页
            {
                HyperLinkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" +
                    Convert.ToString(currentPage + 1);
            }
            //////////////////////////////////////

            Repeater1.DataSource = PDS;
            Repeater1.DataBind();
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            string ID = "";
            for (int i = 0; i < this.Repeater1.Items.Count; i++)
            {
                CheckBox cbox = (CheckBox)this.Repeater1.Items[i].FindControl("chkItem");
                if (cbox.Checked == true)
                {
                    if (ID == "")
                    {
                        ID = "'" + ((Label)this.Repeater1.Items[i].FindControl("lblID")).Text + "'";
                    }
                    else
                    {
                        ID += "," + "'" + ((Label)this.Repeater1.Items[i].FindControl("lblID")).Text + "'";


                    }
                }
            }
            strsql = "delete from tbGoods where id in (" + ID + ")";
            try
            {
                dbHelper.ExcuteSql(strsql);
                System.Web.HttpContext.Current.Response.Write("<script language='javascript'>alert('刪除成功!');</script>");
            }
            catch (System.Data.SqlClient.SqlException E)
            {
                throw new Exception(E.Message);
            }
            finally
            {

            }
            this.bind();
        }
    }
}





[解决办法]
asp.net服务器控件,只要回发就可以正确地自动更新状态。跟autopostback没有关系。你在页面上随便拖入一个Button造成页面会发,就会看到CheckBox的状态正确地改变了,而且还能出发CheckBox的改变事件。

热点排行