C# 将相同的代码从aspx页面写入cs页面中的隐藏代码
有没有办法从aspx页面编写此代码 到asp.net中的aspx页(cs)的隐藏代码C# 将相同的代码从aspx页面写入cs页面中的隐藏代码,c#,asp.net,code-behind,C#,Asp.net,Code Behind,有没有办法从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> 谢谢简短回答-是-超链接控件呈现为
<a rel="lightbox" id="userImageLightBox" runat="server" title="profile image">
<img id="userImage" runat="server" width="150" height="146" alt="" src=""/>
</a>
谢谢简短回答-是-超链接控件呈现为
任何时候控件runat=server
这意味着您将能够从aspx代码隐藏页(.cs、.vb等)访问它。因此,如果您想要更改特定属性,例如NavigateURL
属性,您可以这样做
a.NavigateURL = "someURL";
由于您已经设置了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";
哦,请提高您的接受率。但是我如何从代码背后编写rel=“lightbox”没有a.rel=”“谢谢Dennis的回答,但这不是我想要的,我不想编写我在代码背后发布的代码,而不是在sapx代码中发布的代码好吧,那么我可能误解了这个问题。看看我的最新答案。非常感谢,如果我能再问你一个问题,我如何在超链接中包含图片url和图片高?谢谢你的快速回答你帮了大忙
Hyperlink a = new Hyperlink();
a.ID = "userImageLightBox";
a.NavigateURL = "someURL";
// *.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";
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);