none
为什么checkbox的autopostback没有设置为true也可以改变checked属性??? RRS feed

  • 问题

  • <%@ 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>
    


      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();
            }

    我在网上看了下载了一段代码可以正常运行,但是我不明白为什么checkbox的autopostback没有设置为true,但是删除函数却可以得到checkbox.checked改变之后的值,为什么???

    2013年11月9日 3:29

答案

  • 楼主您好:

    这和AutoPostBack毫无关系。AutoPostBack设置为true表示一旦你改变了现有的CheckBox状态,页面将被重新PostBack一次后重新加载。而CheckBox本质是一个<input type="checkbox"……/>页面往返本无记忆性,是微软内部通过ViewState(本质是一个HiddenField)记录每个控件的真实值往返重新赋值决定的。当然删除函数可以获取Checked内容。

    你自己看CheckBox反射后的源代码:

     [Bindable(true, BindingDirection.TwoWay)]
            [DefaultValue(false)]
            [Themeable(false)]
            [WebSysDescription("CheckBox_Checked")]
            public virtual bool Checked
            {
                get
                {
                    object item = this.ViewState["Checked"];
                    if (item == null)
                    {
                        return false;
                    }
                    return (bool)item;
                }
                set
                {
                    this.ViewState["Checked"] = value;
                }
            }

    For Account Validation, please follow "Verify Account+Number" at http://social.msdn.microsoft.com/Forums/en-us/home?forum=reportabug

    For ASP.NET Question, please ask at http://forums.asp.net

    For other questions, you can find a specific forum and then ask at http://stackexchange.com/sites

    Click and Donate at http://www.freerice.com

    2013年11月9日 12:22

全部回复

  • 楼主您好:

    这和AutoPostBack毫无关系。AutoPostBack设置为true表示一旦你改变了现有的CheckBox状态,页面将被重新PostBack一次后重新加载。而CheckBox本质是一个<input type="checkbox"……/>页面往返本无记忆性,是微软内部通过ViewState(本质是一个HiddenField)记录每个控件的真实值往返重新赋值决定的。当然删除函数可以获取Checked内容。

    你自己看CheckBox反射后的源代码:

     [Bindable(true, BindingDirection.TwoWay)]
            [DefaultValue(false)]
            [Themeable(false)]
            [WebSysDescription("CheckBox_Checked")]
            public virtual bool Checked
            {
                get
                {
                    object item = this.ViewState["Checked"];
                    if (item == null)
                    {
                        return false;
                    }
                    return (bool)item;
                }
                set
                {
                    this.ViewState["Checked"] = value;
                }
            }

    For Account Validation, please follow "Verify Account+Number" at http://social.msdn.microsoft.com/Forums/en-us/home?forum=reportabug

    For ASP.NET Question, please ask at http://forums.asp.net

    For other questions, you can find a specific forum and then ask at http://stackexchange.com/sites

    Click and Donate at http://www.freerice.com

    2013年11月9日 12:22
  • 谢谢,这下子清楚了
    2013年11月10日 6:47