Conditionally setting RadNumericTextBox\Type when binding the TextBox in a ListView

5 posts, 1 answers
  1. Nick
    Nick avatar
    3 posts
    Member since:
    Oct 2012

    Posted 04 Feb 2014 Link to this post

    Hi

    I have a RadNumericTextBox in  a ListView. II'm trying to conditionally set the RadNumericTextBox's Type from Number to Percent when binding the ListView.

    I was trying something like this, with no success. Anybody any ideas.

    <telerik:RadListView ID="lvCover"  runat="server" Width="95%" DataKeyNames="Id"
        ItemPlaceholderID="TempContainer"  AllowPaging="false" >
        <LayoutTemplate>
            <asp:PlaceHolder ID="TempContainer" runat="server"></asp:PlaceHolder
        </LayoutTemplate>
        <ItemTemplate>
            <div>
                <table style="width:100%">                               
                    <tr>    
                        <td style="width:50%; text-align:left">
                            <%#Eval("CoverType.Description")%>
                        </td>
                        <td style="width:50%; text-align:left">
                            <telerik:RadNumericTextBox ID="txtRate" runat="server"  MaxLength="7"
                                Type='<%#Eval("CoverType.Code") == "NCB" ? Telerik.Web.UI.NumericType.Number : Telerik.Web.UI.NumericType.Percent %>'
                                Text='<%#Eval("Rate")%>'>
                                <NegativeStyle Resize="None" />
                                <NumberFormat DecimalDigits="3" ZeroPattern="n" />
                                <EmptyMessageStyle Resize="None" />
                                <ReadOnlyStyle Resize="None" />
                            </telerik:RadNumericTextBox>
                        </td>
                    </tr
                </table>
            </div>
        </ItemTemplate>
    </telerik:RadListView>
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 05 Feb 2014 in reply to Nick Link to this post

    Hi Nick,

    You can handle the OnDataBinding event of RadNumericTextBox to achieve your scenario. Please have a look into the following code snippet.

    ASPX:
    <telerik:RadNumericTextBox ID="txtRate" runat="server" MaxLength="7"                                                                                              OnDataBinding="txtRate_DataBinding" DbValue='<%# Eval("UnitPrice") %>' >
    </telerik:RadNumericTextBox>

    C#:
    int i;
    protected void Page_Init(object sender, EventArgs e)
    {
         i = Convert.ToInt16(HiddenField1.Value);
    }
    protected void txtRate_DataBinding(object sender, EventArgs e)
    {
    RadNumericTextBox number =(RadNumericTextBox) RadListView1.Items[i].FindControl("txtRate");
     {
       if(//condition code)
           number.Type = NumericType.Number;
       else
            number.Type = NumericType.Percent;
       i++;
     }
    }

    Hope this will helps you.
    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Nick
    Nick avatar
    3 posts
    Member since:
    Oct 2012

    Posted 05 Feb 2014 in reply to Shinu Link to this post

    Thank you Shinu

    I properly should have said that I have this working in the code, but I was trying to find out if this could be done more efficiently in the front end.

    Here is working code.

    <telerik:RadListView ID="lvCover"  runat="server" Width="95%" DataKeyNames="Id, Code"
    ItemPlaceholderID="TempContainer"  AllowPaging="false"  OnItemDataBound="lvCover_ItemDataBound" >
    <LayoutTemplate>
        <asp:PlaceHolder ID="TempContainer" runat="server"></asp:PlaceHolder
    </LayoutTemplate>
    <ItemTemplate>
        <div>
            <table style="width:100%">                               
                <tr>    
                    <td>
                        <%#Eval("AqmCoverType.Description")%>
                    </td>
                    <td >
                        <telerik:RadNumericTextBox ID="txtRate" runat="server"  MaxLength="7"
                            DbValue='<%#Eval("Rate")%>'>
                        </telerik:RadNumericTextBox>
                    </td>
                </tr
            </table>
        </div>
    </ItemTemplate>
    </telerik:RadListView>

    protected void lvCover_ItemDataBound(object sender, RadListViewItemEventArgs e)
    {
        Telerik.Web.UI.RadListViewDataItem item = e.Item as Telerik.Web.UI.RadListViewDataItem;
     
        RadNumericTextBox txtRate = (RadNumericTextBox)(item.FindControl("txtRate"));
        string typeCode = item.GetDataKeyValue("Code").ToString();
     
        if (typeCode == "Type")
        {
            txtRate.MaxValue = 200000;
            txtRate.Type = Telerik.Web.UI.NumericType.Number;
        }
        else
        {
            txtRate.MaxValue = 20;
            txtRate.Type = Telerik.Web.UI.NumericType.Percent;
        }
    }


    Thanks
    Nick
  5. Answer
    Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 07 Feb 2014 Link to this post

    Hello Nick,

    Both methods for setting the type of a RadNumericTextBox control should work. Using one or the other is more a personal preference than anything else.

    The Eval() method and ItemDataBound are called when an item is bound so they are both executed at the same place in the page life cycle. Because of this there should not be any difference in performance.

    If you would like to have more versatility and control over the data you could use the code-behind. In case you are implementing a simpler scenario you could change the type of the input control in the markup.

    For convenience I have prepared a sample project illustrating both approaches. It is attached to this post.

    Regards,
    Viktor Tachev
    Telerik
    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 UI for ASP.NET AJAX, subscribe to the blog feed now.
  6. Nick
    Nick avatar
    3 posts
    Member since:
    Oct 2012

    Posted 07 Feb 2014 Link to this post

    Hi Victor

    Thanks for the feedback, muck appreciated.

    Regards
    Nick
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017