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

Default item on postback

4 Answers 158 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Patrik
Top achievements
Rank 1
Patrik asked on 27 Jan 2013, 07:03 PM
Hi guys,

I have a RadComboBox that is bound through EntityDataSource. I also added default item, but I need to be able to access that default item on postback in codebehind, because currently I always get the first item in SelectedValue property, that comes from the collection from EntityDataSource. Basically the control is not mandatory and user does not need to select any value.

Here is the code I have been trying to get working:

<telerik:RadComboBox ID="rcbWarehouse" runat="server" DataSourceId="edsWarehouse"
DataValueField="ID" DataTextField="Name" validationGroup="vgOrder">
         <DefaultItem runat="server" Value="null" Text="<%$ Resources: DirectDelivery %>" />
</telerik:RadComboBox>

Thank you!

P.

4 Answers, 1 is accepted

Sort by
0
Hristo Valyavicharski
Telerik team
answered on 30 Jan 2013, 01:57 PM
Hi Patrik,

Here is how the DefaultItem could be accessed on the server:
protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        string text = RadComboBox1.DefaultItem.Text;
        string value = RadComboBox1.DefaultItem.Value;
    }
}
Is that what you are looking for? 

Kind regards,
Hristo Valyavicharski
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Patrik
Top achievements
Rank 1
answered on 30 Jan 2013, 04:07 PM
Hi Hristo,

thank you for your reply, but it did not help me, because the DefaultItem property on RadComboBox won't tell me if the user actually selected the default item. DefaultItem.Selected returns always false or whatever you set in markup. What I need to know is, if the user selected the default item or some other item within the collection, but when the default item is selected then SelectedValue returns always the first value in the collection, it does not return the value of the selected default item.
0
Accepted
Hristo Valyavicharski
Telerik team
answered on 04 Feb 2013, 08:15 AM
Hi Patrik,

The DefaultIItem does not persist in the server/client side RadComboBoxItem's collection and it does not fire any events. However when it is selected the value of the RadComboBox DOM object returns default item’s value. In order to check if the selected item is the default item add hidden filed somewhere on the page and set value to the DefaultItem. We will store the RadCombobox DOM object value in this hidden field.

<asp:HiddenField ID="HiddenField1" runat="server" />
<div>
    <telerik:RadComboBox ID="RadComboBox1"
        runat="server"
        DataSourceID="EntityDataSource1"
        DataTextField="ProductName"
        DataValueField="ProductID"
        AutoPostBack="true">
        <DefaultItem Text="def" Value="-1" />
    </telerik:RadComboBox>
</div>
and add the following lines in the page_load event:
protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack && (RadComboBox1.DefaultItem.Value == HiddenField1.Value))
    {      
            int i = 1;
    }
 
    string scriptKey = "OnSubmitScript";
    string javaScript = "$get('HiddenField1').value = $find('RadComboBox1').get_element().value;";
    this.ClientScript.RegisterOnSubmitStatement(this.GetType(), scriptKey, javaScript);
}
The last three lines will execute this javascript every time before postback occurs. 

Greetings,
Hristo Valyavicharski
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Patrik
Top achievements
Rank 1
answered on 06 Feb 2013, 08:50 PM
Thank you, the idea doing it with javascript helped me, although when I tried your code the form won't postback at all. Didn't have time to find out why, so I just created two different buttons. One for client and one for server, that is hidden. The client button has simple function assigned in its OnClientClicking event.

function SaveOrder() {
            $('#hfSelectedWarehouse').val($find('rcbWarehouse').get_value());
            $('#btnSave').click();
}

This is working as expected.

Thanks,

Patrik
Tags
ComboBox
Asked by
Patrik
Top achievements
Rank 1
Answers by
Hristo Valyavicharski
Telerik team
Patrik
Top achievements
Rank 1
Share this question
or