Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# asp.net中继器控件中的复选框不起作用_C#_Asp.net_Checkbox - Fatal编程技术网

C# asp.net中继器控件中的复选框不起作用

C# asp.net中继器控件中的复选框不起作用,c#,asp.net,checkbox,C#,Asp.net,Checkbox,我在Repeater控件中有一个复选框列,即使我在第一次加载页面时只使用了一次绑定,它也不会维护该状态。每次它返回Checked=True时,即使我取消选中它。这是代码 这是.aspx代码 <asp:Repeater ID="rptCols" runat="server" onitemdatabound="rptCols_ItemDataBound" > <HeaderTemplate> <table width="200px" bord

我在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>
我想使用此按钮事件获取复选框的当前状态,该事件不起作用,第一次加载页面时返回相同的状态:

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

}

有人能帮忙吗?

我就是这样解决这个问题的:

以下是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;
    }

删除了我的另一个答案,因为问题出在别处,我怀疑这可能是因为您的中继器在回发时被重新标记。尝试从中继器中的复选框中删除Eval,只是为了测试,然后查看在选中和取消选中复选框时,是否可以从复选框中获得正确的状态。您可以不使用Eval,而是将OnItemDataBound中的值绑定到codebehindI中。从rptCols_ItemDataBound事件中删除Eval并将值分配给每个字段。问题依旧。当我取消选中并执行回发时,当我在button click事件中循环查看每个项目时,该值不会反映在回发中。您是否尝试过在回发/页面加载中中断并单步执行,并在此时检查中继器的值?您还可以检查Request.Form以查看实际发布到服务器的内容-通过浏览器工具或page_init/pageload。Yes尝试了Request.Form,也没有运气。表单对象中的正确值是否实际发送到服务器?
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;
    }