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

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

4 Answers 230 Views
Input
This is a migrated thread and some comments may be shown as answers.
Nick
Top achievements
Rank 1
Nick asked on 04 Feb 2014, 12:13 PM
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>

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 05 Feb 2014, 08:14 AM
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.
0
Nick
Top achievements
Rank 1
answered on 05 Feb 2014, 02:59 PM
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
0
Accepted
Viktor Tachev
Telerik team
answered on 07 Feb 2014, 08:29 AM
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.
0
Nick
Top achievements
Rank 1
answered on 07 Feb 2014, 09:11 AM
Hi Victor

Thanks for the feedback, muck appreciated.

Regards
Nick
Tags
Input
Asked by
Nick
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Nick
Top achievements
Rank 1
Viktor Tachev
Telerik team
Share this question
or