I need to set some radgrid cell values client-side, via javascript. Then at postback I need to access those values. Currently I'm setting the cell's innerHTML property, which displays correctly in the grid. However, at postback all the cell values revert to " ". At postback, I am not re-binding the grid.
Thanks!
-rusty
Here's how I set values on the client-side:
// For all selected rows, set the cell value.
function rgSetSelectedRowValue(rg, columnName, val) {
var table = rg.get_masterTableView();
var selectedRows = table.get_selectedItems();
for (var i = 0; i < selectedRows.length; i++) {
var row = selectedRows[i];
var cell = table.getCellByColumnUniqueName(row, columnName);
cell.innerHTML = val;
}
}
// loop on grid rows
foreach (GridDataItem item in RadGrid1.MasterTableView.Items) {
string foo = item["ReplacementID"].Text;
string bar = item["comment"].Text;
...
}
9 Answers, 1 is accepted
function
AccessOnclient() {
var
grid = $find(
"<%= RadGrid1.ClientID %>"
);
if
(grid) {
var
MasterTable = grid.get_masterTableView();
var
Rows = MasterTable.get_dataItems();
for
(
var
i = 0; i < Rows.length; i++) {
var
row = Rows[i];
// Method1
var
getCellText_1 = row.get_element().cells[0].innerHTML;
// Method2
var
getCellText_2 = row.get_cell(
"ReplacementID"
).innerHTML;
// Method3
var
getCellText_2 = row.get_cell(
"ReplacementID"
).getElementsByTagName(
"span"
)[0].innerHTML;
//this code also work for Checkboxcolunm, hyperlinkcolumn...etc
}
}
}
For more info please check below link
http://jayeshgoyani.blogspot.in/2012/07/access-radgrid-on-client.html
Thanks,
Jayesh Goyani
-r
You cannot get the values that are changed in the client side from server side because of postback. One suggestion is that you can set the value to a hiddenfield as the cell value in client side and access it in server side as shown below.
JS:
function
rgSetSelectedRowValue(rg, columnName, val) {
var
table = rg.get_masterTableView();
var
selectedRows = table.get_selectedItems();
for
(
var
i = 0; i < selectedRows.length; i++) {
var
row = selectedRows[i];
var
cell = table.getCellByColumnUniqueName(row, columnName);
cell.innerHTML = val;
var
hidden = document.getElementById(
"HiddenField1"
);
hidden.value = cell.innerHTML;
}
}
protected
void
Button1_Click(
object
sender, EventArgs e)
{
foreach
(GridDataItem item
in
RadGrid1.SelectedItems)
{
string
cellvalue= HiddenField1.Value;
}
}
Thanks,
Shinu
"You cannot get the values that are changed in the client side"
Cannot set or cannot get? Please clarify. I need to adjust date time stamps based on a clients location. Is there a way to this?
Here is an example of how to access and set the RadGrid cell values on the client-side:
ASPX
<script>
function pageLoad() {
var grid = $find("<%= GV_SystemMonitor.ClientID %>");
if (grid) {
var MasterTable = grid.get_masterTableView();
var Rows = MasterTable.get_dataItems();
for (var i = 0; i < Rows.length; i++) {
var row = Rows[i];
var cell = MasterTable.getCellByColumnUniqueName(row, "OrderDate"); // Get the cell for the OrderDate column - see https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/client-side-programming/gridtableview-object/methods/getcellbycolumnuniquename
if (cell) { // Check if the cell was found
var timestamp = cell.innerHTML; // Get the timestamp from the cell
var updatedTimestamp = datetimeConverter(new Date(timestamp)); // Convert the timestamp
cell.innerHTML = updatedTimestamp.toLocaleString(); // Update the cell with the new timestamp
}
}
}
}
function datetimeConverter(date) {
var n = date.getTimezoneOffset();
var x = n / 60;
date.setHours(date.getHours() - x);
return date;
}
</script>
<telerik:RadGrid ID="GV_SystemMonitor" runat="server" AllowPaging="True" Width="800px" OnNeedDataSource="RadGrid1_NeedDataSource">
<MasterTableView AutoGenerateColumns="False" DataKeyNames="OrderID">
<Columns>
<telerik:GridBoundColumn DataField="OrderID" DataType="System.Int32"
FilterControlAltText="Filter OrderID column" HeaderText="OrderID"
ReadOnly="True" SortExpression="OrderID" UniqueName="OrderID">
</telerik:GridBoundColumn>
<telerik:GridDateTimeColumn DataField="OrderDate" DataType="System.DateTime"
FilterControlAltText="Filter OrderDate column" HeaderText="OrderDate"
SortExpression="OrderDate" UniqueName="OrderDate">
</telerik:GridDateTimeColumn>
<telerik:GridNumericColumn DataField="Freight" DataType="System.Decimal"
FilterControlAltText="Filter Freight column" HeaderText="Freight"
SortExpression="Freight" UniqueName="Freight">
</telerik:GridNumericColumn>
<telerik:GridBoundColumn DataField="ShipName"
FilterControlAltText="Filter ShipName column" HeaderText="ShipName"
SortExpression="ShipName" UniqueName="ShipName">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="ShipCountry"
FilterControlAltText="Filter ShipCountry column" HeaderText="ShipCountry"
SortExpression="ShipCountry" UniqueName="ShipCountry">
</telerik:GridBoundColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
ASPX.CS
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
(sender as Telerik.Web.UI.RadGrid).DataSource = OrdersTable();
}
private DataTable OrdersTable()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("OrderID", typeof(int)));
dt.Columns.Add(new DataColumn("OrderDate", typeof(DateTime)));
dt.Columns.Add(new DataColumn("Freight", typeof(decimal)));
dt.Columns.Add(new DataColumn("ShipName", typeof(string)));
dt.Columns.Add(new DataColumn("ShipCountry", typeof(string)));
dt.PrimaryKey = new DataColumn[] { dt.Columns["OrderID"] };
for (int i = 0; i < 70; i++)
{
int index = i + 1;
DataRow row = dt.NewRow();
row["OrderID"] = index;
row["OrderDate"] = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0).AddHours(index);
row["Freight"] = index * 0.1 + index * 0.01;
row["ShipName"] = "Name " + index;
row["ShipCountry"] = "Country " + index;
dt.Rows.Add(row);
}
return dt;
}
You can also check these resources:
In the past, I've used another 3rd-party grid that supported "unbound columns", which could be populated on the client, then accessed server-side after a postback. It looks like this is not possible with RadGrids.
My current solution is to use the grid to store my data, and then handle it all client-side without doing a postback. Took a bit of refactoring but it's working now.
-rd
Hello,
How can I get the DataKeyValue of the row when I click on a button inside the RadGrid ?
Hi Rusty,
I am also in the same boat. I have switched from other 3rd party control that supported "unbound columns", which could be populated on the client, then accessed server-side after a postback. I am trying to set the RadGrid cell values from client side and accessing the same from server side . But after postback value disappears.
My client side code :
function GetSelectedItem(arg) {
if (arg!=null | arg!=undefined)
{
var grid = $find("<%=RadGridRoyaltyMapping.ClientID %>");
var MasterTable = grid.get_masterTableView();
var selectedRows = MasterTable.get_selectedItems();
for (var i = 0; i < selectedRows.length; i++) {
var row = selectedRows[i];
var sPart = arg.split('|');
var cell = MasterTable.getCellByColumnUniqueName(row, "ID_ITEM");
cell.innerHTML = sPart[0];
var cell2 = MasterTable.getCellByColumnUniqueName(row, "EXPECTED_VALUE");
cell2.innerHTML = sPart[1];
}
}
}
Server side code :
protected void btnApply_Click(object sender, EventArgs e)
{
foreach (GridDataItem item in RadGridRoyaltyMapping.Items)
{
string idItem = item["ID_ITEM"].Text.ToString();
string idExpectedValue = item["EXPECTED_VALUE"].Text.ToString();
}
}
If you found a solution, Please share it.