简体   繁体   English

我想在回发时保留一个数组

[英]I want to preserve an array on postback

I have a page where someone is going to input a string.我有一个页面,有人要输入一个字符串。 There will be a button to submit, but also we want this page to work with a scanner so it posts back when it scans.将有一个提交按钮,但我们还希望此页面与扫描仪一起使用,以便在扫描时回发。 That part I will worry about later.那部分我稍后会担心。

What I want right now is to be able to enter a string in the textbox, hit submit, and it save that string to an array.我现在想要的是能够在文本框中输入一个字符串,点击提交,然后将该字符串保存到一个数组中。 Then the textbox is cleared and they can enter another string.然后文本框被清除,他们可以输入另一个字符串。 It will save this string to the array and repeat.它会将这个字符串保存到数组中并重复。 After the user has all the strings in they want, there will be another button to do something else (not important).在用户拥有他们想要的所有字符串后,将有另一个按钮来执行其他操作(不重要)。 So it will grab all the strings in the array and execute some code.所以它会抓取数组中的所有字符串并执行一些代码。

Right now I cannot seem to get the array to keep the information.现在我似乎无法让数组来保存信息。 I'm trying to use ViewState for the first time in my life, and I see the string when I come back in from postback, but I can't save that string to the array.我人生中第一次尝试使用 ViewState,当我从回发回来时看到了该字符串,但我无法将该字符串保存到数组中。

I hope this makes sense我希望这是有道理的

Protected jobs() As String
Protected i As Integer = 0

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

If Not ViewState("jobs") Is Nothing Then
    jobs(i) = ViewState("jobs").ToString
    i += 1
End If

Protected Sub btnAddToBatch_Click(sender As Object, e As EventArgs) Handles btnAddToBatch.Click
    If txtJob.Text <> "" Then
        ViewState("jobs") = txtJob.Text
    End If
End Sub

I have figured it out incase anyone is interested.我已经弄清楚了,以防有人感兴趣。

Protected jobs(0) As String
Protected i As Integer = 0 

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

If Not ViewState("jobs") Is Nothing Then
    jobs = ViewState("jobs")
    i = ViewState("i")
End If

End Sub

Protected Sub btnAddToBatch_Click(sender As Object, e As EventArgs) Handles btnAddToBatch.Click

If txtJob.Text <> "" Then
    If i = 0 Then
        ReDim jobs(0)
    Else
        ReDim Preserve jobs(jobs.Length)
    End If
    jobs(i) = txtJob.Text
    i = i + 1
    ViewState("i") = i
    ViewState("jobs") = jobs
End If

End Sub

I would set the list up like this:我会这样设置列表:

Public MyList As New List(Of String)

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If IsPostBack = False Then
       ' first time page load - put MyList into session()
        Session("Mylist") = MyList
    Else
        ' page post back - pull list from session
        MyList = Session("MyList")
    End If

End Sub

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    ' add a value to a text box on say simple button click

    Debug.Print(TextBox1.Text)

    MyList.Add(TextBox1.Text)


End Sub

Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    Debug.Print("number in list = " & MyList.Count)

    Debug.Print("First value in list = " & MyList.Item(0))

    ' display all items in list

    For Each s As String In MyList
        Debug.Print(s)
    Next

End Sub

Note how we don't even have to shove back the MyList into session().请注意,我们甚至不必将 MyList 推回 session()。 It like any object is a pointer to the Mylist in session.它像任何 object 一样是指向 session 中的 Mylist 的指针。 The above will also work equal well with viewstate.以上内容也适用于视图状态。

As you can see, by using a list in place of a array, we don't have to re-dim, we don't have to have a extra "i" value, and the list can expand as we place.如您所见,通过使用列表代替数组,我们不必重新变暗,我们不必有额外的“i”值,并且列表可以随着我们放置而扩展。 Also, the list has other cool features like此外,该列表还有其他很酷的功能,例如

    MyList.Contains        - search  the list for a match
    MyList.Sort()          - sort the list

So, not only is the list dynamic, does not require re-dim, but you can also search and search the list without having to write looping code.因此,不仅列表是动态的,不需要重新变暗,而且您还可以搜索和搜索列表,而无需编写循环代码。 Even in VBA, I tend now days to use a collection or list - they are just oh so much less code, easier to work with, and as noted you get extra features like searching with "contains", and even sorting abilities.即使在 VBA 中,我现在也倾向于使用集合或列表 - 它们的代码少得多,更容易使用,并且如前所述,您可以获得额外的功能,例如使用“包含”搜索,甚至排序能力。 And even with all these features you wind up writing less code, and in FEW cases have to deal with a loop based on a integer value to get values out of the array.即使拥有所有这些功能,您最终编写的代码也会更少,并且在少数情况下必须处理基于 integer 值的循环以从数组中获取值。

Array's quite much go back to the old days of say FORTRAN or the older PC "basic" languages, and now with lists or collections, we have not only better choices, but those choices are less code, less effort and more flexbile. Array 的 go 可以追溯到过去的 FORTRAN 或较旧的 PC“基本”语言,现在有了列表或 collections,我们不仅有更少的工作量和更好的选择,而且更灵活。

And this is not really a huge deal - but as you can see, by using the on-load event of the given web page, we ONLY one spot and ONLY in one location have to set up the value - since it is a object, then further code can simply add/change or do whatever to the MyList, and we are NOT required to shove the MyList back into session - since the on-load event takes care of this process.这并不是什么大不了的事——但正如你所看到的,通过使用给定 web 页面的加载事件,我们只需在一个位置且仅在一个位置设置值——因为它是 object,然后进一步的代码可以简单地添加/更改或对 MyList 执行任何操作,并且我们不需要将 MyList 推回 session - 因为加载事件会处理此过程。 Setting MyList from session() means the MyList is "pointing" to the object in session - and this is why I don't even have to save the list back to session, but ONLY set it up one time on page load.从 session() 设置 MyList 意味着 MyList “指向” session 中的 object - 这就是为什么我什至不必将列表保存回 Z21D6F40CFB511982E4424E0ELY2509 的原因,而是在加载时加载。 Any and all other events such as button code etc. is now free to add to that list or do whatever - and NO need to send/save the MyList back to session is required with the above setup.任何和所有其他事件(例如按钮代码等)现在都可以自由添加到该列表或执行任何操作 - 并且不需要通过上述设置将 MyList 发送/保存回 session。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM