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

[Solved] Access dropdownlist in EditItemTemplate from _ItemCommand event

2 Answers 181 Views
Grid
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 01 May 2013, 08:27 PM
Hello,
I have a RadGrid that I am trying to place three controls inside a GridTemplateColumn/EditItemTemplate. Two buttons and a drop down list. When one of the buttons is fired I would like to see what value is selected in the DropDownList. I can't seem to access the control.

ASPX example:

<telerik:GridTemplateColumn>
                <EditItemTemplate>
                <p></p>
                    <asp:Button runat="server" ID="btnAddBox" CommandArgument="WHAT!" CommandName="AddBox" Text="Add Box To This Batch" />
                    <p></p>
                    <asp:DropDownList runat="server" ID="ddBoxList"></asp:DropDownList>
                    <asp:Button runat="server" ID="btnRemoveBox" CommandArgument="WHAT!" CommandName="RemoveBox" Text="Remove Box" />
                </EditItemTemplate>
            </telerik:GridTemplateColumn>


VB example:

Protected Sub rgBatchView_ItemCommand(ByVal source As Object, ByVal e As GridCommandEventArgs) Handles rgBatchView.ItemCommand
        If e.CommandName = "AddBox" Then
            Dim item = CType(e.Item, GridDataItem)
            Dim BatchID = item.GetDataKeyValue("BatchID")
            Dim b As New batch
            b.LoadBatch(BatchID)
            b.AddBox()
            notify.Show("Box Added!")
        ElseIf e.CommandName = "RemoveBox" Then
            Dim item = CType(e.Item, GridDataItem)
            Dim BatchID = item.GetDataKeyValue("BatchID")
            Dim b As New batch
            Dim dd As DropDownList = e.Item.Parent.FindControl("ddBoxList") ' **** This returns nothing, Also tried without ".parent"
            b.LoadBatch(BatchID)
            b.RemoveBox(dd.SelectedValue)
            notify.Show("Box Removed")
        End If
    End Sub

Thanks for any help!

2 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 02 May 2013, 04:15 AM
Hi,

Try the following.
VB:
Protected Sub Button2_Click(sender As Object, e As EventArgs)
    Dim btn As Button = DirectCast(sender, Button)
    Dim item As GridEditFormItem = DirectCast(btn.NamingContainer, GridEditFormItem)
    Dim ddl As DropDownList = DirectCast(item.FindControl("DropDownList1"), DropDownList)
    Dim value As String = ddl.SelectedValue
End Sub

Thanks,
Shinu
0
David
Top achievements
Rank 1
answered on 02 May 2013, 01:34 PM
Shinu,

Thanks for the code! I moved this into the button's click event and it worked perfect.

Here is the code working:
<telerik:GridTemplateColumn>
            <EditItemTemplate>
            <p></p>
                <asp:Button runat="server" ID="btnAddBox" CommandArgument="WHAT!" CommandName="AddBox" Text="Add Box To This Batch" />
                <p></p>
                <asp:DropDownList runat="server" ID="ddBoxList"></asp:DropDownList>
                <asp:Button runat="server" ID="btnRemoveBox" CommandArgument="WHAT!" CommandName="RemoveBox" Text="Remove Box" OnClick="btnRemoveBox_Click" />
            </EditItemTemplate>
        </telerik:GridTemplateColumn>
Protected Sub btnRemoveBox_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim btn As Button = DirectCast(sender, Button)
        Dim item As GridEditFormItem = DirectCast(btn.NamingContainer, GridEditFormItem)
        Dim ddl As DropDownList = DirectCast(item.FindControl("ddBoxList"), DropDownList)
        Dim value As String = ddl.SelectedValue
 
        Dim BatchID = item.GetDataKeyValue("BatchID")
        Dim b As New batch
 
        b.LoadBatch(BatchID)
        b.RemoveBox(ddl.SelectedValue)
 
        rgBatchView.Rebind()
 
        notify.Show("Box Removed")
    End Sub
Tags
Grid
Asked by
David
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
David
Top achievements
Rank 1
Share this question
or