We help IT Professionals succeed at work.

getting SelectedValue of a DropDownList in a DataGrid

waterhidden
waterhidden asked
on
1,045 Views
Last Modified: 2012-05-04
i create my dropdownlist in ItemDataBound:

Dim drp As New DropDownList
'fill dropdownlist...
drp.ID = "drp" 'this works (when i view source of html page, i see this id)
e.Item.Cells(3).Controls.Add(drp)


then i wanna get selectedvalue of this dropdownlist:

Dim drop As New DropDownList
drop = CType(Me.SiparisSepet.Items(i).Cells(3).FindControl("drp"), DropDownList) 'when trace in on this line, drop's value is NOTHING!!
Dim yetkiliKod As String = drop.SelectedValue.ToString 'trace breaks on this line and the error is: "Object reference not set to an instance of an object."


is there any way to reach the selectedvalue of a dropdownlist in a datagirid which is created in ITEMDATABOUND?

waiting for any help, thanx..
Comment
Watch Question

Commented:
Are you binding the grid in your page load?
Do you have the the code to bind the grid in am if statement

If Not Page.IsPostBack then
 'fill gird
End if

Page Load is called every time you post back.  If you haven't wrapped your bidinging code it will rebind the grid and wipe out any selections.

But otherwise that is exactly how you retrieve a control value from a grid.  

Author

Commented:

yes, datagrid binds in page load, in "If Not Page.IsPostBack"

any other ideas?

Commented:
post your code

Author

