How can i get WebGrid Selected Row into Hidden Variable.
I am using the following code.
@{
var grid = new WebGrid(@ViewBag.ImageFiles, canPage: false, rowsPerPage: 5, selectionFieldName: "SelectedRow", ajaxUpdateContainerId: "gridImages");
}
<div id="gridContent">
@grid.GetHtml(tableStyle: "webGrid",
htmlAttributes: new { id = "gridImages" },
headerStyle: "header",
displayHeader: true,
alternatingRowStyle: "alt",
selectedRowStyle: "select",
columns: grid.Columns(
grid.Column("Select", format: (item) => item.GetSelectLink("Select")),
grid.Column("File", "Image")))
</div>
<input type="button" value="Submit" onclick="javascript getSelectedRow();"/>
@{
System.Dynamic.ExpandoObject expobject = null;
if (grid.HasSelection)
{
expobject = (System.Dynamic.ExpandoObject)grid.SelectedRow.Value;
<input type="hidden" id="hdnSelectedRow" name="hdnSelectedRow" value="@(expobject == null ? "" : expobject.FirstOrDefault().Value.ToString())" />
}
}
Here i am binding Datatable to Webgrid from the following code
foreach (DataRow row in imageFiles.Rows)
{
var obj = (IDictionary<string, object>)new ExpandoObject();
foreach (DataColumn col in imageFiles.Columns)
{
obj.Add(col.ColumnName, row[col.ColumnName]);
}
result.Add(obj);
}
ViewBag.ImageFiles = result;
When i click on submit button i am not getting hidden variable and value in javascript.
when a webgrid is rendered it is just an html table. I could be wrong but I don't think that your code will change with what is clicked. For changing a hidden value to match a selected row I always use jquery. Try something like this to see if it works for you.
I find the solution to get selected row.
Following is the code.
In the javascript on button click i am using the following code to get selected row