This is a migrated thread and some comments may be shown as answers.

Change dropdownList to a Textbox based on selectedindexChanged on dropdownlist

6 Answers 121 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Aret
Top achievements
Rank 1
Aret asked on 07 Aug 2012, 07:52 PM
I have a DropDownList that appears when my RadGrid is in edit mode or insert mode. 

     <radG:GridTemplateColumn DataField="Covering" HeaderText="Covering"
                        UniqueName="Covering" SortExpression="Covering" >
                        <ItemTemplate>
                               <asp:Label ID="lblCovering" runat="server" Text='<%# Eval("Covering") %>' />
                      </ItemTemplate>

                      <EditItemTemplate>
                               <asp:Label ID="lblCovering2" runat="server" Text='<%# Eval("Covering") %>' Visible="false" />
                               <asp:Textbox ID="txtCovering" runat="server" Text='<%# Eval("Covering") %>' Visible="false" />
                        <asp:DropDownList ID="ddCovering" OnSelectedIndexChanged="ddCovering_SelectedIndexChanged" runat="server" AutoPostBack="true" Visible="True" />
      
                         </EditItemTemplate>
                         </radG:GridTemplateColumn>


As you can see, my dropdownlist has an autopostback set to true and is run by the event called ddCovering_SelectedIndexChanged.  Within the SelectedIndexChanged Event,   the dropdown will change to a textbox based on what the user selects from the dropdownlist WHILE THE RADGRID is in Edit mode.  Here is the code below that drives that event.... 



 Protected Sub ddCovering_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)


        For Each item As GridDataItem In RadGrid_NoReferrals.EditItems

            Dim ddCovering As DropDownList = DirectCast(sender, DropDownList)

            Dim txtCovering As TextBox = DirectCast(item.FindControl("txtCovering"), TextBox)

            If ddCovering.SelectedValue = Nothing Then
                ddCovering.Visible = False
               
                txtCovering.Visible = True
            End If
     
        Next

End Sub


My only problem is that this only happens when the RadGrid is in Edit mode, but not when its in Insert Mode.  I need the DropDownBox to also change to a Textbox in Insert Mode as well.  How would I go about doing this....?  Please Help!

6 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 08 Aug 2012, 05:35 AM
Hi Aret,

Try the following code snippet to show/hide TextBox/DropDownList in SelectedIndexChanged event in insert/edit mode.

VB:
Protected Sub ddCovering_SelectedIndexChanged(sender As Object, e As EventArgs)
    If RadGrid1.MasterTableView.IsItemInserted Then
        Dim DropDownList1 As DropDownList = DirectCast(sender, DropDownList)
        Dim eitem As GridEditFormInsertItem = DirectCast(d.NamingContainer, GridEditFormInsertItem)
        Dim txtCovering As TextBox = DirectCast(eitem.FindControl("txtCovering"), TextBox)
        If DropDownList1.SelectedValue Is Nothing Then
            DropDownList1.Visible = False
            txtCovering.Visible = True
        End If
    Else
        Dim DropDownList1 As DropDownList = DirectCast(sender, DropDownList)
        Dim eitem As GridEditableItem = DirectCast(d.NamingContainer, GridEditableItem)
        Dim txtCovering As TextBox = DirectCast(eitem.FindControl("txtCovering"), TextBox)
        If DropDownList1.SelectedValue Is Nothing Then
            DropDownList1.Visible = False
            txtCovering.Visible = True
        End If
    End If
End Sub

Thanks,
Shinu.
0
Aret
Top achievements
Rank 1
answered on 08 Aug 2012, 12:03 PM
I am getting an error in your solution at this line of code you provided...

Dim eitem As GridEditFormInsertItem = DirectCast(d.NamingContainer, GridEditFormInsertItem)

You use 'd' for namingcontainer but 'd' is never declared in my code..... What are you declaring d as...?
0
Shinu
Top achievements
Rank 2
answered on 08 Aug 2012, 12:07 PM
Hi Aret,

Please change the code as shown below.

VB:
Dim eitem As GridEditFormInsertItem = DirectCast(DropDownList1.NamingContainer,GridEditFormInsertItem)

Thanks,
Shinu.
0
Aret
Top achievements
Rank 1
answered on 08 Aug 2012, 12:11 PM
sorry to be annoying, but i just changed the code to what you said and now I am getting this error at the same line of code where I made my change.  Would you know what to make of it...?

Dim eitem As GridEditFormInsertItem = DirectCast(DropDownList1.NamingContainer,GridEditFormInsertItem)

Error:
Unable to cast object of type 'Telerik.WebControls.GridDataInsertItem' to type 'Telerik.WebControls.GridEditFormInsertItem'.
0
Aret
Top achievements
Rank 1
answered on 08 Aug 2012, 12:21 PM
Also I want to make a note,  it actually does work for the edit mode, but when I go into insert mode and change the selected value of the dropdownlist that is when I get that error.  So it works for edit, but not for insert mode.
0
Aret
Top achievements
Rank 1
answered on 08 Aug 2012, 12:25 PM
OK, i JUST FIGURED IT OUT..
In the first condition of the IF statement instead of using GridEditFormInsertItem,  I used GridDataItem and it worked...
Now It works for both edit mode AND insert mode....

Also for some reason you need to change 'Is Nothing' to ' = Nothing ' for the condition to work in my case.....  just a small note.


Thank You so much... i Just want to mention that you guys provide GREAT feedback and you guys always lead me to the answer to my programming problems.  Keep up the good work!!
Tags
Grid
Asked by
Aret
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Aret
Top achievements
Rank 1
Share this question
or