【发布时间】:2014-12-29 23:01:50
【问题描述】:
我已经尝试了几个小时,但无法弄清楚为什么我无法成功地使用 DetailsView 插入记录,其中字段使用 ITemplate 派生类和 SqlDataSource 动态创建。
这是用于创建模板的代码:
public class DetailsViewTemplate : ITemplate
{
private DataControlRowType templateType;
private string columnNameFriendly;
private string columnNameData;
private Control control;
private DetailsViewMode viewMode;
public DetailsViewTemplate(DataControlRowType type, DetailsViewMode mode, string colNameFr, string colNameDt, Control con)
{
templateType = type;
columnNameFriendly = colNameFr;
columnNameData = colNameDt;
control = con;
viewMode = mode;
}
public void InstantiateIn(System.Web.UI.Control container)
{
switch (templateType)
{
case DataControlRowType.Header:
{
Literal lc = new Literal();
lc.Text = columnNameFriendly;
container.Controls.Add(lc);
break;
}
case DataControlRowType.DataRow:
{
Control field = control;
if (field.GetType() == typeof(Label))
{
Label lbl = (Label)control; // new Label();
if (viewMode != DetailsViewMode.Insert)
lbl.DataBinding += new EventHandler(this.lbl_DataBind);
container.Controls.Add(control);
}
else if (field.GetType() == typeof(TextBox))
{
TextBox txt = (TextBox)control; // new TextBox();
if (viewMode != DetailsViewMode.Insert)
txt.DataBinding += new EventHandler(this.txt_DataBind);
container.Controls.Add(control);
}
else if (field.GetType() == typeof(DropDownList))
{
DropDownList ddl = (DropDownList)field;
if (viewMode != DetailsViewMode.Insert)
ddl.DataBinding += new EventHandler(this.ddl_DataBind);
container.Controls.Add(control);
}
else if (field.GetType() == typeof(CheckBox))
{
CheckBox cbx = (CheckBox)control; // new CheckBox();
if (viewMode != DetailsViewMode.Insert)
cbx.DataBinding += new EventHandler(this.cbx_DataBind);
container.Controls.Add(control);
}
break;
}
}
}
private void txt_DataBind(Object sender, EventArgs e)
{
TextBox txt = (TextBox)sender;
DetailsView container = (DetailsView)txt.NamingContainer;
if (((DataRowView)container.DataItem) != null)
txt.Text = DataBinder.Eval(((DataRowView)container.DataItem), columnNameData).ToString();
//txt.Font.Size = 7;
txt.Font.Name = "Arial";
}
private void lbl_DataBind(Object sender, EventArgs e)
{
Label lbl = (Label)sender;
DetailsView container= (DetailsView)lbl.NamingContainer;
if (((DataRowView)container.DataItem) != null)
lbl.Text = DataBinder.Eval(((DataRowView)container.DataItem), columnNameData).ToString();
//lbl.Font.Size = 7;
lbl.Font.Name = "Arial";
}
private void ddl_DataBind(Object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
DetailsView container = (DetailsView)ddl.NamingContainer;
if (((DataRowView)container.DataItem) != null)
ddl.SelectedValue = DataBinder.Eval(((DataRowView)container.DataItem), columnNameData).ToString();
//ddl.Font.Size = 7;
ddl.Font.Name = "Arial";
}
private void cbx_DataBind(Object sender, EventArgs e)
{
CheckBox cbx = (CheckBox)sender;
DetailsView container = (DetailsView)cbx.NamingContainer;
if (((DataRowView)container.DataItem) != null)
cbx.Checked = (bool)DataBinder.Eval(((DataRowView)container.DataItem), columnNameData);
}
}`
这里是将模板字段添加到 DetailsView 的代码
TextBox tb = new TextBox();
TemplateField tf = new TemplateField();
tf.HeaderTemplate = new DetailsViewTemplate(DataControlRowType.Header, DetailsViewDynAttachment.DefaultMode, fieldName, fieldName, new Label());
tf.InsertItemTemplate = new DetailsViewTemplate(DataControlRowType.DataRow, DetailsViewDynAttachment.DefaultMode, fieldName, fieldName, tb);
DetailsViewDynAttachment.Fields.Add(tf);
DetailsView 模式设置为 Insert,InserCommand 和 opparameters 列表已定义但无论我尝试什么,只要我使用内置的 Insert 命令或在数据源上调用 insert,我所有动态添加的控件似乎都有回发后消失了,所有的价值都丢失了……
请注意,相同的代码在 ReadOnly 模式下工作(使用 SelectCommand 集...) 但无法弄清楚为什么这些字段似乎没有在插入模式下持久化。
我搜索了几个小时并尝试了所有我想要的方法,但找不到解决方案... 任何帮助将不胜感激。
附言
这里的主要问题是动态添加的模板字段在回发后不会持续存在。 DetailsView Fields and Rows 为 1(命令字段),所有其他动态字段都“丢失”(未保留)。 此外,虽然 ItemInserting 被提出,但它的 (DetailsViewInsertEventArgs) e.Values 计数为 0。值未持久化...(?)
为什么?
【问题讨论】:
标签: asp.net