我正在尝试编写代码,只需单击按钮,就可以将字符串变量推送到页面上,然后打开该页面。我遇到的麻烦是变量,一个按钮的文本,嵌套在gridview中,使访问变得困难。这也很困难,因为我将它从SQL数据库导入Gridview,所以我也无法直接从那里访问它。如果我使用按钮的onClick事件,它适用于我当前的代码,但由于某种原因,我必须单击该按钮两次才能使其工作。我在这里读到了使用onPreRender事件,但这会干扰将字符串变量推送到页面上,而我无法做到这一点。是否有任何其他方法可以摆脱不必涉及onPreRender事件的按钮两次?
以下是代码:
Imports System.Data.SqlClient
Imports System.IO
Partial Class PartsLookup
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Private Function FindControlRecursive(ByVal RootControl As Control, ByVal ControlID As String) As Control
If RootControl.ID = ControlID Then
Return RootControl
End If
For Each controlToSearch As Control In RootControl.Controls
Dim controlToReturn As Control = FindControlRecursive(controlToSearch, ControlID)
If controlToReturn IsNot Nothing Then
Return controlToReturn
End If
Next
Return Nothing
End Function
Protected Sub Button1Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim clickedButton As Button = sender
MsgBox(clickedButton.Text)
Dim ControlText As Control = FindControlRecursive(GridView1, clickedButton.ID)
Dim ControlText1 As Button = ControlText
Dim MachineNumber As String = ControlText1.Text
If MachineNumber = "" Then
ControlText1.Visible = False
End If
Dim SpecificPart() As String = {String that is the files contents, the middle of which is where the variable will be pushed to}
Dim path As String = "File path where the variable is to be pushed to"
File.WriteAllLines(path, SpecificPart)
clickedButton.PostBackUrl = "~/PartNumberPages/PartTemplate.aspx"
End Sub
End Class