Asp.net/C#通过复选框获取中继器行数据

本文关键字:获取 中继器 数据 复选框 net Asp | 更新日期: 2023-09-27 18:17:12

你好,我有一个重复器,将数据作为每一行的复选框表,所以我想显示选中的行数据时,单击一个按钮。

。aspx代码:

  <asp:Repeater ID="rptItems" runat="server">
        <HeaderTemplate>
            <table class="table table-bordered table-hover table-responsive table-striped table-condensed">
                <tr>
                    <th> </th>
                    <th>Goods Desc</th>
                    <th>Balance Units</th>
                    <th>Exit Units</th>
                </tr>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td><asp:CheckBox ID="cbItem" runat="server" /></td>                    
                <td><%#Eval("ItemDesc") %></td>
                <td><%#Eval("InvoicBalanceUnits") %></td>
                <td><asp:TextBox ID="txtExitUnits" runat="server" ></asp:TextBox>
                    <asp:RegularExpressionValidator ID="revUnits" runat="server" Display="Dynamic" ControlToValidate="txtExitUnits" ValidationExpression="^'d+$" ErrorMessage="Please, insert a number." CssClass="text-danger"></asp:RegularExpressionValidator>
                    <asp:RequiredFieldValidator ID="rfvUnits" runat="server" Display="Dynamic" ControlToValidate="txtExitUnits" ErrorMessage="Insert number of units." CssClass="text-danger"></asp:RequiredFieldValidator>
                </td>      
            </tr>             
        </ItemTemplate>
        <FooterTemplate>
              </table>   
        </FooterTemplate>
    </asp:Repeater>

后面的代码:

protected void btnExit_Click(object sender, EventArgs e)
{    
        List<RepeaterItem> selectedItems = rptItems.Items.Cast<RepeaterItem>().Where(x => ((CheckBox)x.FindControl("cbItem")).Checked).ToList();
        Repeater1.DataSource = selectedItems;
        Repeater1.DataBind();
}

Repeater1将包含通过:selectedItems选择的数据这是中继器1

 <asp:Repeater ID="Repeater1" runat="server">
    <HeaderTemplate>
        <table class="table table-bordered table-hover table-responsive table-striped table-condensed">
            <tr>                    
                <th>Goods Desc</th>
                <th>Balance Units</th>                    
            </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>                
            <td><%#Eval("ItemDesc") %></td>
            <td><%#Eval("InvoicBalanceUnits") %></td>         
        </tr>         
    </ItemTemplate>
    <FooterTemplate>
          </table>   
    </FooterTemplate>
</asp:Repeater>

当我运行代码时,它给我一个错误,"ItemDesc"不存在于repeater1,怎么了?

Asp.net/C#通过复选框获取中继器行数据

参照this解决:

 DataTable checkedData = new DataTable();
    checkedData.Columns.AddRange(new DataColumn[6] { new DataColumn("CampCode"), new DataColumn("BalUnits", typeof(decimal)), new DataColumn("ExitUni",typeof(decimal)), new DataColumn("itemNum"), new DataColumn("UOM"), new DataColumn("WT", typeof(decimal)) });
    foreach (RepeaterItem item in rptItems.Items)
    {
        if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
        {
            CheckBox checkBoxInRepeater = item.FindControl("cbItem") as CheckBox;
            if (checkBoxInRepeater.Checked)
            {
                DataRow dr = checkedData.NewRow();
                Label lblCampCode = (Label)item.FindControl("lblCampCode");
                Label lblBalUnits = (Label)item.FindControl("lblBalUnits");
                TextBox txtExitUnits = (TextBox)item.FindControl("txtExitUnits");
                Label lblItemNo = (Label)item.FindControl("lblItemNo");
                Label lblUom = (Label)item.FindControl("lblUom");
                Label lblWT =  (Label)item.FindControl("lblWt");
                Label lblcode = (Label)item.FindControl("lblCode");
                HiddenField hfcode = (HiddenField)item.FindControl("hfCustomerId");
                string BalUnits = lblBalUnits.Text;
                string ExitUni = txtExitUnits.Text;
                var exitUni = decimal.Parse(txtExitUnits.Text);
                var balUni = decimal.Parse(BalUnits);

                if (exitUni > balUni)
                {
                  string myStringVariable = "Exit balance is larger than balance units!!";
                  ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');", true);
                }
                else
                { 
                    dr["CampCode"] = lblCampCode.Text;
                    dr["BalUnits"] = lblBalUnits.Text;
                    dr["itemNum"] =  lblItemNo.Text;
                    dr["UOM"] = lblUom.Text;
                    dr["WT"] = lblWT.Text;
                    dr["ExitUni"] = txtExitUnits.Text.Trim();
                    //string code = hfcode.Value;
                    checkedData.Rows.Add(dr);
                    Session["CheckedRows"] = checkedData;