在aspx页面中向cs页面中的后面代码写入相同的代码

发布于 2025-01-01 10:34:00 字数 552 浏览 4 评论 0原文

有没有办法从 aspx 页面编写此代码 到asp.net 中的aspx 页面(cs) 的后台代码。

<a rel="lightbox" id="userImageLightBox" runat="server" title="profile image">
    <img id="userImage"  runat="server"  width="150"  height="146"  alt="" src=""/>
</a>  

例如,如果我在 apsx 中有代码:

<asp:Label ID="pageProfileHeadName" runat="server" Visible="true"/>

在后面的代码中我可以这样做:

    Label label = new Label();
    label.ID = "pageProfileHeadName";
    label.Visible = true;

谢谢

is there a way to write this code from an aspx page
to a behind code of the aspx page (cs), in asp.net.

<a rel="lightbox" id="userImageLightBox" runat="server" title="profile image">
    <img id="userImage"  runat="server"  width="150"  height="146"  alt="" src=""/>
</a>  

for example if i have the code in apsx:

<asp:Label ID="pageProfileHeadName" runat="server" Visible="true"/>

in the behind code i can do:

    Label label = new Label();
    label.ID = "pageProfileHeadName";
    label.Visible = true;

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦一生花开无言 2025-01-08 10:34:00

简短的回答 - 是的 - 超链接控件呈现为 ,因此您可以执行以下操作:

Hyperlink a = new Hyperlink();
a.ID = "userImageLightBox";

请参阅有关此服务器控件的 MSDN:

http://msdn.microsoft.com/en-us/library/k0b15efk(v=vs.71).aspx

任何时候控件runat=server这意味着您将能够从页面后面的 aspx 代码(.cs、.vb 等)访问它。因此,如果您想要更改特定属性,例如 NavigateURL 属性,您可以这样做。

a.NavigateURL = "someURL";

Short answer - yes - a hyperlink control renders as <a> so you could do this:

Hyperlink a = new Hyperlink();
a.ID = "userImageLightBox";

See the MSDN regarding this server control:

http://msdn.microsoft.com/en-us/library/k0b15efk(v=vs.71).aspx

Anytime a control is runat=server this means you will be able to access it from the aspx code behind page (.cs, .vb, etc). So if you ever want to change a specific property, such as the NavigateURL property you could do so.

a.NavigateURL = "someURL";
坦然微笑 2025-01-08 10:34:00

由于您已经设置了 runat="server" 属性,因此您可以通过其 id 访问代码隐藏中的 HTML 控件:

// *.aspx:
<a id="userImageLightBox" runat="server" ...>
    <img id="userImage" runat="server" ... />
</a> 

// code-behind:
userImageLightBox.Title = "New Title";
userImage.Src = "~/images/profile.png";

// To get or set an attribute like `rel`:
userImageLightBox.Attributes["rel"] = "test";

更新: 如果您想从代码隐藏创建 HTML,您可以按照 JonH 写道:

HyperLink a = new HyperLink();
  a.ID = "userImageLightBox";
  a.Attributes["rel"] = "lightbox";

  Image img = new Image();
  img.ID = "userImage";
  img.ImageUrl = "img.png";
  img.Width = 150;
  img.Height = 146;

  a.Controls.Add(img);

哦,请提高您的接受率。

Since you have already set the runat="server" attribute, you can access the HTML controls in your code-behind through its id:

// *.aspx:
<a id="userImageLightBox" runat="server" ...>
    <img id="userImage" runat="server" ... />
</a> 

// code-behind:
userImageLightBox.Title = "New Title";
userImage.Src = "~/images/profile.png";

// To get or set an attribute like `rel`:
userImageLightBox.Attributes["rel"] = "test";

Update: If you want to create the HTML from the code-behind, you can do as JonH wrote:

HyperLink a = new HyperLink();
  a.ID = "userImageLightBox";
  a.Attributes["rel"] = "lightbox";

  Image img = new Image();
  img.ID = "userImage";
  img.ImageUrl = "img.png";
  img.Width = 150;
  img.Height = 146;

  a.Controls.Add(img);

Oh, and please increase your accept rate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文