1

这是我的页面片段:

...
<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
  <table>
    <tr>
      <td>
        <asp:TextBox MaxLength="255" ID="myTextBox" runat="server"></asp:TextBox>
      </td>
    </tr>
  </table>
</asp:Content>
...

这是页面代码隐藏部分:

...
Control parent = this.myTextBox.Parent; //this is acutally asp:Content control
string parentID = parent.ID; //this is PlaceHolderMain
...

我需要的是引用<td>元素(因为我想改变它的Visibility属性)。我怎样才能做到这一点?我哪里错得这么严重?:)

4

4 回答 4

1

td needs to be ran server side for it to be accessed and classed as a parent element.

<td id="td" runat="server">
   <asp:TextBox MaxLength="255" ID="myTextBox" runat="server"></asp:TextBox>
</td>
于 2013-09-04T10:09:22.523 回答
1

ASPX Code behind really only knows about the ASP controls (i.e. the ones with the asp: prefix in the tags) the td is just plain old html. In order for the code behind to interacte with it, you will need to add a runat="server" on the tag

于 2013-09-04T10:10:36.407 回答
0

The table over here is an html table not an ASP.NET server control. So you can't access it in the code behind.

Use this in case :

<asp:Table ID="Table1" runat="server">
            <asp:TableRow ID="TableRow1" runat="server" ForeColor="Teal">
                <asp:TableCell>
                    <asp:TextBox MaxLength="255" ID="myTextBox" runat="server"> 
                    </asp:TextBox>
                </asp:TableCell>
            </asp:TableRow>
</asp:Table>
于 2013-09-04T10:10:25.593 回答
0

<td>按照其他用户的建议,将属性 runat 添加到标记。

<td id="mytd" runat="server">
   <asp:TextBox MaxLength="255" ID="myTextBox" runat="server"></asp:TextBox>
</td>

然后在c#端(返回页面)

添加这个头文件

using System.Web.UI.HtmlControls;
protected void Button_click(object sender,EventArgs e)
{
  mytd.Attributes.Add("style","visibility:none");
}
于 2013-09-04T10:31:37.737 回答