![](/img/trans.png)
[英]On button click send Ajax call and get value from aspx.cs(code behind) to aspx page and display it in input field in the same page
[英]write the same code from aspx page to behind code in the cs page
在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>
例如,如果我在apsx中有代码:
<asp:Label ID="pageProfileHeadName" runat="server" Visible="true"/>
在后面的代码中,我可以做:
Label label = new Label();
label.ID = "pageProfileHeadName";
label.Visible = true;
谢谢
简短答案-是-超链接控件呈现为<a>
因此您可以执行以下操作:
Hyperlink a = new Hyperlink();
a.ID = "userImageLightBox";
有关此服务器控件,请参见MSDN:
http://msdn.microsoft.com/zh-CN/library/k0b15efk(v=vs.71).aspx
每当控件运行为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";
更新:如果要从后面的代码创建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);
哦,请提高您的接受率。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.