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

SelectedValue from dropdownlist via OnCommand

2 Answers 153 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 29 Jun 2011, 01:34 AM
I'm trying to get the selected value from a dropdownlist in a radgrid from GridTemplate when being called from a command button.
I don't know how to get the value to pass to an update procedure. ddlassessPeriod.SelectedValue doesn't work when its in ItemTemplate.
Here's some sample code:

<telerik:RadGrid ID="RadGrid1" runat="server"  >
 <MasterTableView>
   <Columns>
    <telerik:GridTemplateColumn UniqueName="TemplateColumn>
      <ItemTemplate>
        <asp:DropDownList  ID="ddlassessPeriod" runat="server"
              SelectedValue='<%# Eval("assessPeriod") %>'>
                    <asp:ListItem Value="0" Text="N/A" />
                    <asp:ListItem Value="1" Text="1" />
                     <asp:ListItem Value="2" Text="2" />
         </asp:DropDownList>
        </ItemTemplate>
       </telerik:GridTemplateColumn>
       <telerik:GridTemplateColumn UniqueName="TemplateColumn" AllowFiltering="False">
         <ItemTemplate>
             <asp:Button ID="update" runat="server" Text="Update"
                       OnCommand="update_Command" CommandName='<%#Eval("seid")%>' />
           </ItemTemplate>
         </telerik:GridTemplateColumn>
     </Columns>
  </MasterTableView>
</telerik:RadGrid>

protected void update_Command(object sender, CommandEventArgs e)
     SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["curriculum"] + "");
     conn.Open();
 
     class.update(conn, ddlassessPeriod.SelctedValue, Convert.ToInt32(e.CommandName));
 
}

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 29 Jun 2011, 07:46 AM
Hello Michael,

Since the DropDown List is inside RadGrid, we cannot access it directly. You can access the GridDataItem by using the Namingcontainer property of Button and use FindControl to access the DropDownList. Please find below a sample code snippet that shows the needed approach.

C#.
protected void update_Command(object sender, CommandEventArgs e)
    {  
        GridDataItem Item = (GridDataItem)(sender as Button).NamingContainer;
        DropDownList ddlassessPeriod = (Item.FindControl("ddlassessPeriod") as DropDownList);
    }

Thanks
Shinu
0
Michael
Top achievements
Rank 1
answered on 29 Jun 2011, 02:02 PM
Thanks, that works great!
Tags
Grid
Asked by
Michael
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Michael
Top achievements
Rank 1
Share this question
or