我有一个非常大的表,无法对其进行更新,因此我决定使用gridview
创建一个空的asp:templatefields
来向其中添加记录。
我还省略了捕获错误ID重复的重复记录。
该页面处于早期开发阶段。
这是我正在使用的gridview
:
<asp:GridView ID="addView" AllowPaging="false" CellPadding="4" GridLines="None" runat="server" AutoGenerateColumns="false"
OnRowCommand="addView_RowCommand" OnRowCreated="addView_RowCreated" OnRowDataBound="addView_RowDataBound"
OnRowDeleting="addView_RowDeleting">
<Columns>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:LinkButton ID="Delete" runat="server" Text="Eliminar" CommandName="Delete" CssClass="enlace" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Lista de precios">
<ItemTemplate>
<asp:DropDownList ID="addList" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Unidad de medida">
<ItemTemplate>
<asp:DropDownList ID="uomList" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Tipo de Venta">
<ItemTemplate>
<asp:DropDownList ID="sellingcodeList" runat="server">
<asp:ListItem Text="Sin control" Value="1"></asp:ListItem>
<asp:ListItem Selected="True" Text="Completa" Value="2"></asp:ListItem>
<asp:ListItem Text="Completa y fraccionada" Value="3"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Precio">
<ItemTemplate>
<asp:TextBox ID="priceBox" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:LinkButton ID="adduomButton" runat="server" CommandName="Add" CssClass="enlace" Text="Añade Más" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
我一直在搜索,发现完成此操作的唯一方法是将DataSource
保存在ViewState
中
我也找到了一种使用DataTable
中指示的GridView
列来生成aspx
的方法
我不知道是否有更好的方法来生成DataTable
或生成行内列数。
当Empty GridView提示并初始化时,我称之为这段代码。
protected void add_Click(object sender, EventArgs e)
{
DataTable dt = AddViewTable();
dt.Rows.Add(dt.NewRow());
ViewState["addView"] = dt;
this.addView.DataSource = dt;
this.addView.DataBind();
this.addPanel.Visible = true;
this.table.Visible = false;
this.error.Text = "";
this.success.Text = "";
}
在这里,我为每个事件(包括初始化)生成/重新生成表
protected DataTable AddViewTable()
{
DataTable dt = new DataTable();
if (ViewState["addView"] == null)
{
DataControlField[] cols = new DataControlField[this.addView.Columns.Count];
this.addView.Columns.CopyTo(cols, 0);
foreach (DataControlField col in cols)
{
DataColumn column = new DataColumn();
column.ColumnName = col.HeaderText;
dt.Columns.Add(column);
}
}
else
{
dt = (DataTable)ViewState["addView"];
}
foreach (GridViewRow row in addView.Rows)
{
DataRow r = dt.Rows[row.RowIndex];
if (row.FindControl("Delete") != null)
r.SetField(0, row.Cells[0].Text);
if (row.FindControl("addList") != null)
r.SetField(1, ((DropDownList)row.FindControl("addList")).SelectedValue);
if (row.FindControl("uomList") != null)
r.SetField(2, ((DropDownList)row.FindControl("uomList")).SelectedValue);
if (row.FindControl("sellingcodeList") != null)
r.SetField(3, ((DropDownList)row.FindControl("sellingcodeList")).SelectedValue);
if (row.FindControl("priceBox") != null)
r.SetField(4, ((TextBox)row.FindControl("priceBox")).Text);
if (row.FindControl("adduomButton") != null)
r.SetField(5, row.Cells[5].Text);
}
ViewState["addView"] = dt;
return dt;
}
这些是我正在使用的事件:
protected void addView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList AddList = (DropDownList)e.Row.Cells[1].FindControl("addList");
if (AddList != null)
{
AddList.SelectedValue = ((DataTable)ViewState["addView"]).Rows[e.Row.RowIndex].ItemArray[1].ToString();
}
DropDownList UomList = (DropDownList)e.Row.Cells[2].FindControl("uomList");
if (UomList != null)
{
UomList.SelectedValue = ((DataTable)ViewState["addView"]).Rows[e.Row.RowIndex].ItemArray[2].ToString();
}
DropDownList sellingcode = (DropDownList)e.Row.Cells[3].FindControl("sellingcodeList");
if (sellingcode != null)
{
sellingcode.SelectedValue = ((DataTable)ViewState["addView"]).Rows[e.Row.RowIndex].ItemArray[3].ToString();
}
TextBox price = (TextBox)e.Row.Cells[4].FindControl("priceBox");
if (price != null)
{
price.Text = ((DataTable)ViewState["addView"]).Rows[e.Row.RowIndex].ItemArray[4].ToString();
}
}
}
protected void addView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Add")
{
LinkButton lb = (LinkButton)e.CommandSource;
GridViewRow selectedRow = (GridViewRow)lb.Parent.Parent;
DataTable dt = AddViewTable();
dt.Rows.InsertAt(dt.NewRow(), selectedRow.RowIndex + 1);
ViewState["addView"] = dt;
this.addView.DataSource = dt;
this.addView.DataBind();
}
else if (e.CommandName == "Delete")
{
LinkButton lb = (LinkButton)e.CommandSource;
GridViewRow selectedRow = (GridViewRow)lb.Parent.Parent;
DataTable dt = AddViewTable();
dt.Rows.RemoveAt(selectedRow.RowIndex);
ViewState["addView"] = dt;
this.addView.DataSource = dt;
this.addView.DataBind();
}
}
protected void addView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
protected void addView_RowCreated(object sender, GridViewRowEventArgs e)
{
SauBus bb = new SauBus();
DataTable dt = AddViewTable();
DropDownList AddList = (DropDownList)e.Row.Cells[1].FindControl("addList");
if (AddList != null)
{
AddList.DataSource = this.PriceList.SelectedIndex != 0 ?
bb.GetDistinctDataTableCrm("product", "productid as id", "productnumber + ' - ' + name as name", "statecode=0 order by name") :
bb.GetDistinctDataTableCrm("pricelevel", "pricelevelid as id", "name", "statecode=0");
AddList.DataTextField = "name";
AddList.DataValueField = "id";
AddList.DataBind();
dt.Rows[e.Row.RowIndex].ItemArray[1] = AddList.SelectedValue;
}
DropDownList UomList = (DropDownList)e.Row.Cells[2].FindControl("uomList");
if (UomList != null)
{
UomList.DataSource = bb.GetDistinctDataTableCrm("uom", "uomid", "name", "1=1");
UomList.DataTextField = "name";
UomList.DataValueField = "uomid";
UomList.DataBind();
dt.Rows[e.Row.RowIndex].ItemArray[2] = UomList.SelectedValue;
}
DropDownList sellingcode = (DropDownList)e.Row.Cells[3].FindControl("sellingcodeList");
if (sellingcode != null)
{
dt.Rows[e.Row.RowIndex].ItemArray[3] = sellingcode.SelectedValue;
}
}
我所看到的是,我打电话AddViewTable()
的次数太多了,我不知道是否可以抑制某些捕获事件信息(例如行类型或行状态)的呼叫。
然后最后我更新数据库
protected void addsumbit_Click(object sender, EventArgs e)
{
SauBus bb = new SauBus();
this.addPanel.Visible = false;
this.table.Visible = true;
CrmService myCrmService = SauBus.GetMyCrmService();
DataTable dt = AddViewTable();
foreach (DataRow row in dt.Rows)
{
try
{
productpricelevel p = new productpricelevel();
p.pricelevelid = new Lookup();
p.pricelevelid.Value = this.PriceList.SelectedIndex != 0 ? Guid.Parse(productid) : Guid.Parse(row.ItemArray[1].ToString());
p.productid = new Lookup();
p.productid.Value = this.PriceList.SelectedIndex != 0 ? Guid.Parse(row.ItemArray[1].ToString()) : Guid.Parse(productid);
p.uomid = new Lookup();
p.uomid.Value = Guid.Parse(row.ItemArray[2].ToString());
p.quantitysellingcode = new Picklist();
p.quantitysellingcode.Value = int.Parse(row.ItemArray[3].ToString());
p.pricingmethodcode = new Picklist();
p.pricingmethodcode.Value = 1;//Importe en moneda
p.amount = new CrmMoney();
p.amount.Value = decimal.Parse(row.ItemArray[4].ToString());
myCrmService.Create(p);
}
catch (System.Web.Services.Protocols.SoapException ex)
{
this.error.Text += "<br/>" + ex.Message + ": " + ex.Detail.InnerText + "<br/>";
}
catch (Exception ec)
{
this.error.Text = ec.ToString();
}
}
FillView(); //Refilling original table
if (this.error.Text.IndexOf('>') == -1)
this.error.Text = "";
this.addsumbit.Text = "Añade";
}
我对ViewState
的治疗做得好吗?还是有其他/更好的方法来处理空GridViews
来生成记录?