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

combobox in repeater - how to get the selected value

4 Answers 470 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Petr
Top achievements
Rank 2
Petr asked on 15 Feb 2009, 02:31 PM
Hi,

could you please help me with the following simple case: radcombobox in a repeater:

<asp:DataList ID="repeater" runat="server">
   <ItemTemplate>
      <span><%#DataBinder.Eval(Container.DataItem, "Label")%></span>
      <telerik:RadComboBox runat="server" AutoPostBack="false" DataMember="<%#DataBinder.Eval(Container.DataItem, "Combo")%>" DataTextField="<%#DataBinder.Eval(Container.DataItem, "Text")%>"
DataValueField="<%#DataBinder.Eval(Container.DataItem, "Value")%>" OnDataBinding="comboBinding" />
    </ItemTemplate>
</asp:repeater>

protected void comboBinding(Object sender, EventArgs e)
{
    RadComboBox box = sender as RadComboBox;
    box.DataSource = ds;
    box.ID = string.Format("Filter_{0}", box.DataMember);
}

protected void Page_Load(object sender, EventArgs e)

if (! IsPostBack)
{
    repeater.DataSource = dt;
    repeater.DataBind();
}
else
{
    // here I need to find out what was selected in all those combo boxes
}

I'd like to know the selected values of all combo boxes after PostBack but I can't find out how to do that. I set the IDs of the comboboxes to meaningful values but they probably don't exist yet in the PostBack as Page.FindControl("Filter_xx") didn't find them. How else can I obtain the combo boxes? Should I DataBind the repeater first? I have tried something like that but got an error. Please advise.

Thanks

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 16 Feb 2009, 12:20 PM
Hello,

You can set the ID of the combobox  declaratively and then try out the following code in the PageLoad event to access the RadComboBoxes selected items:
aspx:
<asp:DataList ID="repeater"  runat="server">  
   <ItemTemplate>  
      <span><%#DataBinder.Eval(Container.DataItem, "Label")%></span>  
      <telerik:RadComboBox runat="server" AutoPostBack="false" DataMember="<%#DataBinder.Eval(Container.DataItem, "Combo")%>" DataTextField="<%#DataBinder.Eval(Container.DataItem, "Text")%>" 
DataValueField="<%#DataBinder.Eval(Container.DataItem, "Value")%>" OnDataBinding="comboBinding" /> 
    </ItemTemplate>  
 </asp:DataList>  

cs:
protected void comboBinding(Object sender, EventArgs e) 
    RadComboBox box = sender as RadComboBox; 
    box.DataSource = ds
    box.ID = string.Format("Filter_{0}", box.DataMember); 
 
protected void Page_Load(object sender, EventArgs e) 
 
if (! IsPostBack) 
    repeater.DataSource = dt
    repeater.DataBind(); 
else 
     foreach (DataListItem item in repeater.Items) 
        { 
            RadComboBox combo = (RadComboBox)item.FindControl(string.Format("Filter_{0}", combo.DataMember)); 
            string strtxt = combo.SelectedItem.Text; 
        } 

Thanks
Princy.
0
Petr
Top achievements
Rank 2
answered on 16 Feb 2009, 04:01 PM
Princy,

thanks for your reply but I wrote in my original mail that FindControl("Filter_{0}", ..) does not find anything. I had to resort to remembering the RadComboBox's UniqueID (in OnDataBinding) and using that one for the FindControl. I don't think it's the correct approach, though. It's too cumbersome...
0
Princy
Top achievements
Rank 2
answered on 17 Feb 2009, 06:41 AM
Hi,

You can either try setting the Id of your ComboBox declaratively(in the aspx) or create the ComboBox dynamically(in the code behind). You cannot find the control using Page.FindControl(controlId), since the RadComboBox is contained inside the DataList. So you will have to loop through the DataListItems and then find the combobox as shown below:
aspx:
<asp:DataList ID="repeater"  runat="server">   
   <ItemTemplate>   
      <span><%#DataBinder.Eval(Container.DataItem, "Label")%></span>   
      <telerik:RadComboBox ID="RadComboBox1" runat="server" AutoPostBack="false" DataMember="<%#DataBinder.Eval(Container.DataItem, "Combo")%>" DataTextField="<%#DataBinder.Eval(Container.DataItem, "Text")%>"  
DataValueField="<%#DataBinder.Eval(Container.DataItem, "Value")%>" OnDataBinding="comboBinding" />  
    </ItemTemplate>   
 </asp:DataList>  

cs:
protected void Page_Load(object sender, EventArgs e)  
  
if (! IsPostBack)  
{  
    repeater.DataSource = dt;  
    repeater.DataBind();  
}  
else  
{  
     foreach (DataListItem item in repeater.Items)  
        {  
            RadComboBox combo = (RadComboBox)item.FindControl("RadComboBox1");  
            string strtxt = combo.SelectedItem.Text;  
        }  
}  

Thanks
Princy.
0
Matt Jones
Top achievements
Rank 1
answered on 17 Feb 2009, 12:09 PM
Princy,

your last suggestion sounds very logical but I have tried it and it didn't work (the FindControl returned null). Not sure why. As I have a working solution now (based on the UniqueID) I'll stick with it - I've got other things to do, anyway. Thanks!
Tags
ComboBox
Asked by
Petr
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 2
Petr
Top achievements
Rank 2
Matt Jones
Top achievements
Rank 1
Share this question
or