【问题标题】:Change record order by given priority in GridView在 GridView 中按给定优先级更改记录顺序
【发布时间】:2015-11-05 03:22:35
【问题描述】:

我在 GridView 控件中有这样的记录

#          Name                 Priority
1          banana                  1
2          mango                   2
3          apple                   3 
4          water melon             4
5          grape                   5
6          pineapple               6
7          strawberry              7
8          blueberry               8

如果将优先级 6 拖到 1,则记录“菠萝”应排在第一位,其优先级应更改为“1”,其余记录应向下移动。

【问题讨论】:

  • 你在寻找上下交换gridview行吗?

标签: c# jquery asp.net gridview drag-and-drop


【解决方案1】:

在网格视图中交换行会有所帮助

设计:

<asp:GridView ID="gvLocations" runat="server" AutoGenerateColumns="false">
<Columns>
    <asp:TemplateField HeaderText="Id" ItemStyle-Width="30">
         <ItemTemplate>
             <%# Eval("Id") %>
             <input type="hidden" name="LocationId" value='<%# Eval("Id")    
                 %>' /> 
            </ItemTemplate>
     </asp:TemplateField>
     <asp:BoundField DataField="Location" HeaderText="Location" ItemStyle-  
      Width="150" />
<asp:BoundField DataField="Preference" HeaderText="Preference" ItemStyle-Width="100" />


用于拖放网格视图行的 JavaScript 代码:

<script type="text/javascript"     
src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<link rel="stylesheet"               
href="https://ajax.googleapis.com/ajax/libs/jqueryui/
1.8.24/themes/smoothness/jq    uery-ui.css" /><script type="text/javascript"    
src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.24/jquery- 
ui.min.js"></script>
<script type="text/javascript">
$(function () {
$("[id*=gvLocations]").sortable({
    items: 'tr:not(tr:first-child)',
    cursor: 'pointer',
    axis: 'y',
    dropOnEmpty: false,
    start: function (e, ui) {
        ui.item.addClass("selected");
    },
    stop: function (e, ui) {
        ui.item.removeClass("selected");
    },
    receive: function (e, ui) {
        $(this).find("tbody").append(ui.item);
    }
});
});
</script>

拖放后保存网格:

protected void UpdatePreference(object sender, EventArgs e)
{
int[] locationIds = (from p in Request.Form["LocationId"].Split(',')
                        select int.Parse(p)).ToArray();
int preference = 1;
foreach (int locationId in locationIds)
{
    this.UpdatePreference(locationId, preference);
    preference += 1;
}

Response.Redirect(Request.Url.AbsoluteUri);
}

private void UpdatePreference(int locationId, int preference)
{
string constr =   ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
    using (SqlCommand cmd = new SqlCommand("UPDATE HolidayLocations SET Preference = @Preference WHERE Id = @Id"))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@Id", locationId);
            cmd.Parameters.AddWithValue("@Preference", preference);
            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
}
}

更多详情:http://www.aspsnippets.com/Articles/Reorder-GridView-Rows-Drag-and-Drop-ordering-of-GridView-Rows-using-jQuery-in-ASPNet.aspx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多