如何在asp转发器中选中复选框时传递id?

时间:2017-07-17 07:23:44

标签: c# asp.net repeater

我有一个asp转发器,通过跟踪号码来绑定订单列表。我想知道在选中复选框时如何传递所选行的ID。我这样做只绑定选中的复选框。

中继器

<asp:Repeater ID="Repeater2" runat="server" OnItemDataBound="Repeater2_ItemDataBound">
    <HeaderTemplate>
        <table class=" table table-bordered">
            <tr>
                <th></th>
                <th>Order Type</th>
                <th>Job Name</th>
                <th>Price</th>
            </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <asp:CheckBox ID="chkDisplayTitle" runat="server" /></td>
            <td><%#Eval("Order") %></td>
            <td><%#Eval("Job_Name") %></td>
            <td><%#Eval("Price") %></td>
        </tr>

    </ItemTemplate>
    <FooterTemplate>
        <td></td>
        <td></td>
        <td>
            <td>Subtotal:
        <asp:Label ID="lblSubtotal" runat="server"></asp:Label>
            </td>
        </td>
        </tr>
        </table>
    </FooterTemplate>
</asp:Repeater>

代码背后

private void ConsolidateItems()
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connect"].ConnectionString);

    //======= Parameterized select Query.
    string cmdText = "SELECT * FROM Order_TB WHERE Tracking_Number=@tracking AND Status=@status";

    //====== Providning information to SQL command object about which query to 
    //====== execute and from where to get database connection information.
    SqlCommand cmd = new SqlCommand(cmdText, con);

    //===== Adding parameters/Values.
    cmd.Parameters.AddWithValue("@tracking", hfid.Value);
    cmd.Parameters.AddWithValue("@status", "Accepted");


    if (con.State == ConnectionState.Closed)
    {
        con.Open();
    }

    Repeater2.DataSource = cmd.ExecuteReader();
    Repeater2.DataBind();
    con.Close();
}

1 个答案:

答案 0 :(得分:0)

在ASPX上:

  <asp:CheckBox ID="chkBoxID" runat="server" OnCommand="doSomething_Checked" 
   CommandArgument="<%# Some Binding Information%>" CommandName="NameForArgument"> </asp:CheckBox>

On Code-Behind:

 protected void doSomething_Checked(object sender, CommandEventArgs e)
   {

            CheckBox ctrl = (CheckBox)sender;
            RepeaterItem rpItem = ctrl.NamingContainer as RepeaterItem;
            if (rpItem != null)
            {
                CheckBox chkBox = (CheckBox)rpItem.FindControl("chkBoxID");
                chkBox.DoSomethingHere...
            }
   }

你可以这样做......