[英]write the same code from aspx page to behind code in the cs page
is there a way to write this code from an aspx page to a behind code of the aspx page (cs), in asp.net. 在asp.net中,有没有一种方法可以将此代码从aspx页面写到aspx页面(cs)的后面代码。
<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: 例如,如果我在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 谢谢
Short answer - yes - a hyperlink control renders as <a>
so you could do this: 简短答案-是-超链接控件呈现为
<a>
因此您可以执行以下操作:
Hyperlink a = new Hyperlink();
a.ID = "userImageLightBox";
See the MSDN regarding this server control: 有关此服务器控件,请参见MSDN:
http://msdn.microsoft.com/en-us/library/k0b15efk(v=vs.71).aspx http://msdn.microsoft.com/zh-CN/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). 每当控件运行为
runat=server
这意味着您将能够从页面后面的aspx代码(.cs,.vb等)访问它。 So if you ever want to change a specific property, such as the NavigateURL
property you could do so. 因此,如果您想更改特定的属性,例如
NavigateURL
属性,则可以进行更改。
a.NavigateURL = "someURL";
Since you have already set the runat="server"
attribute, you can access the HTML controls in your code-behind through its id
: 由于您已经设置了
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";
Update: If you want to create the HTML from the code-behind, you can do as JonH wrote: 更新:如果要从后面的代码创建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);
Oh, and please increase your accept rate. 哦,请提高您的接受率。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.