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

get label control value from radgrid

3 Answers 948 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ron
Top achievements
Rank 1
Ron asked on 16 Oct 2011, 06:36 PM
Hi,

Can someone help me figure out how to get a label control text value from grid item template column?
<telerik:GridTemplateColumn DataField="Amount" HeaderText="Amount" UniqueName="Amount">
                   <ItemStyle BorderStyle="NotSet" CssClass="test" HorizontalAlign="Right" Width="75px" />
                   <HeaderStyle HorizontalAlign="Center" Width="85px" />
                   <ItemTemplate>
                       <asp:Label ID="lblAmount" runat="server" Text='<%# Eval("Amount") %>'
                           Width="75"></asp:Label>
                   </ItemTemplate>
                   <EditItemTemplate>
                       <asp:Label ID="lblAmount" runat="server" Text='<%# Eval("Amount") %>'
                           Width="75"></asp:Label>
                   </EditItemTemplate>
               </telerik:GridTemplateColumn>
Label amount = (Label)item.FindControl("lblAmount");
double? amount= (item["Amount"].FindControl("lblAmount") as Label).Text;
I've tried both ways and neither seems to work. Thanks in advance.

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 17 Oct 2011, 05:48 AM
Hello Ron,

I tried the same scenario and it is working as expected in my end. Make sure that it is getting inside the loop when you are accessing the Label in ItemDataBound event. Here is the sample code that I tried.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
   if(e.Item is GridDataItem)
   {
      GridDataItem item = (GridDataItem)e.Item;
      Label lbl = (Label)item.FindControl("lblAmount");
      string value=lbl.Text;
   }
}

Thanks,
Shinu.
0
Ron
Top achievements
Rank 1
answered on 17 Oct 2011, 10:27 AM
Hi Shinu,

Does it have to be called from the item data bound event? because I have a method that gets text box values , combo box values and everything works fine except when I try to get the value of a label control which is not a rad control. Here is the complete method.
private void SaveInvoiceItems()
   {
       try
       {
           foreach (GridDataItem item in grdInvoiceItems.Items)
           {
               int id = (int)item.OwnerTableView.DataKeyValues[item.ItemIndex]["Id"];
               string lineItem = (item["Items"].FindControl("ddlItems") as RadComboBox).Text;
               string description = (item["Description"].FindControl("txtDescription") as RadTextBox).Text;
               double? price = (item["Price"].FindControl("txtPrice") as RadNumericTextBox).Value;
               double? qty = (item["Qty"].FindControl("txtQty") as RadNumericTextBox).Value;
               double? discount = (item["Discount"].FindControl("txtDiscount") as RadNumericTextBox).Value;
               double tax = double.Parse(lblTaxes.Text);
 
               Label amount = (Label)item.FindControl("lblAmount");
 
               InvoiceRecurring.UpdateRecurringInvoiceItems(id, lineItem, description, Convert.ToInt32(price),
                   Convert.ToInt32(qty), Convert.ToDouble(discount), tax, Convert.ToDouble(amount));
           }
       }
       catch (Exception ex)
       {
           Response.Write(ex.Message);
       }
   }
Thanks,
Ron.
0
Tsvetoslav
Telerik team
answered on 18 Oct 2011, 01:38 PM
Hello Ron,

I am attaching a sample where I have shown how to get the label control and have demonstrated a better way to extract the field values for a given grid row.

Regards, Tsvetoslav
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
Tags
Grid
Asked by
Ron
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Ron
Top achievements
Rank 1
Tsvetoslav
Telerik team
Share this question
or