访问变量定义并在代码后面设置 - 在aspx页面上

时间:2020-12-21 15:51:51

Sorry for such a basic question. I work in C# but have to do something on a site written in vb.net

对不起这个基本问题。我在C#工作,但必须在用vb.net编写的网站上做一些事情

In C# I can declare a variable and set it like this - in code behind.

在C#中,我可以声明一个变量并将其设置为这样 - 在后面的代码中。

public partial class _Default : System.Web.UI.Page
{
    public string str = "Fred";
    protected void Page_Load(object sender, EventArgs e)
    {
    str = "Jim";
    }
}

And, on the .aspx page, anywhere on the page I can write:

并且,在.aspx页面上,我可以在页面的任何地方写:

<% Response.Write(str) %>

and it will write "Jim" to the screen.

它会将“Jim”写入屏幕。

How do you do that in vb.net? I have ...

你是如何在vb.net中做到的?我有 ...

Public Class _Default
Inherits System.Web.UI.Page
Public Ary As ArrayList
Public str As String

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim str as String
str = "Freda"

If I write <% Response.Write(str) %> on the .aspx page - nothing is written.

如果我在.aspx页面上写<%Response.Write(str)%> - 没有写任何内容。

If I write <% Response.Write("Freda") %> on the .aspx page - it writes "Freda" to the screen.

如果我在.aspx页面上写<%Response.Write(“Freda”)%> - 它会将“Freda”写入屏幕。

In vb.net - what do you have to do to access variables set in the code behind, on the .aspx page?

在vb.net中 - 你需要做些什么来访问.aspx页面后面的代码中设置的变量?

I am not interested in setting the text value of a server control like a label etc. What I actually need to do is:

我对设置像标签等服务器控件的文本值不感兴趣。我真正需要做的是:

Get a list of images from a database call and

从数据库调用中获取图像列表

Build a dynamic jquery slider on the .aspx page by looping through the list of images and writing the various

通过循环遍历图像列表并编写各种图像,在.aspx页面上构建动态jquery滑块

  • tags and onclick events etc.

    Sometimes my slider will have 3 pictures, other times it might have 6 or 8. Could be any number between 3 and 11. But I need to build it dynamically based on data returned from a database.

    有时我的滑块将有3张图片,有时可能有6或8张。可能是3到11之间的任何数字。但我需要根据数据库返回的数据动态构建它。

  • 2 个解决方案

    #1


    When you declare the Public string you should also define a default value. For example:

    声明Public字符串时,还应定义默认值。例如:

    Public str as String = "Fred"
    

    Then in the Page_Load event you don't need to declare a new variable, simply reference the public variable like you did in C#:

    然后在Page_Load事件中,您不需要声明一个新变量,只需像在C#中那样引用公共变量:

    str = "Freda"
    

    Then when writing that to the output you would simply declare:

    然后在将其写入输出时,您只需声明:

    <%=str%>
    

    However, if you are bringing through from a database would it not be best to use DataList object and build the HTML for the slider that way?

    但是,如果从数据库中引入,那么最好不要使用DataList对象并为滑块构建HTML吗?

    #2


    If you define str before your page load, you will be able to access it throughout your class. Hopefully this helps.

    如果在页面加载之前定义str,则可以在整个班级中访问它。希望这会有所帮助。

    Public Class _Default : System.Web.UI.Page
    Inherits BasePage
    
    Dim str as String = ""
    
    protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    {
        str = "Jim";
        Response.Write(str)
    
    }
    

    #1


    When you declare the Public string you should also define a default value. For example:

    声明Public字符串时,还应定义默认值。例如:

    Public str as String = "Fred"
    

    Then in the Page_Load event you don't need to declare a new variable, simply reference the public variable like you did in C#:

    然后在Page_Load事件中,您不需要声明一个新变量,只需像在C#中那样引用公共变量:

    str = "Freda"
    

    Then when writing that to the output you would simply declare:

    然后在将其写入输出时,您只需声明:

    <%=str%>
    

    However, if you are bringing through from a database would it not be best to use DataList object and build the HTML for the slider that way?

    但是,如果从数据库中引入,那么最好不要使用DataList对象并为滑块构建HTML吗?

    #2


    If you define str before your page load, you will be able to access it throughout your class. Hopefully this helps.

    如果在页面加载之前定义str,则可以在整个班级中访问它。希望这会有所帮助。

    Public Class _Default : System.Web.UI.Page
    Inherits BasePage
    
    Dim str as String = ""
    
    protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    {
        str = "Jim";
        Response.Write(str)
    
    }