2017-02-19 51 views
0

我是ASP.net的新手,主要使用Access和VB6编写程序,并试图将某些内容移植到ASP.net。我基本上有一个程序,动态创建复选框/文本框,并将查询值读入每个文本框。这个公园效果很好,但我在尝试触发事件时遇到问题,无论何时复选框被选中,因为我希望它能够针对该文本框消息进行特定的操作。我知道如何触发常规固定复选框的点击事件,但这些动态的事件让我发疯,因为我无法完成它的工作。我一起黑了一些来自不同网站的代码,这就是我所拥有的。谁能帮忙?DynamicCheckbox的onClick事件ASP.net VB

这里的概念是动态texboxes将包含从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

首先请写出下列线以上的线路:

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 

=真是不必要的。 chk1.Checked可以替换为!chk1.Checked。

请勿使用MsgBox。改为使用MessageBox.Show("")