简体   繁体   English

我如何在使用C#的情况下在代码中签入带复选框的'tr's ID

[英]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. 我有一个ListView ,里面有一个checkbox ,外面有一个按钮。

When I click button , will show the rows Tr ID that rows checkbox checked . 当我click button ,将显示已checkbox checked行的行Tr ID

but it always show ctrl2 instead of ID. 但它始终显示ctrl2而不是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. 这样做<tr id="<%#Eval("ID").ToString()%>" runat="server">将引发错误,即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. 如果您的目标是获取代表数据的每一行的ID ,则只需添加一个文字控件,将其隐藏,然后在代码隐藏中获取其值即可。 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); 在后面的代码中,在if (stylistcheck.Checked == true)执行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. 如果我使用此'错误将是:控件的ID属性只能使用标签中的ID属性和简单值进行设置。

I write the code which will retrieve ID might help you... 我写的代码将检索ID可能对您有帮助...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在后面的代码中获取WPF DataGrid的Row CheckBox的Checked事件 - how can I get the WPF DataGrid's Row CheckBox's Checked Event in code behind 如何使用WPF在C#中获取combobox复选框选中的值 - how to get combobox checkbox checked values in c# using wpf 如何将HTML元素的值传递给C#代码隐藏方法? - How do I pass an HTML element's value to a C# code-behind method? x:Name 在 c# wpf 后面的代码中找不到,我如何获得它的值? - x:Name dont find in code behind c# wpf, how do i get the value of it? 如何使用代码隐藏更改C#中添加到画布的UserControl的宽度? - How do I change the width of a UserControl that is added to a canvas in C# using code-behind? 如何使用jQuery在C#中的代码后面获取单击事件 - How to get click event in code behind in c# using jquery 如何在背后使用C#代码获取JavaScript日期格式 - How to get javascript date format using C# code behind 我在工作室代码中使用 C#,如何从列表视图中的选中列中获取内容? - I'm using C# in studio code, how to get content from checked column in listview? 如何通过选中获取动态复选框控件ID为true? - How to get Dynamic checkbox control id's by checked is true? 选中复选框后如何执行代码? C# - How to execute code the second a checkbox is checked? C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM