简体   繁体   中英

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

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:

            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 = "";
                
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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