簡體   English   中英

我如何在使用C#的情況下在代碼中簽入帶復選框的'tr's ID

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

我遇到了一些問題,讓我花一些時間進行測試。

我有一個ListView ,里面有一個checkbox ,外面有一個按鈕。

當我click button ,將顯示已checkbox checked行的行Tr ID

但它始終顯示ctrl2而不是ID。

這是我的代碼:

 <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> 

這是我的代碼背后

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
        }
    }
}

這樣做<tr id="<%#Eval("ID").ToString()%>" runat="server">將引發錯誤,即AFAIR。 如果您的目標是獲取代表數據的每一行的ID ,則只需添加一個文字控件,將其隱藏,然后在代碼隱藏中獲取其值即可。 就像是:

<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>

在后面的代碼中,在if (stylistcheck.Checked == true)執行Response.Write(((Literal)row.FindControl("idLiteral")).Text); 像這樣:

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

它可能會幫助您。

要么

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

要么

 if (CheckBox1.Checked==true)
    {

    }

這也可能為您工作。

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

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

要么

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

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

只是想知道您確定可以運行此代碼?

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

我嘗試了您的代碼並運行,它將在此行遇到錯誤...
因為使用“您將獲得錯誤,服務器標記格式不正確。
如果我使用此'錯誤將是:控件的ID屬性只能使用標簽中的ID屬性和簡單值進行設置。

我寫的代碼將檢索ID可能對您有幫助...

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM