Asp.net/C#通过复选框获取中继器行数据 [英] Asp.net/C# Get repeater row data through checkbox

查看:68
本文介绍了Asp.net/C#通过复选框获取中继器行数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个中继器,它将以表格的形式将数据显示为每一行带有复选框,因此我想在单击按钮时显示选中的行数据.

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代码:

  <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 选择的数据这是repeater1

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>

当我运行代码时,它给我一个错误,指出Repeater1中不存在"ItemDesc",这是怎么回事?

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

推荐答案

通过引用解决:

 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;

这篇关于Asp.net/C#通过复选框获取中继器行数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