【问题标题】:onClick Event for DynamicCheckbox ASP.net VBDynamicCheckbox ASP.net VB 的 onClick 事件
【发布时间】:2017-02-19 21:20:52
【问题描述】:

我是 ASP.net 的新手,主要使用 Access 和 VB6 编写程序,并且正在尝试将某些东西移植到 ASP.net。我基本上有一个程序可以动态创建复选框/文本框并将查询值读入每个文本框。这个公园效果很好,但是我在检查复选框时试图触发事件时遇到问题,因为我希望它能对文本框消息进行特定的操作。我知道如何触发常规固定复选框的点击事件,但这些动态复选框让我发疯,因为我无法让它完全正常工作。我已经将来自不同站点的一些代码拼凑在一起,这就是我所拥有的。有人可以帮忙吗?

这里的概念是动态 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

【问题讨论】:

    标签: asp.net vb.net


    【解决方案1】:

    首先请在你的行上方写下以下几行:

    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("")

    【讨论】:

      猜你喜欢
      • 2018-12-13
      • 2013-10-13
      • 1970-01-01
      • 2010-10-06
      • 1970-01-01
      • 2013-04-30
      • 1970-01-01
      • 1970-01-01
      • 2013-03-12
      相关资源
      最近更新 更多