2014年7月13日 星期日

[RESOLVED] Dynamic FormView


Hello,


Is it possible to add a dynamic formview to a placeholder, with edititemtemplate, insertitemtemplate, and itemtemplate's created dynamically? and I wanted to load the data into that form view from a datatable, if possible? If so does anyone have a simple
example. I've been searching the web and haven't found anything good, everythign that i have found, has been extremely complicated and not what i was looking for.



Any help is very much appreciated.



Thank you very much for your links, but that is alot more advanced than i expected, i'm looking for something a little bit simpler.



I am trying to figure it out,  and have came up with the following code:



using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class test3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable table = GetTable();
FormView frm = new FormView();
frm.ID = "FormView1";
frm.DataSource = table;
frm.DataBind();
DynamicControlsHolder1.Controls.Add(frm);
}

static DataTable GetTable()
{
//
// Here we create a DataTable with four columns.
//
DataTable table = new DataTable();
table.Columns.Add("Dosage", typeof(int));
table.Columns.Add("Drug", typeof(string));
table.Columns.Add("Patient", typeof(string));
table.Columns.Add("Date", typeof(DateTime));

//
// Here we add five DataRows.
//
table.Rows.Add(25, "Indocin", "David", DateTime.Now);
table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
return table;
}


}


But for some reason, it doesn't dislpay anything. Can someone please help me with this?





Hi,


From what I understand you are having difficulty with generating FormView dynamically. According to your code, I find you have not created
ItemTemplate for FormView, that’s why your FormView not shown.


I have created a sample, please try to refer to the following code:


using System;

using System.Collections.Generic;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;



namespace DynamicFormViewWeb

{

public sealed class GenerTemplate : ITemplate

{

void ITemplate.InstantiateIn(Control container)

{

Label DosageLabel = new Label();

DosageLabel.ID = "Dosage";

DosageLabel.DataBinding += new EventHandler(DosageLabel_DataBinding);

LiteralControl lineBreak = new LiteralControl("
");

Label DrugLabel = new Label();

DrugLabel.ID = "Drug";

DrugLabel.DataBinding += new EventHandler(DrugLabel_DataBinding);

Label PatientLabel = new Label();

PatientLabel.ID = "Patient";

PatientLabel.DataBinding += new EventHandler(PatientLabel_DataBinding);

Label DateLabel = new Label();

DateLabel.ID = "Date";

DateLabel.DataBinding += new EventHandler(DateLabel_DataBinding);

//System.Web.UI.WebControls.LinkButton myBtn = new LinkButton();

//myBtn.Text = "dd";

//myBtn.ID = "Edit";

//myBtn.CommandName = "Edit";

container.Controls.Add(DosageLabel);

container.Controls.Add(lineBreak);

container.Controls.Add(DrugLabel);

container.Controls.Add(lineBreak);

container.Controls.Add(PatientLabel);

container.Controls.Add(lineBreak);

container.Controls.Add(DateLabel);

//container.Controls.Add(myBtn);

}

private void DosageLabel_DataBinding(Object sender, EventArgs e)

{

Label DosageLabelControl = (Label)sender;

FormView formViewContainer = (FormView)DosageLabelControl.NamingContainer;

DataRowView rowView = (DataRowView)formViewContainer.DataItem;

DosageLabelControl.Text = rowView["Dosage"].ToString();

}

private void DrugLabel_DataBinding(Object sender, EventArgs e)

{

Label DrugLabelControl = (Label)sender;

FormView formViewContainer = (FormView)DrugLabelControl.NamingContainer;

DataRowView rowView = (DataRowView)formViewContainer.DataItem;

DrugLabelControl.Text = rowView["Drug"].ToString();

}

private void PatientLabel_DataBinding(Object sender, EventArgs e)

{

Label PatientLabelControl = (Label)sender;

FormView formViewContainer = (FormView)PatientLabelControl.NamingContainer;

DataRowView rowView = (DataRowView)formViewContainer.DataItem;

PatientLabelControl.Text = rowView["Patient"].ToString();

}

private void DateLabel_DataBinding(Object sender, EventArgs e)

{

Label DateLabelControl = (Label)sender;

FormView formViewContainer = (FormView)DateLabelControl.NamingContainer;

DataRowView rowView = (DataRowView)formViewContainer.DataItem;

DateLabelControl.Text = rowView["Date"].ToString();

}

}

public partial class WebForm1 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

DataTable table = GetTable();

FormView frm = new FormView();

frm.ID = "FormView2";

frm.ItemTemplate = new GenerTemplate();

frm.DataSource = table;

frm.DataBind();

form1.Controls.Add(frm);

}



static DataTable GetTable()

{

//

// Here we create a DataTable with four columns.

//

DataTable table = new DataTable();

table.Columns.Add("Dosage", typeof(int));

table.Columns.Add("Drug", typeof(string));

table.Columns.Add("Patient", typeof(string));

table.Columns.Add("Date", typeof(DateTime));



//

// Here we add five DataRows.

//

table.Rows.Add(25, "Indocin", "David", DateTime.Now);

table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);

table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);

table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);

table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);

return table;

}

}

}

Hope it can help you, if you have any question, please let me know.


Best Regards,

Terry Guo



Wow, that was awesome, exactly what i was looking for, thank you!!!


沒有留言:

張貼留言