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

setting and finding radcomboboxitems on a radgrid

3 Answers 48 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Philip
Top achievements
Rank 1
Philip asked on 27 May 2010, 11:05 PM
I have a radgrid which gets bound to a dataset returned by a stored procedure. I have 3 hyperlink 'options' that gets generated dynamically from the DB. Instead of creating 3 columns with links, I just wanted a dropdownlist that users can select from and a button that will trigger the redirect to a page referenced by the links.

Also, I added the button on the template column but can't get it to display , I'll just create an image button then on another template column. But on the on-click event, when I do a findcontrol, I can't seem to see the radcombobox so I can reference the selected value and redirect the user to the link. Please advise. Code samples would help. Thanks!

I have code similar to below:

 

 

<telerik:GridTemplateColumn HeaderText="Options" UniqueName="Options">

 

 

 

<ItemTemplate>

 

 

 

<telerik:RadComboBox ID="rcbOptions" runat="server" AllowCustomText="True"

 

 

 

HighlightTemplatedItems="True" EmptyMessage="(Select)">

 

 

 

<Items>

 

 

 

<telerik:RadComboBoxItem Text="Option1" Value='<%#Eval("URL1") %>' />

 

 

 

<telerik:RadComboBoxItem Text="Option2" Value='<%#Eval("URL2") %>' />

 

 

 

<telerik:RadComboBoxItem Text="Option3" Value='<%#Eval("URL3") %>' />

 

 

 

</Items>

 

 

 

</telerik:RadComboBox>

 

 

 

</ItemTemplate>

 

 

 

</telerik:GridTemplateColumn>


 

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 28 May 2010, 06:55 AM
Hello Philip,

In order to access the RadComboBox in OnClick event of ImageButton , you have to access the GridDataItem using NamingContainer property of ImageButton and then by using FindControl method get reference to the RadCombobox. Check out the following code snippet.

ASPX:
 <telerik:GridTemplateColumn HeaderText="Options" UniqueName="Options"
         <ItemTemplate> 
           <telerik:RadComboBox ID="rcbOptions" runat="server" AllowCustomText="True" HighlightTemplatedItems="True" 
                                EmptyMessage="(Select)"
              <Items> 
                <telerik:RadComboBoxItem Text="Option1" Value='<%#Eval("URL1") %>' /> 
                <telerik:RadComboBoxItem Text="Option2" Value='<%#Eval("URL2") %>' /> 
                <telerik:RadComboBoxItem Text="Option3" Value='<%#Eval("URL3") %>' /> 
              </Items> 
           </telerik:RadComboBox> 
          </ItemTemplate> 
 </telerik:GridTemplateColumn> 
 <telerik:GridTemplateColumn> 
           <ItemTemplate> 
               <asp:ImageButton ID="ImageButton1" runat="server" OnClick="ImageButton1_Click" /> 
          </ItemTemplate> 
</telerik:GridTemplateColumn> 

C#:
  
 protected void ImageButton1_Click(object sender, ImageClickEventArgs e) 
    { 
        ImageButton btn = (ImageButton)sender; 
        GridDataItem item = (GridDataItem)btn.NamingContainer; 
        RadComboBox combo = (RadComboBox)item.FindControl("rcbOptions"); 
        Response.Redirect(combo.SelectedValue); 
    } 

Regards,
Shinu.


0
Philip
Top achievements
Rank 1
answered on 28 May 2010, 08:42 PM
Thanks, Shinu. However, still having issues.. note that I am using a radgrid under a radajaxpanel. Using the code you gave me, I a get a postback error when I click on the image button. I tried using a gridbuttoncolumn and same thing. However, when I changed the gridbuttoncolumn to use a link button it goes through.

 

<telerik:GridTemplateColumn HeaderText="Options" UniqueName="Options">

 

 

<ItemTemplate>

 

 

<asp:DropDownList ID="ddlOptions" runat="server">

 

 

<asp:ListItem Text="(Select)" Value="(Select)"></asp:ListItem>

 

 

<asp:ListItem Text="Option1" Value="Option1"></asp:ListItem>

 

 

<asp:ListItem Text="Option2" Value="Option2"></asp:ListItem>

 

 

<asp:ListItem Text="Option3" Value="Option3"></asp:ListItem>

 

 

</asp:DropDownList>

 

 

</ItemTemplate>

 

 

</telerik:GridTemplateColumn>

 

 

<telerik:GridButtonColumn ButtonType="LinkButton" Text="Go" CommandName="MyCommand" UniqueName="ButtonColumn">

 

 

</telerik:GridButtonColumn>

 


I prefer to use an image than a link if there is a way around this. Thanks.

Also, I get a null value when it tries to find the radcombobox, I changed it to a dropdownlist (see code) and was able to find it..

I tried a different approach too, since I have an 'id' that is being passed from the DB, I can pass this to a session variable and call pages that will load info from a stored procedure based on this session variable. However, when I select a dropdown value and hit the link button, the selectedvalue is always the first option (Select), how can I fix this? Thanks.


 

protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)

 

{

if

 

(e.CommandName == "MyCommand")

 

{
Session["ID"] = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString();

GridDataItem

 

item = e.Item as GridDataItem;

 

DropDownList

 

 ddl = (DropDownList)item.FindControl("ddlOptions");

 

 

if (ddl.SelectedValue == "Option1")

 

{

Response.Redirect(

"Page1.aspx");

 

}
...

}
}



0
Radoslav
Telerik team
answered on 03 Jun 2010, 08:27 AM
Hello Philip,

I am sending you a simple example which demonstrates the desired functionality. Please check it out and let me know if it helps you.

Additionally to find the control into the TemplateColumn on the server, you need to use the following code snippet:
DropDownList ddl = (DropDownList)item["ColumnUniqueName"].FindControl("ControlID");

I hope this helps.

All the best,
Radoslav
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Grid
Asked by
Philip
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Philip
Top achievements
Rank 1
Radoslav
Telerik team
Share this question
or