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

How to assign cell value of the radgrid in the textbox in server side coding.

5 Answers 303 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Asif
Top achievements
Rank 1
Asif asked on 31 Aug 2010, 02:59 PM
Hi,
        How to assign the values from selected row of the radgrid to
Page1.aspx:
  
<script language="javascript">
function callShow() 
{
  if (window["<%=RadGrid1.ClientID %>"].MasterTableView.SelectedRows.length > 0) 
  {
    var selectedRows = window["<%=RadGrid1.ClientID %>"].MasterTableView.SelectedRows[0];
    window["<%= RadGrid1.ClientID %>"].AjaxRequest("<%= RadGrid1.UniqueID %>", "No|" + selectedRows.KeyValues["No"]);
  }    
}
</script>
<html>
<asp:UpdatePanel ID="updatePanel1" runat="server" UpdateMode="Conditional">
  <ContentTemplate>
     <asp:TextBox ID="txtNo" runat="server"/>
     <asp:TextBox ID="txtName" runat="server"/>
  </ContentTemplate> 
</asp:UpdatePanel>
..
..
<radG:RadGrid ID="RadGrid1" runat="server" AllowPaging="true" GridLines="None" Skin="Office2007" AllowSorting="false" EnableAJAX="true" AutoGenerateColumns="false">
<MasterTableView AutoGenerateColumns="False" DataKeyNames="No">
 <Columns>
   <radG:GridBoundColumn DataField="No" UniqueName="No"     HeaderText="No">                                                                     </radG:GridBoundColumn>
   <radG:GridBoundColumn DataField="Name" UniqueName="Name"   HeaderText="Name">                                                                   </radG:GridBoundColumn>
 </Columns>  
</MasterTableView> 
<ClientSettings EnableClientKeyValues="true">
      <Selecting AllowRowSelect="true" />
      <ClientEvents OnRowSelected="callShow" />
</ClientSettings> 
</radG:RadGrid>
</html>
  
  
Page1.aspx.cs:
  
protected override void RaisePostBackEvent(IPostBackEventHandler sourceControl, string eventArgument)
{
  if (eventArgument != null)
  {
    if (eventArgument.StartsWith("No"))
    {
      string[] arr1 = eventArgument.Split(new char[] { '|' });
      txtNo.Text = arr1[1].ToString();
    }
  
}
textboxes which is outside of radgrid in server side coding.
Please find me the solution as soon as possible.
Note: im using classic radgrid.

Regards,
Asif

5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 01 Sep 2010, 08:43 AM
Hello Asif,

One suggestion to achieve this is placing the TextBoxes (outside grid) and RadGrid in a UpdatePanel/RadAjaxPanel. Then in ItemCommand event check for the CommandName 'RowClick' (when setting EnablePostBackOnRowClick to True) and get the selected row and set the TextBoxes value.

Another option is using an AjaxManager on page and invoking the ajaxRequest from 'OnrowSelected' client event. In AjaxManager_AjaxRequest server event, get the selected item and set the TextBoxes value as per the cell value for selected row.

Thanks,
Princy.
0
Suzan
Top achievements
Rank 1
answered on 15 Apr 2011, 02:37 PM
This sounds very similar to what I am trying to do. Can you give me an example of the code that you would use to do this?
0
Princy
Top achievements
Rank 2
answered on 20 Apr 2011, 08:15 AM
Hello Suzan,

You can try the following code snippet in ItemCommand event to get the value of selected row in TextBox.

C#:
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
   {
       if (e.CommandName == "RowClick")
       {
               GridDataItem selectedRow=(GridDataItem)RadGrid1.SelectedItems[0];
               TextBox1.Text = selectedRow["ColumnUniqueName].Text;//get cell vaue using UniqueName of column
       }
   }

Note:Ensure that EnablePostBackOnRowClick is true to fire this event..

And here is the code for second approach.
ASPX:
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" OnAjaxRequest="RadAjaxManager1_AjaxRequest1">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="RadAjaxManager1">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
            </UpdatedControls>
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManager>
  
<telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1">
    <MasterTableView DataKeyNames="EmployeeID">
        <Columns>
            <telerik:GridBoundColumn DataField="EmployeeID" HeaderText="EmployeeID" UniqueName="EmployeeID">
            </telerik:GridBoundColumn>
            <telerik:GridClientSelectColumn>
            </telerik:GridClientSelectColumn>
        </Columns>
    </MasterTableView>
    <ClientSettings Selecting-AllowRowSelect="true">
        <ClientEvents OnRowSelected="RowSelected" />
    </ClientSettings>
</telerik:RadGrid>

Java Script:
<script type="text/javascript">
    function RowSelected(sender, args) {
        var rowindex = args._itemIndexHierarchical;
        var ajaxManager = $find("<%= RadAjaxManager1.ClientID %>");
        ajaxManager.ajaxRequest(rowindex); // ajaxRequest
    }
</script>

C#:
protected void RadAjaxManager1_AjaxRequest1(object sender, AjaxRequestEventArgs e)
   {
       string  rowIndex =e.Argument;//getting row index
       GridDataItem item = (GridDataItem)RadGrid1.Items[rowIndex]; //accessing grid item
       //access cell value of item and store into db
   }

Thanks,
Princy.
0
Nisha
Top achievements
Rank 1
answered on 07 Aug 2012, 07:32 AM
hello princy,
i want to do same thing i want to do like if user click on single row from the radgrid the value should be auto populated in the text box which is  not wrapped by rad ajax panel. it is out side of the radgrid. also rad grid is wrapped by ajaxmanager.
now using this code i can successfully get the value at server side.
but when i assign the value to the textbox which is showing me properly at code behind but is does not display in the textbox at browser side. no value display in the textbox but if i removed the

<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" OnAjaxRequest="RadAjaxManager1_AjaxRequest1">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="RadAjaxManager1">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
            </UpdatedControls>
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManager>

this code above the grid then the value will display in textbox when i select the row from the grid. please help me how  do i assign the value to the textbox when row is selected and also page should not be refreshed i have tried radajaxpanel, update panel, same problem occurs. without ajax its work perfectly but when i tried to put ajaxpanel or ajax manger i can not see the assigned value in textbox at browser side i am using firefox browser, ASP.net 4.0..
let me know how to deal with this problem.......
0
Tsvetina
Telerik team
answered on 10 Aug 2012, 07:56 AM
Hi Nisha,

I you assign the value on the server-side when a postback from RadGrid occurs, you need to have an AjaxSetting in RadAjaxManager where the grid updates the textbox:
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" OnAjaxRequest="RadAjaxManager1_AjaxRequest1">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="RadAjaxManager1">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
            </UpdatedControls>
        </telerik:AjaxSetting>
        <telerik:AjaxSetting AjaxControlID="RadGrid1">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="TextBox1" />
            </UpdatedControls>
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManager>


All the best,
Tsvetina
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
Asif
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Suzan
Top achievements
Rank 1
Nisha
Top achievements
Rank 1
Tsvetina
Telerik team
Share this question
or