asp:c#使用复选框打印选中的行

时间:2016-11-13 11:29:00

标签: c# asp.net

我想在单击按钮时在转发器表中打印选定的行,这是.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"  ClientIDMode="AutoID" AutoPostBack="True" /></td>

                <td><asp:Label runat="server" Id="lblCampCode" Text='<%#Eval("ItemDesc") %>'></asp:Label></td>

                <td><asp:Label ID="lblBalUnits" runat="server" text='<%#Eval("InvoicBalanceUnits") %>'></asp:Label> </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>


    <asp:Button ID="btnExit" runat="server"  Text="Exit now" OnClick="btnExit_Click" />

这是按钮点击事件的.cs代码,它将循环转发器中的已检查项目,并将检索已检查的行数据:

   protected void btnExit_Click(object sender, EventArgs e)
    {
        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)
                {
                    Label lblCampCode = (Label)item.FindControl("lblCampCode");
                    Label lblBalUnits = (Label)item.FindControl("lblBalUnits");
                    TextBox txtExitUnits = (TextBox)item.FindControl("txtExitUnits");
                    string CampCode = lblCampCode.Text;
                    string BalUnits = lblBalUnits.Text;
                    string ExitUni = txtExitUnits.Text;

                }
            }
        }

    }

我认为我应该使用数据表来存储数据。

1 个答案:

答案 0 :(得分:0)

我使用Datatable这样解决它:

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

dr["CampCode"]=lblexitUnits.text;
//And so on
DataRow dr = checkedData.NewRow();