Solved

getting SelectedValue of a DropDownList in a DataGrid

Posted on 2004-04-05
8
1,024 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..
0
Comment
Question by:waterhidden
  • 4
  • 4
8 Comments
 
LVL 12

Expert Comment

by:dfiala13
ID: 10757162
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.  
0
 
LVL 2

Author Comment

by:waterhidden
ID: 10757402

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

any other ideas?
0
 
LVL 12

Expert Comment

by:dfiala13
ID: 10757413
post your code
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 2

Author Comment

by:waterhidden
ID: 10757468
   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
0
 
LVL 12

Expert Comment

by:dfiala13
ID: 10758042
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
0
 
LVL 2

Author Comment

by:waterhidden
ID: 10763488
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)

0
 
LVL 12

Accepted Solution

by:
dfiala13 earned 150 total points
ID: 10765332
>>i thought to create a templated column to hold the combo box, but this will make me lost time.
Adding a column takes about 2 minutes, but OK.

The Request.Form collection contains the values of all the inputs on the form.  

You can get the values using the full ID of the control, which will iinclude the name of the grid and row.  Usually looks something like this:

SiparisSepet:_ctl2:drp


where _ctl2 is the ID generated for the row.  The header item is is _ctl1, the first Item _ctl2, etc....

So you can get the value of the first dropdown like so...

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

The second...
sVal = Request.Form("SiparisSepet:_ctl3:drp")

and since all you need is a string argument you can always generate the name of the control you are looking for based on the row.

Dim iRowID as Integer

iRowID = 3

sVal = Request.Form("SiparisSepet:_ctl" & iRowID & ":drp")

0
 
LVL 2

Author Comment

by:waterhidden
ID: 10772012
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..

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question