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

Identify radgrid row index in keypress event

5 Answers 562 Views
Grid
This is a migrated thread and some comments may be shown as answers.
santhosh
Top achievements
Rank 2
santhosh asked on 13 Nov 2008, 08:46 AM
Hi,

I am using a radgrid which has 3 template columns, each template columns contains the textbox, when i type something in the textbox and tabout i should be able to get the textbox value as well as the grid row index so that i can access other gird data.

so kindly advise me on which event to be used to fire events when i change the text and tabout and also how to get the rowindex within that event.

Thanks in advance.

Santhosh

5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 14 Nov 2008, 10:37 AM
Hello Santhosh,

You can try out the following code to get the text typed in the textBox and the row index of that particular textbox, on tab key press as shown below.
aspx:
 <telerik:GridTemplateColumn> 
      <ItemTemplate> 
           <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>             
       </ItemTemplate> 
 </telerik:GridTemplateColumn> 

cs:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridDataItem) 
        { 
            GridDataItem dataItem = (GridDataItem)e.Item; 
            TextBox txtBx = (TextBox)dataItem.FindControl("TextBox1");             
            int index=dataItem.ItemIndex; 
            txtBx.Attributes.Add("onKeyDown", "return tabKeypress(event,'" + txtBx.ClientID + "','" + index + "');"); 
        } 
    } 

js:
function tabKeypress(e,textboxID,rowindex) 
    {     
     if(e.keyCode==9) 
     { 
      alert("TextBox Text = " +document.getElementById(textboxID).value); 
      alert("row index = " +rowindex); 
     } 
    } 

Thanks
Princy.

0
santhosh
Top achievements
Rank 2
answered on 14 Nov 2008, 11:03 AM
Hi Princy,

Thank you very much for your replay, but what i need is assume there are other columns in the gird which are not template columns, that are grid bound columns, I want to retrive those data i.e other columun data(bound column), when the text change happens in the template column text box.

Kindly advise.

Regards,

Santhosh

0
Princy
Top achievements
Rank 2
answered on 17 Nov 2008, 05:46 AM
Hello Santhosh,

You can extract cell values from other columns when the text changes in the textbox as shown below.
js:
function tabKeypress(e,textboxID,rowindex) 
    { 
     
     if(e.keyCode==9) 
     { 
      alert("TextBox Text = " +document.getElementById(textboxID).value); 
      alert("row index = " +rowindex); 
       
        var grid = $find("<%=RadGrid1.ClientID %>"); 
        var MasterTable = grid.get_masterTableView(); 
        //access the row  contining the textbox
        var row = MasterTable.get_dataItems()[rowindex];       
         
        // To get the text for a particular column by its UniqueName  
        var cell = MasterTable.getCellByColumnUniqueName(row, "ColumnUniqueName"); 
        alert( cell.innerHTML) 
       
      } 
    } 

Thanks
Princy
0
santhosh
Top achievements
Rank 2
answered on 17 Nov 2008, 06:53 AM
Hi Princy,
I tried the method specified by you to get the grid column values but I am unable to get the values, its saying that "e.keyCode" value itself as undefined, Below is the code that I am woking, kindly have look at it once.

function

tabKeypress(e,textboxID,rowindex)

{

alert(

'hai');

 

 

alert(e.keyCode);

 

if(e.keyCode==9)

{

  alert(

"TextBox Text = " +document.getElementById(textboxID).value);

 

 

  alert(

"row index = " +rowindex);

 

 

 

var grid = $find("<%=radgridPaymentDetails.ClientID %>");

 

 

 

var MasterTable = grid.get_masterTableView();

 

 

 

//access the row contining the textbox

 

 

 

var row = MasterTable.get_dataItems()[rowindex];

 

 

 

// To get the text for a particular column by its UniqueName

 

 

 

var cell = MasterTable.getCellByColumnUniqueName(row, "ColumnUniqueName");

 

 

  alert( cell.innerHTML)

}

 

 

 

 

 

<telerik:RadGrid  ID="radgridPaymentDetails" runat="server" EnableViewState="true"  
                            
Skin="<%$ Resources:pageLabels, skinGrid %>"     
                            AllowSorting
="false" AllowPaging="True" 
                            
AutoGenerateColumns="False" PageSize="<%$ Resources:pageLabels, pageSize %>" 
                            
GridLines="None" ShowFooter="false"> 

 

     <SelectedItemStyle Wrap="false" BackColor="#BDD6F5"></SelectedItemStyle>

 

 

 

    <HeaderStyle HorizontalAlign="Left" BackColor="#E3EAF1"></HeaderStyle>

 

 

     <ClientSettings>

 

         <Selecting AllowRowSelect="True"></Selecting>

 

         <ClientEvents OnKeyPress="tabKeypress" />

 

     </ClientSettings>

 

     <PagerStyle Mode="NextPrevNumericAndAdvanced" />

 

     <MasterTableView>

 

         <Columns>

 

             <telerik:GridBoundColumn DataField="CPT/ASA" HeaderText="CPT/ASA" UniqueName="CPT/ASA">

 

                 <ItemStyle Width="70px" />

 

             </telerik:GridBoundColumn>
            <telerik:GridTemplateColumn HeaderText="Denied $">

 

 

            <ItemStyle width="30px" />

 

 

            <ItemTemplate>

 

 

                 <telerik:RadNumericTextBox

 

                   EmptyMessage="$0.00" ShowSpinButtons="false"

 

 

 

                   ButtonsPosition="Right" Type="Currency"

 

 

 

                   Culture="English (United States)" width="60px"

 

 

 

                   MaxLength ="6"

 

 

 

                   ID="radtxtDenied" value='<%# (CDbl((CType(Container.DataItem, System.Data.DataRowView)("Denied $"))))%>'

 

 

 

                   runat="server" onchange="UpdateDenied()">

 

 

 

                   <EnabledStyle HorizontalAlign="right" />

 

 

 

                </telerik:RadNumericTextBox>

 

 

 

           </ItemTemplate>

 

 

 

</telerik:GridTemplateColumn>

 

 

 

</Columns>

 

 

 

</MasterTableView>

 

 

 

</telerik:RadGrid>

 

 

 

 

 Regards,

Santhosh

 

 

 

 

 

 

0
Shinu
Top achievements
Rank 2
answered on 17 Nov 2008, 07:45 AM

Hi Santhosh,

You can get this undefined error, probably if you are not passing the event parameter from the code behind and then call the javascript function as shown by princy in her previous code.
cs:

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)   
    {   
        if (e.Item is GridDataItem)   
        {   
            GridDataItem dataItem = (GridDataItem)e.Item;   
            TextBox txtBx = (TextBox)dataItem.FindControl("TextBox1");               
            int index=dataItem.ItemIndex;   
            txtBx.Attributes.Add("onKeyDown", "return tabKeypress(event,'" + txtBx.ClientID + "','" + index + "');");   
        }   
    }  

Shinu.
Tags
Grid
Asked by
santhosh
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 2
santhosh
Top achievements
Rank 2
Shinu
Top achievements
Rank 2
Share this question
or