asp.net中的Repeater控件中的复选框无效

时间:2014-09-17 06:03:25

标签: c# asp.net checkbox

我在Repeater控件中有一个复选框列,即使我第一次加载页面时只使用了一次绑定,也没有保持状态。每次返回Checked = True,即使我取消选中它。这是代码。

这是.aspx代码

<asp:Repeater ID="rptCols" runat="server" onitemdatabound="rptCols_ItemDataBound" >     
   <HeaderTemplate>
      <table width="200px" border="1" id="tblRpt" style="background-color:White;border-color:#CCCCCC;border-width:1px;border-style:None;font-family:Arial;font-size:X-Small;width:180px;border-collapse:collapse;">
          <tr>
             <th>COLUMN</th>
             <th colspan="2">&nbsp;</th>
          </tr>
  </HeaderTemplate>
  <ItemTemplate>
      <tr runat="server" id="tblRow">
         <td><asp:Label id="lblCol" runat="server" Text= '<%# Eval("Col1")%>'></asp:Label> </td>        
         <td><asp:CheckBox id="chkSelect" runat="server" Checked= '<%# Convert.ToBoolean(Eval("Col2")) %>'/></td>
         <td style="visibility:hidden"><asp:Label ID ="lblHidCol" runat="server" Text='<%# Eval("Col3") %>' /></td>
     </tr>
  </ItemTemplate>
  <FooterTemplate>
      </table>
  </FooterTemplate>
</asp:Repeater>

这是

背后的aspx.cs代码
protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        if (!Page.IsPostBack)
        {
            FillColsGrid(false); //Binding only once..
        }

    }
    catch (Exception ex)
    {
        this.lblException.Text = ex.Message.ToString() ;
    }
}

我希望使用此按钮事件获取复选框的当前状态,该事件不起作用,在第一次加载页面时返回相同的内容:

protected void wibHideShow_Click(object sender, EventArgs e)
{
    List<UserSettings> lstColsGrid = new List<UserSettings>();

    string sUser = Session["UserId"].ToString();
    foreach (RepeaterItem item in rptCols.Items)
    {
        if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
        {
            UserSettings us = new UserSettings();
            Label lblColName = item.FindControl("lblCol") as Label;
            CheckBox chkSel = item.FindControl("chkSelect") as CheckBox; //Here is the issue
            Label hidAppName = item.FindControl("lblHidCol") as Label;

            us.BemsId = sUser;
            us.ApplicationName = hidAppName.Text;
            us.ColumnName = lblColName.Text;
            us.ColumnValue = chkSel.Checked.ToString();
            us.LastUpdateDate = System.DateTime.Now;
            lstColsGrid.Add(us);
        }
    }
    rptCols.DataSource = LoadColsDataSet(lstColsGrid);
    rptCols.DataBind();

}

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

所以这就是我解决这个问题的方法:

以下是ASPX页面中的转发器:

<div style="overflow: auto;height:140px" id="DivMainContent">  
  <asp:Repeater ID="rptCols" runat="server" OnItemDataBound="rptCols_ItemDataBound"> 
     <HeaderTemplate>
         <table id="tblRpt" style="background-color:White;border-color:#CCCCCC;border-width:1px;border-style:None;font-family:Arial;font-size:X-Small;width:180px;border-collapse:collapse;">
            <tr class="GridHeader" style="height:15px;">
                <th>COLUMN</th>
                <th colspan="2"></th>
            </tr>
     </HeaderTemplate>
     <ItemTemplate>
        <tr runat="server" id="tblRow">
           <td><asp:Label id="lblCol" runat="server"></asp:Label> </td>
           <td><asp:CheckBox id="chkSelect" runat="server"/></td>
           <td style="display:none"><asp:Label ID ="lblHidCol" runat="server"/></td>
        </tr>
     </ItemTemplate>
     <FooterTemplate>
        </table>
     </FooterTemplate>
  </asp:Repeater>
</div>
<div id="DivFooterRow" style="overflow: hidden; display: none;">
     <input type="hidden" id="hidChk" runat="server" />
</div>
<br />
<asp:Button ID="wibHideShow" Font-Names="Arial" Font-Size="XX-Small" runat="server"
     ToolTip="Save Columns to Hide or Show" Text="Hide/Show" OnClick="wibHideShow_Click"  />
<br />

在页面末尾,写下这个jquery。

<script type="text/javascript">
function showValues() {
    var str = $("#DivMainContent :input").serialize();
    $("#hidChk").val(str); //hidden field
}
$("input[type='checkbox']").on("click", showValues);
showValues();

ASPX.CS代码

int iChk = 1;
protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        if (!Page.IsPostBack)
        {
             this.rptCols.DataSource = LoadColsDataSet(); //load data from database
             this.rptCols.DataBind();
        }
    }
    catch (Exception ex)
    {
        //display error in some label
    }
}
protected void rptCols_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    try
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DataRowView drv = (DataRowView)e.Item.DataItem;
            Label lblCol = e.Item.FindControl("lblCol") as Label;
            CheckBox chkCol = e.Item.FindControl("chkSelect") as CheckBox;
            Label lblHidc = e.Item.FindControl("lblHidCol") as Label;

            lblCol.Text = drv.Row["DBCol1"].ToString();
            chkCol.Checked = Convert.ToBoolean(drv.Row["DBCol2"].ToString());
            lblHidc.Text = drv.Row["DBCol3"].ToString();
        }
    }
    catch (Exception ex)
    {
        this.lblException.Text = ex.Message.ToString();
    }
}

protected void wibHideShow_Click(object sender, EventArgs e)
{
    List<UserSettings> lstColsGrid = new List<UserSettings>();
    iChk = 0;
    string sUser = Session["BEMSID"].ToString();
    ArrayList ar = GetChecked();
    foreach (RepeaterItem item in rptCols.Items)
    {
        UserSettings us = new UserSettings();
        Label lblColName = item.FindControl("lblCol") as Label;
        Label hidAppName = item.FindControl("lblHidCol") as Label;

         us.BemsId = sUser;
         us.ApplicationName = hidAppName.Text;
         us.ColumnName = lblColName.Text;
         us.ColumnValue = ar[iChk].ToString();
         us.LastUpdateDate = System.DateTime.Now;
         iChk++;
         lstColsGrid.Add(us);
     }
     rptCols.DataSource = LoadColsDataSet(lstColsGrid); //Update and load data
     rptCols.DataBind();
     iChk = 1;
 }

    private ArrayList GetChecked()
    {
        ArrayList ar = new ArrayList();
        int chkNo = 0;
        string strChkNo = string.Empty;
        string[] strChk = hidChk.Value.Split('&');

        //initialize array with all false
        for (int i = 0; i < rptCols.Items.Count; i++)
        {
            ar.Add("false");
        }
        for (int i = 0; i < strChk.Length; i++)
        {
            strChkNo = strChk[i].Replace("%3A", ":");
            if (strChkNo != "")
            {
                chkNo = Convert.ToInt16(Regex.Match(strChkNo, @"\d+").Value); //Extract int from string
                ar[--chkNo] = "true";
            }
        }

        return ar;
    }