[英]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.