How to shuffle or reorder rows in the gridview on button click
I have radgrid for which I have binded data from code behind, I have two buttons next to my grid control, one moveup button and movedown button. On selecting a particular row on the grid and on click of moveup button I have to move the row up. How do I exchange the rows?
I am trying to implement the following code, but for some reason my MoveRowUp() and MoveRowDown() events are not firing in the javascript.
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
var SelectedRow;
function RowSelected(rowObject)
{
SelectedRow = rowObject;
}
function MoveRowUp()
{
if (SelectedRow && SelectedRow.Control.sectionRowIndex > 0)
{
SerializeReorderChanges(SelectedRow.Control.sectionRowIndex, SelectedRow.Control.sectionRowIndex - 1);
SelectedRow.Control.swapNode(SelectedRow.Control.parentNode.rows[SelectedRow.Control.sectionRowIndex - 1]);
}
}
function MoveRowDown()
{
if (SelectedRow && SelectedRow.Control.sectionRowIndex < SelectedRow.Control.parentNode.rows.length - 1)
{
SerializeReorderChanges(SelectedRow.Control.sectionRowIndex, SelectedRow.Control.sectionRowIndex + 1);
SelectedRow.Control.parentNode.rows[SelectedRow.Control.sectionRowIndex + 1].swapNode(SelectedRow.Control);
}
}
function SerializeReorderChanges(index1, index2)
{
var reorderInput = document.getElementById("ReorderChanges");
if (reorderInput)
{
reorderInput.value += index1 + "," + index2开发者_StackOverflow中文版 + ";";
}
}
if(window.netscape)
{
Node.prototype.swapNode = function(node)
{
var nextSibling = this.nextSibling;
var parentNode = this.parentNode;
node.parentNode.replaceChild(this, node);
parentNode.insertBefore(node, nextSibling);
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="hidden" name="ReorderChanges" id="ReorderChanges">
<input type="button" value="Move selected row up" onclick="MoveRowUp()">
<br />
<input type="button" value="Move selected row down" onclick="MoveRowDown()">
<radG:RadGrid ID="RadGrid1" OnNeedDataSource="RadGrid1_NeedDataSource" runat="server">
<ClientSettings>
<Selecting AllowRowSelect="True"></Selecting>
<ClientEvents OnRowSelected="RowSelected"></ClientEvents>
</ClientSettings>
</radG:RadGrid>
<input type="button" value="Apply changes server side" onclick="__doPostBack('<%= RadGrid1.ClientID %>','ReorderedRows:' + document.getElementById('ReorderChanges').value);">
</div>
</form>
</body>
</html>
精彩评论