我有从xml构建的动态表单。我可以生成表单但我无法从中获取新值。我只获得生成的旧值。我想更新旧的不同的值。我认为问题是我用旧的覆盖了page_init上的新值?
这是我的用户界面的保管箱链接,可以更好地了解我想要实现的目标:https://www.dropbox.com/s/nacs9ohgjxefft9/FormView.png?dl=0#
public sealed class FormViewEditTemplate : IBindableTemplate
{
public FormViewEditTemplate(ListItemType type, mmXMLdoc.mmXMLdoc editXmlForm)
{
templateType = type;
_editXmlForm = editXmlForm;
}
//This method provides a way to insert an instance of text and controls into the specified container.
void ITemplate.InstantiateIn(Control container)
{
tabela = new Table();
tabs = new TabContainer();
tapPanel = new TabPanel();
row = new TableRow();
switch (templateType)
{
case ListItemType.EditItem:
List<XmlElement> Controls = _editXmlForm.getNodCollection("//Tabs");
if (Controls.Count > 0)
{
//pridobimo tabe za formo
foreach (XmlElement ctrlItem in Controls)
{
var ControlsItems = _editXmlForm.getNodCollection("./Controls", "Control", -1, ctrlItem);
tabela = new Table();
tapPanel = new TabPanel();
tapPanel.ID = Guid.NewGuid().ToString();
tapPanel.HeaderText = ctrlItem.GetAttribute("name").ToString();
//pridobimo podatke za formo
//var controls1 = ctrlItem.SelectNodes("//Controls");
foreach (XmlElement ctrl in ControlsItems)
{
string name = _editXmlForm.gStr("./@name", -1, ctrl) + Guid.NewGuid().ToString();
string label = _editXmlForm.gStr("./@label", -1, ctrl);
string width = _editXmlForm.gStr("./@width", -1, ctrl);
string type = _editXmlForm.gStr("./@type", -1, ctrl);
string value = String.Empty;
string helpnote = _editXmlForm.gStr("./HelpNote", -1, ctrl);
string boundfield = _editXmlForm.gStr("./BoundField", -1, ctrl);
row = new TableRow();
TableCell cell = new TableCell();
switch (type)
{
case "TextBox":
//The TemplateField allows for a mix of HTML markup, Web controls, and data-binding syntax.
Label lbl_item = new Label();
lbl_item.Font.Bold = true;
lbl_item.Text = label + ": ";
cell = new TableCell();
cell.Controls.Add(lbl_item);
row.Controls.Add(cell);
TextBox txt_item = new TextBox();
txt_item.ID = boundfield;
txt_item.Text = boundfield;
txt_item.DataBinding += new EventHandler(txt_DataBind);
cell = new TableCell();
cell.Controls.Add(txt_item);
row.Controls.Add(cell);
break;
case "CheckBox":
lbl_item = new Label();
lbl_item.Font.Bold = true;
lbl_item.Text = label + "<br/>";
//The BoundField displays the value of specified DataSource field as text.
BoundField bfield = new BoundField();
bfield.HeaderText = label;
bfield.DataField = boundfield;
break;
default:
break;
}
tabela.Controls.Add(row);
}
tapPanel.Controls.Add(tabela);
tabs.Controls.Add(tapPanel);
}
}
break;
case ListItemType.Footer:
Button saveButton = new Button();
saveButton.Command += new CommandEventHandler(SaveButton_Command);
saveButton.CommandName = "Update";
saveButton.Text = "Save";
saveButton.ID = "EditButton";
container.Controls.Add(saveButton);
break;
}
container.Controls.Add(tabs);
}
IOrderedDictionary IBindableTemplate.ExtractValues(Control container)
{
OrderedDictionary dict = new OrderedDictionary();
List<XmlElement> Controls = _editXmlForm.getNodCollection("//Tabs");
if (Controls.Count > 0)
{
//pridobimo tabe za formo
foreach (XmlElement ctrlItem in Controls)
{
var ControlsItems = _editXmlForm.getNodCollection("./Controls", "Control", -1, ctrlItem);
foreach (XmlElement ctrl in ControlsItems)
{
string type = _editXmlForm.gStr("./@type", -1, ctrl);
string boundfield = _editXmlForm.gStr("./BoundField", -1, ctrl);
switch (type)
{
case "TextBox":
TextBox tb = (TextBox)FindControlRecursive(container, boundfield);
if (tb != null)
dict[boundfield] = tb.Text;
break;
case "Label":
Label lb = (Label)FindControlRecursive(container, boundfield);
if (lb != null)
dict[boundfield] = lb.Text;
break;
case "CheckBox":
CheckBox cb = (CheckBox)FindControlRecursive(container, boundfield);
if (cb != null)
dict[boundfield] = cb.Checked;
break;
case "DropDown":
DropDownList ddl = (DropDownList)FindControlRecursive(container, boundfield);
if (ddl != null)
dict[boundfield] = ddl.SelectedValue;
break;
default:
break;
}
}
}
}
return dict;
}
private Control FindControlRecursive(Control ctlRoot, string sControlId)
{
// if this control is the one we are looking for, break from the recursion
// and return the control.
if (ctlRoot.ID == sControlId)
{
return ctlRoot;
}
// loop the child controls of this parent control and call recursively.
foreach (Control ctl in ctlRoot.Controls)
{
Control ctlFound = FindControlRecursive(ctl, sControlId);
// if we found the control, return it.
if (ctlFound != null)
{
return ctlFound;
}
}
// we never found the control so just return null.
return null;
}
我在Page_init
中调用它protected void Page_Init(object sender,EventArgs e) {
FormViewEdit.HeaderTemplate = new FormViewEditTemplate(ListItemType.Header, editXmlForm);
FormViewEdit.EditItemTemplate = new FormViewEditTemplate(ListItemType.EditItem, editXmlForm);
FormViewEdit.FooterTemplate = new FormViewEditTemplate(ListItemType.Footer, editXmlForm);
FormViewEdit.ItemUpdating += new FormViewUpdateEventHandler(FormView1_ItemUpdating);
EditFormView.SelectCommand = "SELECT * FROM " + UniqueTable + " WHERE " + PrimaryKey + "=" + ident;
EditFormView.DataBind();
}
protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
//TODO get all new values and make query string.
EditFormView.UpdateCommand = "UPDATE " + UniqueTable + " SET bla_Opis='" + e.NewValues["bla_Opis"] + "' WHERE " + PrimaryKey + "=" + ident;
EditFormView.Update();
EditFormView.DataBind();
}
答案 0 :(得分:0)
我发现了问题。我使用随机生成的id作为点击面板因此我认为双向绑定在post back上没有用。在每次回发中,我都获得了新的id,旧的数据没有找到控制来绑定新的值。
tapPanel.ID = Guid.NewGuid()。ToString();