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

Access values from EditItemTemplate column

6 Answers 300 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Soumya Parla
Top achievements
Rank 1
Soumya Parla asked on 11 Jan 2011, 11:19 PM
Hi,

I am having issues accessing values from a Template Column that I created. Here is what I have.

....
....
  <telerik:GridTemplateColumn UniqueName="ItemDiscount" HeaderText="Discount">
                                    <EditItemTemplate>                                        
                                                    <asp:RadioButtonList ID="rdListItemDiscount" runat="server">
                                                        <asp:ListItem Selected="True">Discount in %</asp:ListItem>
                                                        <asp:ListItem>Discount in $</asp:ListItem>
                                                    </asp:RadioButtonList>
                                                    &nbsp;
                                                    <telerik:RadNumericTextBox ID="txtItemDiscountVal" runat="server" Width="20px">
                                                    </telerik:RadNumericTextBox>                                           
                                    </EditItemTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="lblLineDiscountVal" runat="server" Text="No Discount Applied"></asp:Label>
                                    </ItemTemplate>
                                </telerik:GridTemplateColumn>
                                <telerik:GridEditCommandColumn UniqueName="EditCommandColumn" EditText="Add Discount"
                                    UpdateText="Update Discount">
                                    <ItemStyle CssClass="MyImageButton" />
                                </telerik:GridEditCommandColumn>
.....
.....
Code Behind:

Protected Sub rgLineItems_ItemUpdated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridUpdatedEventArgs) Handles rgLineItems.ItemUpdated
        Try
            Dim i As Integer = 1
            Dim CellValue As String
            Dim txtVal As String
            CellValue = CType(rgLineItems.Items(i).FindControl("rdListItemDiscount"), RadioButtonList).SelectedItem.Value
            txtVal = CType(rgLineItems.Items(i).FindControl("txtItemDiscountVal"), TextBox).Text
        Catch ex As Exception
        End Try
    End Sub

I would like to get the selected value from the radiobuttonlist and the text value upon clicking on Update Discount. Once this is done, I would like to change the EditItemTemplate column to a value entered by the user in the textbox. Let me know where I am going wrong and is this really possible?

Thanks in advance!
Soumya

6 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 12 Jan 2011, 06:17 AM
Hello Soumya,

In UpdateCommand, you can access the control and then get the value like below.

Vb.Net:
Protected Sub rgLineItems_UpdateCommand(source As Object, e As GridCommandEventArgs)
    Dim edititem As GridEditableItem = DirectCast(e.Item, GridEditableItem)
    Dim txt As TextBox = DirectCast(edititem.FindControl("txtItemDiscountVal"), TextBox)
    Dim txtValue As String = txt.Text
End Sub

Also refer the following documentation which uses  ExtractValuesFromItem() method  to get the values from updated item.
Updating values in-place and with edit forms
 
Thanks,
Princy.
0
Soumya Parla
Top achievements
Rank 1
answered on 12 Jan 2011, 04:02 PM
Thanks Princy for a quick reply. But this did not resolve my issue. I am getting an InvalidCastException on debugging the code.
The exact exception is:

System.InvalidCastException was unhandled by user code
  Message=Unable to cast object of type 'Telerik.Web.UI.RadNumericTextBox' to type 'System.Web.UI.WebControls.TextBox'.
  Source=App_Web_1o5k4sfx
  StackTrace:
       at _Default.rgLineItems_UpdateCommand(Object sender, GridCommandEventArgs e) in c:\My Documents\Visual Studio 2010\Projects\SalesQuoteApplication\Default.aspx.vb:line 209
       at Telerik.Web.UI.RadGrid.OnUpdateCommand(GridCommandEventArgs e)
       at Telerik.Web.UI.RadGrid.CallOnUpdateCommand(GridCommandEventArgs e)
       at Telerik.Web.UI.GridCommandEventArgs.ExecuteCommand(Object source)
       at Telerik.Web.UI.RadGrid.OnBubbleEvent(Object source, EventArgs e)
       at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
       at Telerik.Web.UI.GridItem.OnBubbleEvent(Object source, EventArgs e)
       at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
       at System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)
       at System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: 

Here is my code behind:

Protected Sub rgLineItems_UpdateCommand(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles rgLineItems.UpdateCommand
        'Dim i As Integer = 1
        'Dim CellValue As String = Nothing
        'Dim txtVal As String = Nothing
        'Dim item As GridEditFormItem = TryCast(e.Item, GridEditFormItem)


        'CellValue = DirectCast(item.FindControl("rdListItemDiscount"), RadioButtonList).SelectedItem.Value
        'txtVal = DirectCast(item.FindControl("txtItemDiscountVal"), RadNumericTextBox).Text
        Dim edititem As GridEditableItem = DirectCast(e.Item, GridEditableItem)
        Dim txt As TextBox = DirectCast(edititem.FindControl("txtItemDiscountVal"), TextBox)
        Dim txtValue As String = txt.Text
    End Sub

The commented code you see above is another method I tried and I get an "object reference cannot be set to an object" exception.
Any more Ideas???

Thanks,
Soumya 





0
Daniel
Telerik team
answered on 12 Jan 2011, 04:26 PM
Hello Soumya,

I already answered to your support ticket.
The exception is caused by the fact that you are trying to cast RadNumericTextBox control to TextBox.

Let us know if you need further assistance.

Regards,
Daniel
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Soumya Parla
Top achievements
Rank 1
answered on 12 Jan 2011, 04:57 PM
Hi,
This is what I have and still have issues:
 Protected Sub rgLineItems_UpdateCommand(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles rgLineItems.UpdateCommand
        Try
            Dim i As Integer = 1
            Dim CellValue As String = Nothing
            Dim txtVal As String = Nothing
            Dim item As GridEditFormItem = TryCast(e.Item, GridEditFormItem)
            CellValue = DirectCast(item.FindControl("rdListItemDiscount"), RadioButtonList).SelectedItem.Value
            txtVal = DirectCast(item.FindControl("txtItemDiscountVal"), RadNumericTextBox).Text
            'Dim edititem As GridEditableItem = DirectCast(e.Item, GridEditableItem)
            'Dim txt As TextBox = DirectCast(edititem.FindControl("txtItemDiscountVal"), TextBox)
            'Dim txtValue As String = txt.Text
        Catch ex As Exception
        End Try

The new exception I have is this:
"Object reference not set to an instance of an object." System.Exception 

And I did mention this in my last reply.
Let me know where I am doing wrong.

Thanks,
Soumya

0
Accepted
Princy
Top achievements
Rank 2
answered on 13 Jan 2011, 08:38 AM
Hello Soumya,

I guess you are using 'InPlace' edit mode. If so accss the RadNumericTextBox using GridEditableItem instead of GridEditFormItem. Try the following code snippet and see whether it eliminate the error.

VB.NET:
Protected Sub rgLineItems_UpdateCommand(source As Object, e As GridCommandEventArgs)
     Dim edititem As GridEditableItem = DirectCast(e.Item, GridEditableItem)
     Dim txt As RadNumericTextBox = DirectCast(edititem.FindControl("txtItemDiscountVal"), RadNumericTextBox)
     Dim txtValue As String = txt.Text
  End Sub

Thanks,
Princy.
0
Soumya Parla
Top achievements
Rank 1
answered on 13 Jan 2011, 04:19 PM
Thanks Princy. This worked out for me.
Tags
Grid
Asked by
Soumya Parla
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Soumya Parla
Top achievements
Rank 1
Daniel
Telerik team
Share this question
or