开发者

dynamically created dropdownlist

开发者 https://www.devze.com 2022-12-31 09:22 出处:网络
I have a problem with DropDownList\'s SelectedIndexChanged event. when the value of district DropDownList is changed, it fires its SelectedIndexChanged event but it does not fill the licencee DropDown

I have a problem with DropDownList's SelectedIndexChanged event. when the value of district DropDownList is changed, it fires its SelectedIndexChanged event but it does not fill the licencee DropDownList.

Also, when the value of dynamically created brandDropDownList is changed, it first fires the SelectedIndexChanged event of district DropDownList and then fills the licencee DropDownList.

I am giving some code here so you can understand what I am saying.

Imports System.Data
Imports System.Data.SqlClient
Imports Microsoft.VisualBasic.CompilerServices

Partial Class Transaction_Sales_Demand
    Inherits System.Web.UI.Page
    Dim objClsDemand As New ClsDemand
    Dim objClsPurchase As New ClsPurchaseOrderGen
    Shared ds As DataSet
    Shared dt As DataTable
    Public WithEvents Drpdynamic As DropDownList
    Public WithEvents Txtdynamic As TextBox
    Public unitname() As String



    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        'If Not IsPostBack Then
        'ds = objClsDemand.FillUnitInHeader()
        'generateDynamicColumns(ds)
        'GrdDemand.DataBind()

        'End If
    End Sub
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ''Dim newddlBrand As DropDownList
        'Dim strddlbrandID As String
        'Dim ddlNewBrand As DropDownList = form1.FindControl("ddlBrand")

        'strddlbrandID = ddlNewBrand.ID

        ds = objClsDemand.FillUnitInHeader()
        generateDynamicColumns(ds)
        If Not (IsPostBack) Then
         开发者_开发知识库   'objClsDemand.FillBrand(ddlNewBrand)
            objClsDemand.FillDistrict(ddlDistrict)
        End If

    End Sub


    Private Sub generateDynamicColumns(ByVal Ds As DataSet)
        Dim i As Integer = 0
        Dim h As Integer = 0
        Dim pnl As Panel = New Panel()
        Dim intcount As Integer = 0
        ' Dim dr As DataRow


        'objClsDemand.FillLicencee(ddllicencee)
        'objClsDemand.FillBrand(ddlBrand)
        'objClsDemand.FillLabel(ddlLabel)
        pnl.Controls.Clear()
        dt = New DataTable()

        If dt.Columns.Count = 0 Then
            dt.Columns.Add("LabelCode")
            dt.Columns.Add("BrandName")
            dt.Columns.Add("LabelName")
            dt.Columns.Add("ProofLiter")
        End If

        For i = 0 To Ds.Tables(0).Rows.Count - 1
            dt.Columns.Add(New DataColumn(Ds.Tables(0).Rows(i)(0)))
            dt.Columns.Add(New DataColumn(Ds.Tables(0).Rows(i)(1)))
        Next

        Dim Lbldynamic As Label

        Response.Write("</table>")
        Response.Write("<tr>")
        For intcount = 0 To dt.Columns.Count - 1
            Response.Write("<th>")
            Response.Write("<b>")
            Lbldynamic = New Label()
            Lbldynamic.Height = "23"

            Lbldynamic.Font.Bold = True
            Lbldynamic.Style("Position") = "Absolute"
            ' txtunit(i).Style("Top") = "210px"
            Lbldynamic.Style("Top") = 130

            If dt.Columns(intcount).ColumnName = "BrandName" Then
                Lbldynamic.Width = "140"
                Lbldynamic.Style("Left") = h
                h = h + 143
            ElseIf dt.Columns(intcount).ColumnName = "LabelName" Then
                Lbldynamic.Width = "210"
                Lbldynamic.Style("Left") = h
                h = h + 213
            Else
                Lbldynamic.Width = "110"
                Lbldynamic.Style("Left") = h
                h = h + 113
            End If
            Lbldynamic.Text = dt.Columns(intcount).ColumnName

            'unitname(intcount) = Lbldynamic.Text

            ' Response.Write(un.GetValue(i))
            Response.Write("</b>")
            Response.Write("</th>")
            pnl.Controls.Add(Lbldynamic)
            'form1.Controls.Add(Lbldynamic)

            Response.Write("<th>")
        Next
        Response.Write("</tr>")
        h = 0

        Response.Write("<tr>")


        For intcount = 0 To dt.Columns.Count - 1
            If dt.Columns(intcount).ColumnName = "BrandName" Or dt.Columns(intcount).ColumnName = "LabelName" Then
                Response.Write("<th>")
                Response.Write("<b>")
                Drpdynamic = New DropDownList

                If dt.Columns(intcount).ColumnName = "BrandName" Then
                    Drpdynamic.ID = "ddlBrand"
                    Drpdynamic.AutoPostBack = True
                    objClsDemand.FillBrand(Drpdynamic)
                    AddHandler Drpdynamic.SelectedIndexChanged, AddressOf Me.Drpdynamic_SelectedIndexChanged
                    Drpdynamic.Width = "140"
                    Drpdynamic.Style("Left") = h
                    h = h + 143
                ElseIf dt.Columns(intcount).ColumnName = "LabelName" Then
                    Drpdynamic.ID = "ddlLabel"
                    Drpdynamic.AutoPostBack = True
                    Drpdynamic.Width = "210"
                    Drpdynamic.Style("Left") = h
                    h = h + 213
                End If

                Drpdynamic.Height = "23"
                'Drpdynamic.Width = "110"
                Drpdynamic.Font.Bold = True
                Drpdynamic.Style("Position") = "Absolute"
                ' txtunit(i).Style("Top") = "210px"
                Drpdynamic.Style("Top") = 150
                'Drpdynamic.Style("Left") = h
                ' Response.Write(un.GetValue(i))
                Response.Write("</b>")
                Response.Write("</th>")
                pnl.Controls.Add(Drpdynamic)
                'h = h + 113
                Response.Write("<th>")

            Else
                Response.Write("<th>")
                Response.Write("<b>")
                Txtdynamic = New TextBox()
                If dt.Columns(intcount).ColumnName = "LabelCode" Then
                    Txtdynamic.ID = "txtLabelCode"
                Else
                    Txtdynamic.ID = "txtdynamic" + intcount.ToString()
                End If

                AddHandler Txtdynamic.TextChanged, AddressOf Me.TextBox_TextChanged
                Txtdynamic.AutoPostBack = True
                Txtdynamic.Height = "23"
                Txtdynamic.Width = "110"
                Txtdynamic.Font.Bold = True
                Txtdynamic.Style("Position") = "Absolute"
                ' txtunit(i).Style("Top") = "210px"
                Txtdynamic.Style("Top") = 150
                Txtdynamic.Style("Left") = h
                ' Response.Write(un.GetValue(i))
                Response.Write("</b>")
                Response.Write("</th>")
                pnl.Controls.Add(Txtdynamic)
                h = h + 113
                Response.Write("<th>")
            End If

        Next
        Response.Write("</tr>")
        Response.Write("</table>")
        form1.Controls.Add(pnl)

    End Sub


    Private Sub Drpdynamic_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Drpdynamic.SelectedIndexChanged
        Try

            Dim ddlNewBrand As DropDownList = New DropDownList()
            ddlNewBrand = DirectCast(sender, DropDownList)
            Dim ddlNewLabel As DropDownList = New DropDownList()
            ddlNewLabel = form1.FindControl("ddlLabel")
            If Not ddlNewBrand.SelectedValue = "" Then
                objClsDemand.BrandId = ddlNewBrand.SelectedValue
            End If
            objClsDemand.FillLabel(ddlNewLabel)
            'ddlLabel.Enabled = True
        Catch ex As Exception
            ProjectData.SetProjectError(ex)
            clsFunctions.ErrorLog(DateAndTime.Today, "Transaction", "Demand Generation", "ddlBrand_SelectedIndexChanged", ex.Message)
            ProjectData.ClearProjectError()
        End Try
    End Sub

    Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Txtdynamic.TextChanged
        Dim txtNewText As TextBox = New TextBox()
        txtNewText = DirectCast(sender, TextBox)
        Dim i As Integer = 3

        Dim ddlNewBrand As DropDownList = DirectCast(sender, TextBox).FindControl("ddlBrand")
        Dim ddlNewLabel As DropDownList = New DropDownList()
        ddlNewLabel = form1.FindControl("ddlLabel")

        If txtNewText.ID = "txtLabelCode" Then
            Try

                Dim dr As DataRow

                objClsPurchase.LabelCode = txtNewText.Text

                If Not txtNewText.Text.Equals("") Then
                    dr = objClsPurchase.fillDetails()
                    If Not IsDBNull(dr("Brandname")) Then
                        ddlNewBrand.SelectedItem.Text = dr("BrandName")
                        ddlNewBrand.Enabled = False
                    End If

                    If Not IsDBNull(dr("LabelName")) Then
                        If ddlNewLabel.SelectedValue = "" Then
                            objClsDemand.FillLabel(ddlNewLabel)
                            ddlNewLabel.SelectedItem.Text = dr("LabelName")
                            ddlNewLabel.Enabled = False
                        Else
                            ddlNewLabel.SelectedItem.Text = dr("LabelName")
                            ddlNewLabel.Enabled = False
                        End If
                    End If

                Else
                    objClsPurchase.FillBrand(ddlNewBrand)
                End If


            Catch ex As Exception
                ProjectData.SetProjectError(ex)
                clsFunctions.ErrorLog(DateAndTime.Today, "Transaction", "Purchase Order Generation", "txtLabelcode_TextChanged", ex.Message)
                ProjectData.ClearProjectError()
            End Try

            'Else
            '    Dim dr1 As DataRow
            '    For i = 4 To dt.Columns.Count - 1

            '        If txtNewText.ID = "txtdynamic" + i.ToString() Then
            '            Dim txtnew As TextBox = New TextBox()
            '            txtnew = form1.FindControl(dt.Columns(i + 1).ColumnName())
            '            txtNewText.ID = "txtdynamic" + i.ToString()
            '            objClsDemand.UnitId = objClsDemand.UnitId_byName(dt.Columns(i).ColumnName)
            '            objClsDemand.LabelId = Val(ddlNewLabel.SelectedValue)
            '            dr1 = objClsDemand.FillRate(txtNewText)
            '            txtnew.Text = dr1("LendingPrice")
            '        End If
            '    Next



        End If


    End Sub

    Protected Sub ImageButton3_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton3.Click
        'Dim txtNew As TextBox = New TextBox()
        'Dim ddlnew As DropDownList = New DropDownList()
        'Dim str As String
        'For intcount = 0 To dt.Columns.Count - 1
        '    If dt.Columns(intcount).ColumnName = "BrandName" Then

        '        'ddlnew = DirectCast(sender, DropDownList)
        '        ddlnew = form1.FindControl("ddlBrand")
        '        MsgBox("this is Brand ddl" + ddlnew.SelectedItem.Text)
        '    ElseIf dt.Columns(intcount).ColumnName = "LabelName" Then
        '        'ddlnew = DirectCast(sender, DropDownList)
        '        ddlnew = form1.FindControl("ddlLabel")
        '        MsgBox("this is label ddl" + ddlnew.SelectedItem.Text)
        '    ElseIf dt.Columns(intcount).ColumnName = "LabelCode" Then
        '        'txtNew = DirectCast(sender, TextBox)
        '        txtNew = form1.FindControl("txtLabelCode")
        '        MsgBox("this is label code txt")
        '    Else
        '        str = "txtdynamic" + intcount.ToString()
        '        MsgBox("this is" + str + " label code txt" + txtNew.Text)
        '    End If
        'Next

    End Sub


    Protected Sub ddlDistrict_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDistrict.SelectedIndexChanged

        If Not ddlDistrict.SelectedValue = "" Then
            objClsDemand.DistrictId = ddlDistrict.SelectedValue
        End If
        objClsDemand.FillLicencee(ddllicencee)
    End Sub
End Class


Have you checked that after you set a new datasource for each dropdown you are making a call to DataBind() on that contol?

0

精彩评论

暂无评论...
验证码 换一张
取 消