简体   繁体   中英

How do I get 'tr's ID with checkbox checked in code behind using C#

I got some problem and let me spend some time on testing.

I have a ListView , and a checkbox inside this, and a button outside.

When I click button , will show the rows Tr ID that rows checkbox checked .

but it always show ctrl2 instead of ID.

This is my code:

 <asp:ListView ID="ListView1" runat="server"> <LayoutTemplate> <table cellspacing="0" border="0"> <tr> <th scope="col" width="125">Man </th> <th scope="col" width="50">Type </th> <th scope="col" width="400">Reason </th> <th scope="col" width="125">date </th> <th scope="col" width="100">cheack </th> <th scope="col" width="125">remark </th> </tr> <tr runat="server" id="itemPlaceholder" /> </table> </LayoutTemplate> <ItemTemplate> <tr id="<%#Eval("ID").ToString()%>" runat="server"> <td scope="col" > <%#Eval("UserID").ToString()%> </td> <td scope="col"> <%#Eval("Purpose").ToString() %> </td> <td scope="col"> <%#Eval("Reason").ToString() %> </td> <td scope="col"> <%#dateSession(Convert.ToDateTime( Eval("startTime"))) %> </td> <td scope="col"> <asp:CheckBox ID="CheckBox1" runat="server" Checked="True" /> </td> <td scope="col"> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </td> </tr> </ItemTemplate> </asp:ListView> 

This is my code behind

protected void Btn_save_Click(object sender, EventArgs e)
{
    foreach (ListViewItem row in this.ListView1.Items)
    {
        CheckBox stylistcheck = (CheckBox)row.FindControl("CheckBox1");

        if (stylistcheck.Checked == true)
        {
            Response.Write(row.ID);
            // Always get wrong ID , like 'ctrl3' not real rows Tr ID
        }
    }
}

Doing this <tr id="<%#Eval("ID").ToString()%>" runat="server"> will throw an error, AFAIR. If your goal is to get the ID for each row that represents your data then you can just add a literal control, hide it, and get it's value in code-behind. Something like:

<tr runat="server">
    <td scope="col" >
        <%# Eval("UserID").ToString()%>
        <asp:Literal ID="idLiteral" runat="server" Text='<%# Eval("ID").ToString()%>' Visible="false" ></asp:Literal>
    </td>
    ...rest of your code goes here...
</tr>

In code-behind, inside your if (stylistcheck.Checked == true) do a Response.Write(((Literal)row.FindControl("idLiteral")).Text); like so:

if (stylistcheck.Checked == true)
{
    Response.Write(((Literal)row.FindControl("idLiteral")).Text);       
}
CheckBox1.Checked = true;

It may help you.

OR

CheckBox chkDelete = (CheckBox)item.FindControl("CheckBox1");
if(chkDelete.Checked)
{
    string yourID = item.DataItem["id"].ToString();
    itemsToDelete.Add(yourID);
}

OR

 if (CheckBox1.Checked==true)
    {

    }

This may also work for you.

 yourListView.DataSource = GetCourses();
 yourListView.DataBind();

 var checkBox = yourListView.Controls.Cast<Control>).First().FindControl("yourID")

or

if (item.ItemType == ListViewItemType.DataItem){

CheckBox final = (CheckBox)item.FindControl("yourID");
if (final.Checked)  {
}

Just wondering are you sure you can run this code ?

<tr id="<%#Eval("ID").ToString()%>" runat="server"> 

I tried your code and run it will encounter error on this line...
Because with " you will get error server tag is not well formed.
If i use this ' the error will be : The ID property of a control can only be set using the ID attribute in the tag and a simple value.

I write the code which will retrieve ID might help you...

.aspx

<asp:ListView ID="ListView1" runat="server" >
        <LayoutTemplate>
            <table id="tbllol" runat="server" cellspacing="0" border="0">
                <tr>
                    <th scope="col" width="100">cheack
                    </th>
                    <th scope="col" width="125">remark
                    </th>
                </tr>
                <tr runat="server" id="itemPlaceholder" />
            </table>
        </LayoutTemplate>
        <ItemTemplate>
            <tr>
                <td scope="col">
                    <asp:CheckBox ID="chkBox" runat="server" Checked="true" />
                    <asp:Label ID="lblID" runat="server" Text='<%# Eval("ID") %>' Visible="false"></asp:Label>
                </td>
                <td scope="col">
                    <asp:TextBox ID="txtRemarks" runat="server"></asp:TextBox>
                </td>
            </tr>
        </ItemTemplate>
    </asp:ListView>

    <asp:Button ID="btn" runat="server" Text="Save" OnClick="btn_Click" />

.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    // Check
    if (!IsPostBack)
    {
        // Variable
        DataTable dt = new DataTable();
        dt.Columns.Add("ID");

        // Loop & Add ID
        for (int i = 0; i < 10; i++) dt.Rows.Add("myIDIi" + i);

        ListView1.DataSource = dt;
        ListView1.DataBind();



    }
}

protected void btn_Click(object sender, EventArgs e)
{
    // Variable
    string value = string.Empty;

    // Loop
    foreach (ListViewItem row in ListView1.Items)
    {
        // Find Control
        CheckBox chkbox = row.FindControl("chkBox") as CheckBox;
        Label lblID = row.FindControl("lblID") as Label;

        // Check & Check Then Set to Value
        if (chkbox != null && chkbox.Checked)
            if (lblID != null) value += lblID.Text.Trim() + "<br />";
    }

    Response.Write(value);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM