[英]Asp.Net pass data from code-behind to code-infront?
I have this in my code behind on my Asp.Net page 我在我的Asp.Net页面上的代码中有此功能
Public Class display
Inherits System.Web.UI.Page
Protected Property pID As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Request.QueryString("id") = "") Then
Response.Redirect("Default.aspx")
Else
pID = Request.QueryString("id")
End If
End Sub
End Class
Now, I want to get the ID to influence items in the code infront when the page loads, so i added this: 现在,我想获取ID以在页面加载时影响代码前面的项目,因此我添加了以下内容:
<asp:Label ID="Label1" runat="server" Text='<%# pID %>'></asp:Label>
as a test object. 作为测试对象。 when ran 'Label1' is just a blank span.
当运行“ Label1”只是一个空白范围时。 what am i doing wrong?
我究竟做错了什么?
You don't need <%# pID %>
, Just use 您不需要
<%# pID %>
,只需使用
Label1.Text = pID
As Label1
is a server control and is accessible in page load. 由于
Label1
是服务器控件,因此可以在页面加载中访问。
Change your code as 将您的代码更改为
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Request.QueryString("id") = "") Then
Response.Redirect("Default.aspx")
Else
pID = Request.QueryString("id")
Label1.Text = pID
End If
End Sub
EDIT: 编辑:
I would suggest you to use LinkButton 我建议您使用LinkButton
<asp:LinkButton OnClick="LinkButton_Click" />
CS Code CS代码
Sub LinkButton_Click(sender As Object, e As EventArgs)
Response.Redirect(piccom.displayLink(pID))
End Sub
Keep things simple and organized like @saptal is suggesting. 保持事情简单和井井有条,就像@saptal所建议的那样。
pID = Request.QueryString("id")
Label1.Text = pID
If you need an anchor control you can use the HyperLink control like this on your markup: 如果您需要锚控件,则可以在标记中使用HyperLink控件,如下所示:
<asp:HyperLink ID ="genHyperLink" CssClass="genLink" runat="server" />
code behind: 后面的代码:
genHyperLink.NavigateUrl = piccom.displayLink(pID)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.