Asp.net/C# Get repeater row data through checkbox

2019-09-04 04:10发布

Hello i have a repeater that will present data as a table with checkbox for each row, so i want to show the checked rows data when a button is clicked.

.aspx code:

  <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>

the code behind:

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 will contain the data selected through :selectedItems this is the repeater1

 <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>

when i run the code it gives me an error that "ItemDesc" isn't exist in repeater1, whats wrong ?

1条回答
forever°为你锁心
2楼-- · 2019-09-04 05:10

Solved by reference to 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;
查看更多
登录 后发表回答