Default item on postback

5 posts, 1 answers
  1. Patrik
    Patrik avatar
    17 posts
    Member since:
    Jan 2013

    Posted 27 Jan 2013 Link to this post

    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.
  2. Hristo Valyavicharski
    Admin
    Hristo Valyavicharski avatar
    975 posts

    Posted 30 Jan 2013 Link to this post

    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.
  3. Patrik
    Patrik avatar
    17 posts
    Member since:
    Jan 2013

    Posted 30 Jan 2013 Link to this post

    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.
  4. Answer
    Hristo Valyavicharski
    Admin
    Hristo Valyavicharski avatar
    975 posts

    Posted 04 Feb 2013 Link to this post

    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.
  5. Patrik
    Patrik avatar
    17 posts
    Member since:
    Jan 2013

    Posted 06 Feb 2013 Link to this post

    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
Back to Top