mercredi 19 août 2015

How do I get 'tr's ID when 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
        }
    }
}




Aucun commentaire:

Enregistrer un commentaire