[英]c# code to access all the controls of a html table row
我正在做的是在 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#
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;
}
}
}
我所做的就是使用以下代碼定義 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");
}
然后:
foreach (Control c in ControlParent.Controls)
{
if(c is TextBox)
{
(c as TextBox).Text = "";
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.