Commented:
   Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.msg.Text = ""
        Dim Funcs As New Functions

        If Not Page.IsPostBack Then

            'FillPers()
            Me.perKod.Text = "00700178"
            Me.malzemeKategori.Text = Funcs.FillMalzemeKategori(Me.MyConn, Me.MyComm)
            FillMalKat()

            Me.SiparisSepet.DataSource = (ListTaleps(Me.perKod.Text))
            Me.SiparisSepet.DataBind()

        End If
        Me.siparisSepeti.Text = Funcs.FillSiparisSepeti(Me.MyConn, Me.MyComm, Me.perKod.Text)
        Me.favorilerim.Text = Funcs.FillFavoriListesi(Me.MyConn, Me.MyComm, Me.perKod.Text)

    End Sub



    Private Sub SiparisSepet_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles SiparisSepet.ItemDataBound

        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then

            If e.Item.Cells(0).Text = "0" Or e.Item.Cells(0).Text = "1" Then
                e.Item.Cells(3).ColumnSpan = CInt(e.Item.Cells.Count)
                For i = e.Item.Cells.Count - 1 To 0 Step -1
                    If i <> 3 And i <> 1 And i <> 0 And i <> 2 Then
                        e.Item.Cells.RemoveAt(i)
                    End If
                Next
                e.Item.Cells(0).Visible = False
                e.Item.Cells(1).Visible = False
                e.Item.Cells(2).Visible = False
                j += 1
                e.Item.Cells(3).BackColor = Color.LightBlue

                Dim lbl1 As New Label
                Dim lbl2 As New Label
                Dim lbl3 As New Label

                lbl1.Text = "<table border=""1"" cellpadding=""0"" cellspacing=""0"" style=""border-collapse: collapse"" bordercolor=""#C0C0C0"" width=""100%""><tr><td>" & _
                                 "<table border=""0"" cellpadding=""0"" cellspacing=""0"" style=""border-collapse: collapse"" bordercolor=""#C0C0C0"" width=""100%"">" & _
                                 "<tr>" & _
                                 "<td width=""30%"" rowspan=""2"">" & j & ".<font color=""#808080""><i> " & e.Item.Cells(2).Text & "</i></font></td>" & _
                                 "<td width=""15%""><font color=""#808080""><i>Yetkili:</i></font></td>" & _
                                 "<td width=""55%"" align=""right"">"

                lbl2.Text = "</td></tr><tr><td width=""15%""><font color=""#808080""><i>Maliyet Merkezi:</i></font></td><td width=""55%"" align=""right"">"

                lbl3.Text = "</td> </tr> </table> </td> </tr> </table>"

                'YETKILI DROPDOWNLIST
                Dim drp As New DropDownList

                MyConn.Open()
                If e.Item.Cells(0).Text = "1" Then
                    MyComm.CommandText = "SELECT FATURA.GEN60PERSORG.PERSKOD, FATURA.GEN60PERSORG.PERSAD FROM ORGAGRUPDETAY" & _
                            "2, GEN81WORKFLOWGRUP2, FATURA.GEN60PERSORG WHERE ORGAGRUPDETAY2.GRPKOD = GEN81WO" & _
                            "RKFLOWGRUP2.WFGRPKOD AND ORGAGRUPDETAY2.PERSKOD = FATURA.GEN60PERSORG.PERSKOD AN" & _
                            "D (GEN81WORKFLOWGRUP2.PROGRAM = 'ETALEP') AND (GEN81WORKFLOWGRUP2.GRPKOD = '" & e.Item.Cells(1).Text & "'" & _
                            ") AND (GEN81WORKFLOWGRUP2.WFSIRA = 1) AND (FATURA.GEN60PERSORG.ORGKADEME IN ('" & _
                            "ORG', 'BOS')) AND (FATURA.GEN60PERSORG.ORGSEVIYE = 0)"
                ElseIf e.Item.Cells(0).Text = "0" Then
                    MyComm.CommandText = "SELECT EXPR1, EXPR2 FROM ((SELECT FATURA.GEN60PERSORG.PERSKOD AS EXPR1, FATURA.GE" & _
                    "N60PERSORG.PERSAD AS EXPR2 FROM ORGAGRUPDETAY2, GEN82WORKFLOWGRUP2, FATURA.GEN60" & _
                    "PERSORG WHERE ORGAGRUPDETAY2.GRPKOD = GEN82WORKFLOWGRUP2.WFGRPKOD AND ORGAGRUPDE" & _
                    "TAY2.PERSKOD = FATURA.GEN60PERSORG.PERSKOD AND (GEN82WORKFLOWGRUP2.PROGRAM = 'ET" & _
                    "ALEP') AND (GEN82WORKFLOWGRUP2.MALKOD = '" & e.Item.Cells(1).Text & "') AND (GEN82WORKFLOWGRUP2.WFSIRA =" & _
                    " 1) AND (FATURA.GEN60PERSORG.ORGKADEME IN ('ORG', 'BOS')) AND (FATURA.GEN60PERSO" & _
                    "RG.ORGSEVIYE = 0)) UNION (SELECT '0' AS EXPR1, 'SATINALMA' AS EXPR2 FROM GEN82WO" & _
                    "RKFLOWGRUP2 WHERE MALKOD = '" & e.Item.Cells(1).Text & "' AND WFGRPKOD = '-')) DERIVEDTBL"
                End If

                Dim rdr As OleDb.OleDbDataReader = MyComm.ExecuteReader
                k = 0
                While rdr.Read
                    drp.Items.Add(rdr.Item(1))
                    drp.Items(k).Value = rdr.Item(0)
                    k += 1
                End While
                rdr.Close()

                'MALIYET MERKEZI DROPDOWNLIST

                Dim drp2 As New DropDownList

                MyComm.CommandText = "SELECT EL3_CODE MMCODE, EL3_NAME MMNAME FROM OASKOC.OAS_EL3_ELEMENT " & _
                "WHERE (EL3_CMPCODE = '010') AND (EL3_CODE LIKE '3%') ORDER BY MMNAME"

                Dim rdr2 As OleDb.OleDbDataReader = MyComm.ExecuteReader
                k = 0
                While rdr2.Read
                    drp2.Items.Add(rdr2.Item(1))
                    drp2.Items(k).Value = rdr2.Item(0)
                    k += 1
                End While
                rdr2.Close()


                drp.ID = "drp"

                drp.Attributes.Add("Runat", "Server")

                drp.DataBind()

                drp2.ID = "drp2"

                MyComm.CommandText = "SELECT FATURA.HUS_RIGHT('0000000000' || NVL(MALIYETMERKEZI, 'Tnmsz'), 5) AS MALIYETMERKEZI " & _
                "FROM FATURA.GEN60PERSONEL WHERE (LOGONUSER = '" & UCase(Request.ServerVariables("REMOTE_USER").Replace("i", "I")) & "') " & _
                "GROUP BY MALIYETMERKEZI"

                Dim rdr3 As OleDb.OleDbDataReader = MyComm.ExecuteReader
                Dim mmerkezi As String
                While rdr3.Read
                    mmerkezi = rdr3.Item(0)
                End While
                rdr3.Close()

                'If Not mmerkezi = "Tnmsz" Then
                '    drp2.Items.FindByValue(mmerkezi).Selected = True
                'End If


                MyConn.Close()

                e.Item.Cells(3).Controls.Add(lbl1)
                e.Item.Cells(3).Controls.Add(drp)
                e.Item.Cells(3).Controls.Add(lbl2)
                e.Item.Cells(3).Controls.Add(drp2)
                e.Item.Cells(3).Controls.Add(lbl3)

                'CType(e.Item.Cells(3).Controls(3), HtmlControls.HtmlInputHidden).Value = "drp" 'e.Item.Cells(3).Controls(3).UniqueID()



                'e.Item.Cells(3).Controls(1).Visible = False
            Else
                    e.Item.Cells(4).Text = "<a href=""javascript:popupPage('MalzemeAyrinti.aspx?mamulKod=" & e.Item.Cells(3).Text & "',1)"">" & e.Item.Cells(4).Text & "</a>"
                    e.Item.Cells(0).Visible = False
                    e.Item.Cells(1).Visible = False
                    e.Item.Cells(2).Visible = False
                    e.Item.Cells(3).Visible = False
                End If

            ElseIf e.Item.ItemType = ListItemType.Header Then
            e.Item.Cells(0).Visible = False
            e.Item.Cells(1).Visible = False
            e.Item.Cells(2).Visible = False
            e.Item.Cells(3).Visible = False

        ElseIf e.Item.ItemType = ListItemType.Footer Then
            e.Item.Cells(0).ColumnSpan = CInt(e.Item.Cells.Count)
            e.Item.Cells(0).Text = "<font color = ""red"">Talep Sepetinizdeki Malzemeler " & j & " Farklý Talepe Dönüþmüþtür. Onaylýyorsanýz 'Devam' Tuþuna Basýnýz. Talep(ler) Ýþleme Alýnacaktýr.</font>"
            For i = e.Item.Cells.Count - 1 To 1 Step -1
                e.Item.Cells.RemoveAt(i)
            Next
        End If

    End Sub




    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'Dim aaa As String = (CType(Me.SiparisSepet.Items(1).FindControl("drp"), DropDownList).SelectedItem.Value).ToString
        Dim Funcs As New Functions
        Dim talepID, tempGrpKod, yetkiliKod, grpKod As String
        Dim arr As New ArrayList
        Dim arr2 As New ArrayList

        MyConn.Open()
        MyComm.CommandText = "SELECT MAMULKOD, GRPKOD, BIRIM FROM TEMPTALEP WHERE PERSKOD = '" & Me.perKod.Text & "' AND KATEGORI = '0'"
        Dim rdr1 As OleDb.OleDbDataReader = MyComm.ExecuteReader

        If rdr1.HasRows Then
            While rdr1.Read
                arr.Add(rdr1.Item(0))
                arr2.Add(rdr1.Item(2))
                grpKod = rdr1.Item(1)
            End While

            MyComm2.CommandText = "SELECT MAX(SIPARISKOD) FROM SIPARISLER"
            MyConn2.Open()
            Dim rdr2 As OleDb.OleDbDataReader = MyComm2.ExecuteReader
            While rdr2.Read
                talepID = Funcs.StringIncreaser(rdr2.Item(0).ToString)
            End While
            rdr2.Close()

            'INSERT INTO SIPARISLER
            MyComm2.CommandText = "INSERT INTO SIPARISLER (SIPARISKOD, PERSKOD, SIPTARIH, ACIL, ONAY, AKTIF) VALUES " & _
            "('" & talepID & "', '" & Me.perKod.Text & "', TO_DATE('" & Date.Now.ToString & "', 'DD/MM/YYYY HH24:MI:SS'), '0', '0', '1')"

            Dim rdr3 As OleDb.OleDbDataReader = MyComm2.ExecuteReader
            rdr3.Close()

            'INSERT ARR() INTO SIPARISAYRINTI
            For i = 0 To arr.Count - 1
                MyComm2.CommandText = "INSERT INTO SIPARISAYRINTI (SIPARISKOD, MAMULKOD, BIRIM, DURUM, ONAY) VALUES " & _
                "('" & talepID & "', '" & arr.Item(i) & "', '" & arr2.Item(i) & "', '0', '0')"
                Dim rdr4 As OleDb.OleDbDataReader = MyComm2.ExecuteReader
                rdr4.Close()
            Next

            'INSERT INTO SIPARISDURUM
            MyComm2.CommandText = "SELECT WFKOD, WFSIRA, WFGRPKOD FROM GEN81WORKFLOWGRUP2 WHERE (PROGRAM = 'ETALEP')" & _
            " AND (GRPKOD = '" & grpKod & "') ORDER BY WFKOD"

            Dim rdr5 As OleDb.OleDbDataReader = MyComm2.ExecuteReader

            While rdr5.Read

                '****************NE YAP NE ET BU DROPDOWNA ULAS!!

                If rdr5.Item(1) = "1" Then
                    For i = 0 To Me.SiparisSepet.Items.Count - 1
                        If Me.SiparisSepet.Items(i).Cells(0).Text = "1" Then
                            Dim drp As New DropDownList
                            drp = CType(Me.SiparisSepet.Items(i).Cells(3).FindControl("drp"), DropDownList)
                            'yetkiliKod = CType(Me.SiparisSepet.Items(i).Cells(3).Controls(0), HtmlControls.HtmlInputHidden).Value
                            yetkiliKod = drp.SelectedValue.ToString '*************** HERE DFIALA13 ****************
                            'yetkiliKod = (CType(Me.SiparisSepet.Items(i).Cells(3).FindControl("drp"), DropDownList).SelectedItem.Value).ToString
                            'yetkiliKod = Me.SiparisSepet.Items(i).Controls(1).ID
                        End If
                    Next
                Else
                    yetkiliKod = "-"
                End If
                'yetkiliKod = "?"

                MyConn3.Open()
                MyComm3.CommandText = "INSERT INTO SIPARISDURUM (SIPARISKOD, ADIM, YETKILIKOD, TARIH, DURUM, ONAY) VALUES " & _
                "('" & talepID & "', '" & rdr5.Item(0) & "', '" & yetkiliKod & "', TO_DATE('" & Date.Now.ToString & "', 'DD/MM/YYYY HH24:MI:SS'), '0', '0')"
                Dim rdr6 As OleDb.OleDbDataReader = MyComm3.ExecuteReader
                rdr6.Close()
                MyConn3.Close()

            End While
            rdr5.Close()

            MyConn2.Close()

        End If
        rdr1.Close()
       MyConn.Close()
        Response.Redirect("Message.aspx?msg=1")
    End Sub

Commented:
OK,
This is one of those situations when you need to rebind your datagrid in order to access the values of the dynamic controls.  The value of the control will be preserved when you post back, but if you haven't re-created the dyanmic control you can't get it using the method you are using.

You have 3 choices to get the values:
1) rebind the grid then read the data using the method you are attempting
2) read the values out of Request.Form() using the control names -- if you don't need to recreate the grid, this might the way to go.
3) create a templated column to hold the combo box, which will preserve the control structure as well as the values between postbacks

Author

Commented:
i thought to create a templated column to hold the combo box, but this will make me lost time.

i don't need to rebind the grid, even it does not work.

can you tell more about 2)

Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
thank you dfiala13,

the datagrid creates coloumns at run time. so that i thought to create a templated column to hold the combo box, will make me lost time. but anyway,

Dim sVal = Request.Form("SiparisSepet:_ctl2:drp")

worked. thank you again..

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.