简体   繁体   English

c#代码访问html表行的所有控件

[英]c# code to access all the controls of a html table row

what i am doing is to find the checkbox control in a html table(id=tblorder) which is in the div(id=test1) and i am working in the content page and what i want is to access all the controls(other textboxes) which lies in the same row of the html table.all the code that i have tried is in coments but none of them works我正在做的是在 div(id=test1) 中的 html 表 (id=tblorder) 中找到复选框控件,我正在内容页面中工作,我想要的是访问所有控件(其他文本框) 位于 html 表的同一行。我尝试过的所有代码都在评论中,但没有一个有效

<div id="test1" runat="server">
      <table id="tblorder" align="center" class="auto-style3" border="1">
                <tr>
                    <td class="auto-style9">
                         <asp:Label ID="label11" runat="server" Text="Order Number : "></asp:Label>
                         <asp:Label ID="lblorderno" runat="server" Text="" ></asp:Label>

                    </td>
                    <td><asp:Label ID="Label2" runat="server" Text="Enter item description"></asp:Label></td>
                    <td class="auto-style8"><asp:Label ID="Label1" runat="server" Text="Amount"></asp:Label></td>
                </tr>
                <tr>
                    <td class="auto-style12">
                        <asp:CheckBox ID="chkCoat" Text="Coat" runat="server" />
                    </td>
                    <td class="auto-style13">
                        <asp:TextBox ID="txtItemDesc1" runat="server" Width="339px"></asp:TextBox>
                    </td>
                    <td class="auto-style14"> 
                        <asp:TextBox ID="txtItemAmnt1"  class="totaltextbox" runat="server" Width="106px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style5">
                        <asp:CheckBox ID="chkPant" Text="Pant" runat="server" />
                    </td>
                    <td class="auto-style6">
                        <asp:TextBox ID="txtItemDesc2"  runat="server" Width="337px"></asp:TextBox>
                    </td>
                    <td class="auto-style15"> 
                        <asp:TextBox ID="txtItemAmnt2"  class="totaltextbox" runat="server" Width="106px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style5">
                        <asp:CheckBox ID="chkShirt" Text="Shirt" runat="server" />
                    </td>
                    <td class="auto-style6">
                        <asp:TextBox ID="txtItemDesc3"  runat="server" Width="338px"></asp:TextBox>
                    </td>
                    <td class="auto-style15"> 
                        <asp:TextBox ID="txtItemAmnt3"  class="totaltextbox" runat="server" Width="106px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style5">
                        <asp:CheckBox ID="chksherwani" Text="Serwani" runat="server" />
                    </td>
                    <td class="auto-style6">
                        <asp:TextBox ID="txtItemDesc4"   runat="server" Width="335px"></asp:TextBox>
                    </td>
                    <td class="auto-style15"> 
                        <asp:TextBox ID="txtItemAmnt4"  class="totaltextbox" runat="server" Width="106px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style5">
                        <asp:CheckBox ID="chkmodijacket" Text="Modijacket" runat="server" />
                    </td>
                    <td class="auto-style6">
                        <asp:TextBox ID="txtItemDesc5"  runat="server" Width="336px"></asp:TextBox>
                    </td>
                    <td class="auto-style15"> 
                        <asp:TextBox ID="txtItemAmnt5"  class="totaltextbox" runat="server" Width="106px"></asp:TextBox>
                    </td>
                </tr>
               </div>

C# C#

foreach (Control ctrlChk in test1.Controls)
{
    if (ctrlChk is CheckBox)
    {
        string tt=((CheckBox)ctrlChk).Text;
        if (lstItemsCount.Any(obj => obj.Contains(tt)))
        {
            //TableRow tr = (TableRow)ctrlChk.NamingContainer;
            //string selRowIndex = (Table)(ctrlChk.Parent.ID;
            //CheckBox objttl = (CheckBox)ctrlChk.Parent.NamingContainer;
            //  var re= (CheckBox)ctrlChk.NamingContainer;

            ((CheckBox)ctrlChk).Checked = true;
            lstItemsCount.Remove(tt);
            i = 0;
            break;
        }
    }
}

all i did is defining the parent of the TextBox using this code:我所做的就是使用以下代码定义 TextBox 的父级:

            Control myControl1 = FindControl("TextBoxID");
        if (myControl1 != null)
        {
            // Get control's parent.
            Control myControl2 = myControl1.Parent;
            Response.Write("Parent of the text box is : " + myControl2.ID);
        }
        else
        {
            Response.Write("Control not found");
        }

then:然后:

foreach (Control c in ControlParent.Controls)
        {
            if(c is TextBox)
            {
                (c as TextBox).Text = "";
                
            }
        }

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

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