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

Set SelectedText from code behind

7 Answers 75 Views
DropDownTree
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
Veteran
Iron
David asked on 24 Jan 2020, 04:19 PM

tried rddt1.SelectedText = "bla" and it didn't work

Any suggestions?

Thank you

7 Answers, 1 is accepted

Sort by
0
Vessy
Telerik team
answered on 28 Jan 2020, 12:40 PM

Hi David,

I tested the SelectedText functionality and it works properly at my end. Can you, please, confirm that you are setting the SelectedText after the RadDropDownTree is successfully data-bound?

Below you can see the setup I used for my test - are you facing the same behavior with it?

        <telerik:RadDropDownList ID="rddt1" runat="server" DataTextField="TextField" DataValueField="ValueField" DefaultMessage="NONE">
        </telerik:RadDropDownList>

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            rddt1.DataSource = GetData();
            rddt1.DataBind();
        }

        rddt1.SelectedText = "bla";
    }

    private DataTable GetData()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("TextField");
        dt.Columns.Add("ValueField");

        for (char a = 'a'; a <= 'z'; a++)
        {
            string item = new string(a, 7);

            for (char b = 'a'; b < 'c'; b++)
            {
                item += new string(b, 3);
                dt.Rows.Add(item, item);
            }
        }

        dt.Rows.Add("bla", "blaValue");

        return dt;

    }

Regards,
Vessy
Progress Telerik

Get quickly onboarded and successful with UI for ASP.NET AJAX with the Virtual Classroom technical trainings, available to all active customers. Learn More.
0
David
Top achievements
Rank 1
Veteran
Iron
answered on 28 Jan 2020, 01:26 PM

Hi Vessy,

  Hmm, I do use the code after data-bound.

  Did you try your code for RadDropDownTree (you seem to use RadDropDownList here)?

Thank you

David

  

0
Vessy
Telerik team
answered on 28 Jan 2020, 02:09 PM

Hi David,

Please, excuse me for the lapse, I have copied the wrong snippet. Yes, I can confirm that the behavior at my end is the same both with RadDropDownList and RadDropDownTree (using the latest 2020.1.114 version of the controls).

I am attaching my whole test page for your reference here.

Regards,
Vessy
Progress Telerik

Get quickly onboarded and successful with UI for ASP.NET AJAX with the Virtual Classroom technical trainings, available to all active customers. Learn More.
0
David
Top achievements
Rank 1
Veteran
Iron
answered on 28 Jan 2020, 02:47 PM

Not sure what to say

My definition is as follows:

<telerik:RadDropDownTree ID="rddtAirports" runat="server" Skin="Bootstrap" style="display:none;"
                                   Width="460px" CheckBoxes="SingleCheck" CheckNodeOnClick="true"                                  
                                   EnableFiltering="true" FilterSettings-EmptyMessage="Type to find"
                                   DefaultMessage="Please select your Airports"                                    
                                   DataValueField="ID" DataTextField="Name"
                                   OnClientEntryAdding="RadDropDownTree1_OnClientEntryAdding"
                                   OnClientEntryRemoving="RadDropDownTree1_OnClientEntryRemoving">
                 <ButtonSettings ShowCheckAll="true" ShowClear="true"/>
                 <Localization Clear="Clear All"/>
          </telerik:RadDropDownTree> 

 

 function RadDropDownTree1_OnClientEntryAdding(sender, args) {
         
        if (sender._selectedValue != '') {

            setTimeout(function () {
                sender._get_inputElement().text(sender._selectedValue);
            }, 0);

        }
    }

    function RadDropDownTree1_OnClientEntryRemoving(sender, args) {
        
        if (sender._selectedValue.split(",").length != 2) {

            setTimeout(function () {
                sender._get_inputElement().text(sender._selectedValue);
            }, 0);

        }
    }

This code achieves that i have values instead of text in selectedText area, which is helpful if many selected.

However, after i do postback, values are getting replaced by text, and rddtAirports.SelectedText = rddtAirports.SelectedValue

does not help.

Not sure if it helps

Thank you

0
Accepted
Vessy
Telerik team
answered on 31 Jan 2020, 10:51 AM

Hi David,

If I understand the problem properly, you can showing the selected values instead of the selected text, which does not work when the value is selected from the server. Am I correct? If this is the case, it is expected as the OnClientEntryAdding event is not triggered when the selected text is set from the server, but only when the node is being checked: 
https://docs.telerik.com/devtools/aspnet-ajax/controls/dropdowntree/client-side-programming/events/oncliententryadding

As the applied show-value-instead-of-text logic is only visually applied in the browser, the control reloads the selected text from its bound data once it gets updated from the server. A possible approach you can consider (if it is applicable to your scenario) is handling the RadDropDownTree OnClientLoad event, applying a similar to the OnClientEntryAdding text-replacing logic in it.

The other option I can think of, is setting the "ID" field both as DataValueField and DataTextField value, using the "Name" only in the DropDownNode template, so the actual selected text will be actually the value. For example:

            <telerik:RadDropDownTree ID="rddtAirports" runat="server" Skin="Bootstrap"
                Width="460px" CheckBoxes="SingleCheck" CheckNodeOnClick="true"
                EnableFiltering="true" FilterSettings-EmptyMessage="Type to find"
                DefaultMessage="Please select your Airports"
                DataValueField="ID" DataTextField="ID">
                <ButtonSettings ShowCheckAll="true" ShowClear="true" />
                <Localization Clear="Clear All" />
                <DropDownNodeTemplate>
                    <%# Eval("Name") %>
                </DropDownNodeTemplate>
            </telerik:RadDropDownTree>
            <asp:Button ID="btn1" runat="server" Text="Do PostBack" />

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            rddtAirports.DataSource = GetData();
            rddtAirports.DataBind();
            rddtAirports.SelectedText = "value_bla";
        }
    }

    private DataTable GetData()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Name");
        dt.Columns.Add("ID");

        dt.Rows.Add("text_bla", "value_bla");
        dt.Rows.Add("text_bla1", "value_bla1");
        dt.Rows.Add("text_bla2", "value_bla2");
        dt.Rows.Add("text_bla3", "value_bla3");
        dt.Rows.Add("text_bla4", "value_bla4");
        dt.Rows.Add("text_bla5", "value_bla5");
        dt.Rows.Add("text_bla6", "value_bla6");
        dt.Rows.Add("text_bla7", "value_bla7");
        dt.Rows.Add("text_bla8", "value_bla8");
        dt.Rows.Add("text_bla9", "value_bla9");

        return dt;
    }

Regards,
Vessy
Progress Telerik

Get quickly onboarded and successful with UI for ASP.NET AJAX with the Virtual Classroom technical trainings, available to all active customers. Learn More.
0
David
Top achievements
Rank 1
Veteran
Iron
answered on 31 Jan 2020, 08:37 PM

Very elegant solution!

Thank you

0
Vessy
Telerik team
answered on 03 Feb 2020, 07:56 AM

Hi,

You are welcome, David - I am glad the suggested solution fits the needs of your application.

Kind regards,
Vessy
Progress Telerik

Get quickly onboarded and successful with UI for ASP.NET AJAX with the Virtual Classroom technical trainings, available to all active customers. Learn More.
Tags
DropDownTree
Asked by
David
Top achievements
Rank 1
Veteran
Iron
Answers by
Vessy
Telerik team
David
Top achievements
Rank 1
Veteran
Iron
Share this question
or