我是ASP.net的新手,主要使用Access和VB6编写程序,并试图将某些内容移植到ASP.net。我基本上有一个程序可以动态创建复选框/文本框,并将查询值读入每个文本框。这个公园工作得很好,但是每当选中复选框时我都会遇到尝试触发事件的问题,因为我希望它能够使用该文本框消息进行特定的操作。我知道如何触发常规固定复选框的点击事件,但这些动态的复制品让我发疯,因为我无法完成它的工作。我已经将来自不同网站的一些代码整理在一起,这就是我所拥有的。有人可以帮忙吗?
这里的概念是动态texbox将包含从SQL查询读取的汉堡的配件(可以工作),但是当用户点击与每个文本框关联的复选框时,它将记录它的值(像生菜或番茄)。
Private Sub Burgers_Init(sender As Object, e As EventArgs) Handles Me.Init
Dim myConnString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\xxxx250\Documents\Visual Studio 2015\Projects\Burgers\Burgers\App_Data\Burgers Database.accdb;"
Dim myConnection As New OleDbConnection(myConnString)
myConnection.Open()
Dim strSQL_Count As String
Dim intSQL_Count As Integer
strSQL_Count = "SELECT COUNT(*) As Record_Count FROM tblBurgers WHERE Type = 'Topping'"
Dim myCommand As New OleDbCommand(strSQL_Count, myConnection)
Dim myReader As OleDbDataReader = myCommand.ExecuteReader()
While myReader.Read
intSQL_Count = myReader.GetInt32(0)
End While
Dim myCommand2 As New OleDbCommand("SELECT * FROM tblBurgers", myConnection)
Dim myReader2 As OleDbDataReader = myCommand2.ExecuteReader()
Dim i As Integer
'This automatically loops through the query results, whether it be 1 or 100
While myReader2.Read
Dim txt1 = New TextBox()
Dim chk1 = New CheckBox
txt1.ID = "txtTextBox" & (i).ToString
chk1.ID = "chkCheckBox" & (i).ToString
chk1.AutoPostBack = True
txt1.Width = 400
txt1.Text = myReader2.GetString(2)
txt1.Wrap = True
txt1.TextMode = TextBoxMode.MultiLine
txt1.AutoPostBack = True
TabContainer1_Toppings_Panel.Controls.Add(chk1)
TabContainer1_Toppings_Panel.Controls.Add(txt1)
Dim lt As New Literal()
lt.Text = "<br />"
TabContainer1_Toppings_Panel.Controls.Add(lt)
If IsPostBack = True Then
RecreateControls("chk", "Checkbox")
RecreateControls("txt", "Textbox")
End If
i = i + 1
End While
End Sub
Private Function FindOccurence(ByVal substr As String) As Integer
Dim reqstr As String = Request.Form.ToString()
Return ((reqstr.Length - reqstr.Replace(substr, "").Length) / substr.Length)
End Function
Private Sub RecreateControls(ByVal ctrlPrefix As String, ByVal ctrlType As String)
Dim ctrls As String() = Request.Form.ToString().Split("&"c)
Dim cnt As Integer = FindOccurence(ctrlPrefix)
If cnt > 0 Then
Dim lt As Literal
For k As Integer = 1 To cnt
For i As Integer = 0 To ctrls.Length - 1
If ctrls(i).Contains((ctrlPrefix & "-") + k.ToString()) Then
Dim ctrlName As String = ctrls(i).Split("="c)(0)
Dim ctrlValue As String = ctrls(i).Split("="c)(1)
'Decode the Value
ctrlValue = Server.UrlDecode(ctrlValue)
If ctrlType = "TextBox" Then
Dim txt As New TextBox()
txt.ID = ctrlName
txt.Text = ctrlValue
TabContainer1_Toppings_Panel.Controls.Add(txt)
lt = New Literal()
lt.Text = "<br />"
TabContainer1_Toppings_Panel.Controls.Add(lt)
End If
If ctrlType = "Checkbox" Then
Dim chk1 As New CheckBox
chk1.ID = ctrlName
'These msgboxes aren't firing, although post back activity is happening here
If chk1.Checked = True Then
MsgBox("We have a hit on checked checkbox")
ElseIf chk1.Checked = False Then
MsgBox("We have a hit on non-checked checkbox")
End If
Exit For
End If
End If
Next
Next
End If
End Sub
答案 0 :(得分:0)
首先请在您的行上方写下以下行:
Option Strict On
Option Infer Off
这是为了更好的代码。
您要搜索的是AddHandler和RemoveHandler语句。
https://msdn.microsoft.com/en-us/library/7taxzxka.aspx
- 应始终定义变量:
Dim strSQL_Count As String = String.Empty
Dim intSQL_Count As Integer = 0
- 使用方法而不是直接将代码写入click_event,init或其他任何方法。
- 这可以写得更容易:
Dim txt1 = New TextBox()
Dim chk1 = New CheckBox
txt1.ID = "txtTextBox" & (i).ToString
chk1.ID = "chkCheckBox" & (i).ToString
chk1.AutoPostBack = True
txt1.Width = 400
txt1.Text = myReader2.GetString(2)
txt1.Wrap = True
txt1.TextMode = TextBoxMode.MultiLine
txt1.AutoPostBack = True
Dim chk1 As CheckBox = New CheckBox()
Dim txt1 As TextBox = New TextBox()
With chk1
.ID = "chkCheckBox" & (i).ToString
.AutoPostBack = True
End With
With txt1
....
End With
如果IsPostBack = True那么没必要。只写:
If IsPostBack Then
i = i + 1没必要。只写i + = 1
If chk1.Checked = True Then
MsgBox("We have a hit on checked checkbox")
ElseIf chk1.Checked = False Then
MsgBox("We have a hit on non-checked checkbox")
End If
= True也没有必要。 chk1.Checked可以替换为!chk1.Checked。
不要使用MsgBox。请改用MessageBox.Show("")
。