我觉得很奇怪:
为什么这段代码的的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();
        }
    }
}

解决方案 »

  1.   

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

  2.   

    asp.net控件就好像是1988年到2002的Winform一样,在一个原本无状态的web上实现了方便与状态编程,不需要你低级地写许多代码。
      

  3.   

    autopostback auto post back就是 自动 post 表单 提交。autopostback = true 当你在checkbox上单击鼠标之后,立马自动提交表单。autopostback = false当你在checkbox上单击鼠标之后,不会自动提交表单,等你按按钮的时候,在提交表单。区别在于啥时候提交表单,和checkbox有没有选择没有任何关系。
      

  4.   

    CheckBox控件的AutoPostBack一般是对protected void CheckBox1_CheckedChanged(object sender, EventArgs e)这个事件用的
      

  5.   


    瞎说。你可以试一下,随便拖一个Button,或者别的什么可以回发的东西,然后操作时同时修改了CheckBox的值,就会触发这个事件。
      

  6.   

    autopostback 只是表明是不是需要刷新页面,checked属性的值该是哪样还是哪样
      

  7.   

    如果想理解一点原理,可以看 msdn 上关于 IPostBackDataHandler 的说明。不过msdn写的比较八股、繁琐。如果可能,找一本比较好的“asp.net控件开发”方面的专著看看更好。
      

  8.   

    原来是我再page load 的时候没有用
    if(!ispostback){.......}
    所以页面一刷新,textbox的值就绑定为原来的