我有一个可以很好地添加和删除行的Gridview,但它只添加没有刷新的行。我需要它来删除没有刷新的行。我在UpdatePanel中有完整的东西,但删除命令仍然刷新页面。我正在使用母版页。这是我的标记:
<%@ Page Title="" Language="C#" MasterPageFile="~/TextAnalyzer.Master" AutoEventWireup="true" CodeBehind="Upload4.aspx.cs" Inherits="TextAnalyzer.Upload4" EnableEventValidation="true" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<!-- Using Ajax panels to set sessionvars before upload is started -->
<ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</ajaxToolkit:ToolkitScriptManager>
<asp:UpdatePanel ID="udpnlStep1" runat="server">
<ContentTemplate>
<div id="Tasks" style="float: left; width: 100%; border: 0px solid red;">
<asp:GridView ID="gridTasks" runat="server" Width="100%" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left"
ShowFooter="True" AutoGenerateColumns="False" ClientIDMode="Static"
CellPadding="4" ForeColor="#333333" DataKeyNames="TaskNumber" OnRowDataBound="gridTasks_RowDataBound" OnDataBound="gridTasks_DataBound"
OnRowCommand="gridTasks_RowCommand" GridLines="None" >
<Columns>
<asp:BoundField DataField="TaskNumber" HeaderText="Task #" Visible="true" />
<asp:TemplateField HeaderText="Window Size">
<ItemTemplate>
<asp:DropDownList ID="ddlParameter1" runat="server" >
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Centrality">
<ItemTemplate>
<asp:DropDownList ID="ddlParameter2" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Add/Remove">
<HeaderStyle />
<HeaderTemplate>
<asp:Button ID="ButtonAdd" runat="server"
Text="Add Summary Task" OnClick="ButtonAdd_Click" />
</HeaderTemplate>
<ItemTemplate>
<asp:Button ID="btnDelete" runat="server"
Text="Delete" OnClick="btnDelete_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#0096D6" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#0096D6" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
我的代码背后:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace TextAnalyzer
{
public partial class Upload5 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Session.Abandon();
AddNewRowToGrid();
}
}
private void AddNewRowToGrid()
{
DataTable dtCurrentTable = new DataTable();
DataRow drNewRow = null;
if (Session["CurrentTable"] == null)
{
dtCurrentTable = InitDataTableCols(dtCurrentTable);
}
else
{
dtCurrentTable = (DataTable)Session["CurrentTable"];
}
drNewRow = dtCurrentTable.NewRow();
if (dtCurrentTable.Rows.Count == 0)
{
drNewRow["TaskNumber"] = 1;
}
else
{
drNewRow["TaskNumber"] = Convert.ToInt32((dtCurrentTable.Rows[dtCurrentTable.Rows.Count - 1] as DataRow)[0].ToString()) + 1;
}
drNewRow["Parameter1"] = string.Empty;
drNewRow["Parameter2"] = string.Empty;
dtCurrentTable.Rows.Add(drNewRow);
BindGridFromNewDataAndSendToSession(dtCurrentTable);
//Session["CurrentTable"] = dtCurrentTable;
//gridTasks.DataSource = dtCurrentTable;
//gridTasks.DataBind();
}
private void BindGridFromNewDataAndSendToSession(DataTable myDataTable)
{
Session["CurrentTable"] = myDataTable;
gridTasks.DataSource = myDataTable;
gridTasks.DataBind();
}
DataTable InitDataTableCols(DataTable myTable)
{
myTable.Columns.Add(new DataColumn("TaskNumber", typeof(string)));
myTable.Columns.Add(new DataColumn("Parameter1", typeof(string)));
myTable.Columns.Add(new DataColumn("Parameter2", typeof(string)));
return myTable;
}
//http://www.codeproject.com/Questions/398783/Exporting-a-grid-view-to-datatable
DataTable GetDataTableFromCurrentGridValues(GridView myGrid)
{
DataTable dt = new DataTable();
dt = InitDataTableCols(dt);
// add the columns to the datatable
//if (myGrid.HeaderRow != null)
//{
// for (int i = 0; i < myGrid.HeaderRow.Cells.Count; i++)
// {
// dt.Columns.Add(myGrid.HeaderRow.Cells[i].Text);
// }
//}
// add each of the data rows to the table
foreach (GridViewRow row in myGrid.Rows)
{
DataRow dr;
dr = dt.NewRow();
dr["TaskNumber"] = row.Cells[0].Text;
dr["Parameter1"] = (row.Cells[1].FindControl("ddlParameter1") as DropDownList).SelectedItem.Value;
dr["Parameter2"] = (row.Cells[2].FindControl("ddlParameter2") as DropDownList).SelectedItem.Value;
dt.Rows.Add(dr);
}
return dt;
}
//http://stackoverflow.com/questions/18982343/how-to-add-a-row-dynamically-in-gridview
private void PutCurrentGridIntoSession()
{
Session["CurrentTable"] = GetDataTableFromCurrentGridValues(gridTasks);
}
protected void ButtonAdd_Click(object sender, EventArgs e)
{
PutCurrentGridIntoSession();
AddNewRowToGrid();
}
private void FillSummWinSizeDDL(DropDownList ddl)
{
Dictionary<string, string> lstSummWinSize = new Dictionary<string, string>();
lstSummWinSize.Add("3", "3");
lstSummWinSize.Add("4", "4");
lstSummWinSize.Add("5", "5");
lstSummWinSize.Add("6", "6");
ddl.DataSource = lstSummWinSize;
ddl.DataValueField = "Key";
ddl.DataTextField = "Value";
//ddl.SelectedValue = "4";
ddl.DataBind();
}
private void FillSummCentralityDDL(DropDownList ddl)
{
Dictionary<string, string> lstCentrality = new Dictionary<string, string>();
lstCentrality.Add("degree", "Degree");
lstCentrality.Add("betweenness", "Betweenness");
lstCentrality.Add("closeness", "Closeness");
lstCentrality.Add("eigenvector", "Eigenvector");
lstCentrality.Add("weighted_degree", "Weighted Degree");
ddl.DataSource = lstCentrality;
ddl.DataValueField = "Key";
ddl.DataTextField = "Value";
//ddl.SelectedValue = "degree";
ddl.DataBind();
}
protected void gridTasks_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView drview = e.Row.DataItem as DataRowView;
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlParam1 = (DropDownList)e.Row.FindControl("ddlParameter1");
DropDownList ddlParam2 = (DropDownList)e.Row.FindControl("ddlParameter2");
FillSummWinSizeDDL(ddlParam1);
FillSummCentralityDDL(ddlParam2);
if (Session["CurrentTable"] != null)
{
DataTable dtCurrentTable1 = (DataTable)Session["CurrentTable"];
if (dtCurrentTable1.Rows[e.Row.RowIndex] != null)
{
if ((dtCurrentTable1.Rows[e.Row.RowIndex][1] != null) && (dtCurrentTable1.Rows[e.Row.RowIndex][1].ToString() != ""))
{
ddlParam1.Items.FindByValue(dtCurrentTable1.Rows[e.Row.RowIndex][1].ToString()).Selected = true;
}
else
{
ddlParam1.SelectedValue = "4";
}
if ((dtCurrentTable1.Rows[e.Row.RowIndex][2] != null) && (dtCurrentTable1.Rows[e.Row.RowIndex][2].ToString() != ""))
{
ddlParam2.Items.FindByValue(dtCurrentTable1.Rows[e.Row.RowIndex][2].ToString()).Selected = true;
}
else
{
ddlParam2.SelectedValue = "degree";
}
}
}
}
if (e.Row.RowType == DataControlRowType.Header)
{
foreach (TableCell cell in e.Row.Cells)
{
if (cell.Text == "Window Size")
{
cell.Text = @"<span class='toolTip' title='This is a summary task window size tool tip.'>" + cell.Text + @"</span>";
}
if (cell.Text == "Centrality")
{
cell.Text = @"<span class='toolTip' title='This is a summary task centrality tool tip.'>" + cell.Text + @"</span>";
}
}
}
}
protected void gridTasks_DataBound(object sender, EventArgs e)
{
if (gridTasks.Rows.Count == 1)
{
(gridTasks.Rows[0].FindControl("btnDelete") as Button).Enabled = false;
}
}
protected void gridTasks_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
DataTable dt = GetDataTableFromCurrentGridValues(gridTasks);
//Get the button that raised the event
Button btn = (Button)sender;
//Get the row that contains this button
GridViewRow gvr = (GridViewRow)btn.NamingContainer;
dt.Rows.RemoveAt(gvr.RowIndex);
BindGridFromNewDataAndSendToSession(dt);
}
}
}
我不知道我是否遗漏了一些东西,但我现在暂时还没弄清楚。我认为UpdatePanel中的任何内容都变得ajaxified,不是吗?
我已经更新了代码,但它仍然是相同的。 :/
编辑工作解决方案 我不得不摆脱删除命令和OnDeleting,只是在用户Programmin Tool的建议上使用另一个按钮 - RIP