顯示具有 DynamicControl 標籤的文章。 顯示所有文章
顯示具有 DynamicControl 標籤的文章。 顯示所有文章

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!!!


[RESOLVED] Ajaxmodalpopup extender goes to new page instead of popping up window


I am trying to create a gridview inside a modalpopup extnder. But as soon as i click the button that triggers the event to create it, it seems to just act like a link to another page instead of popping up a window. Below is the code i have:



protected void Page_Load(object sender, EventArgs e)
{
DataTable table = GetTable();
GridView gv = new GridView();
gv.ID = "GridView1";
gv.DataSource = table;
gv.DataBind();

Button btn = new Button();
btn.ID = "button1";
btn.Click += (sender, e) => CreateModalPop(ph, btn.ID);

DynamicControlsHolder1.Controls.Add(gv);
DynamicControlsHolder1.Controls.Add(btn);
}


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;
}


protected void CreateModalPop(PlaceHolder ph, string btnID)
{

DataTable tableNew = GetTable();

GridView gv = new GridView();
gv.ID = "GridViewNew1"
gv.AutoGenerateColumns = true;
gv.DataSource = tableNew;
gv.DataBind();


Panel pn = new Panel();
pn.ID = "Panel1";
pn.Controls.Add(gv);


AjaxControlToolkit.ModalPopupExtender modalPop = new AjaxControlToolkit.ModalPopupExtender();
modalPop.ID = "ModalPopup-1";
modalPop.PopupControlID = "Panel1"
modalPop.TargetControlID = btnID;
modalPop.X = 50;
modalPop.Y = 50;
modalPop.Show();

// Adding modalpop to panel
pn.Controls.Add(modalPop);

// Adding Panel to placeholder
ph.Controls.Add(pn);

}







It's just doing a postback.  If you put it inside of an updatepanel you won't see the postback.



So i should try to put it inside of an updatepanel instead of regular panel?



You put you regular panel inside of the UpdatePanel.  The UpdatePanel just uses AJAX so you do not see the postback. 










then if you want an ajax loader (in case it takes too long, you don't get a response) download an ajax image loader here: http://www.ajaxload.info/


Then add this code where you want your loader to go.




Loading...








I just modified the code and tried that, and get the same result. This thing is driving me crazy, i can't figure out what i am doing wrong here.


protected void Page_Load(object sender, EventArgs e)
{
DataTable table = GetTable();
GridView gv = new GridView();
gv.ID = "GridView1";
gv.DataSource = table;
gv.DataBind();

Button btn = new Button();
btn.ID = "button1";
btn.Click += (sender, e) => CreateModalPop(ph, btn.ID);

DynamicControlsHolder1.Controls.Add(gv);
DynamicControlsHolder1.Controls.Add(btn);
}


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;
}


protected void CreateModalPop(PlaceHolder ph, string btnID)
{

DataTable tableNew = GetTable();

GridView gv = new GridView();
gv.ID = "GridViewNew1"
gv.AutoGenerateColumns = true;
gv.DataSource = tableNew;
gv.DataBind();


Panel pn = new Panel();
pn.ID = "Panel1";
pn.Controls.Add(gv);


UpdatePanel up = new UpdatePanel();
up.ID = "UpdatePanel1";
up.ContentTemplateContainer.Controls.Add(pn);

AjaxControlToolkit.ModalPopupExtender modalPop = new AjaxControlToolkit.ModalPopupExtender();
modalPop.ID = "ModalPopup-1";
modalPop.PopupControlID = "UpdatePanel1"
modalPop.TargetControlID = btnID;
modalPop.X = 50;
modalPop.Y = 50;

// Adding modalpop to panel
pn.Controls.Add(modalPop);

// Adding Panel to placeholder
ph.Controls.Add(up);

        modalPop.Show();


 }







You aren't setting a css class of your Panel (Panel1) or of the MPE (ModalPopup-1).  I think without that, it just has default values.  Here is an example I have:


set your Panel CssClass = "modalPopup" and your MPE CssClass = .modalBackground.  Then you can play around with it.


.modalBackground { background-color: Gray; filter: alpha(opacity=70); opacity: 0.7; cursor:not-allowed; position: absolute; top: 0%; left: 0%; width: 100%; height: 140%; overflow:hidden;}

.modalPopup { background-color: #ffffdd; border-width: 3px; border-style: solid; border-color: Gray; padding: 3px; width: 350px; }






[RESOLVED] Creating Persistent Dynamic Controls from Code Behind


I need to create a List of placeholder dynamically, In each placeholder there can be Label, texbox, gridview with template field, etc. The created controls and its values should be retained in postback.  What is the simplest way to get this. 


Thanks,


Bala



Hi,


I think the solution in
here
is what you are looking for as the following:


Yes, this is possible. One way to do this using purely ASP.NET (which seems like what you're asking for) would be to keep a count of the TextBox


controls that you have added (storing that value in the ViewState) and recreate the TextBox
controls in the Page_Load event. Of course, nowadays most people would probably use Javascript or jQuery to handle this task client side, but I put together a quick example to demonstrate how it works with postbacks:


Front page:


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DynamicControls.aspx.cs" Inherits="MyAspnetApp.DynamicControls" EnableViewState="true" %>













 


Code behind:


using System;
using System.Web.UI.WebControls;

namespace MyAspnetApp
{
public partial class DynamicControls : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Recreate textbox controls
if(Page.IsPostBack)
{
for (var i = 0; i < TextBoxCount; i++)
AddTextBox(i);
}
}

private int TextBoxCount
{
get
{
var count = ViewState["txtBoxCount"];
return (count == null) ? 0 : (int) count;
}
set { ViewState["txtBoxCount"] = value; }
}

private void AddTextBox(int index)
{
var txt = new TextBox {ID = string.Concat("txtDynamic", index)};
txt.Style.Add("display", "block");
phControls.Controls.Add(txt);
}

protected void btnAddTextBox_Click(object sender, EventArgs e)
{
AddTextBox(TextBoxCount);
TextBoxCount++;
}

protected void btnWriteValues_Click(object sender, EventArgs e)
{
foreach(var control in phControls.Controls)
{
var textBox = control as TextBox;
if (textBox == null) continue;
Response.Write(string.Concat(textBox.Text, "
"));
}
}
}
}

 


Since you are recreating the controls on each postback, the values entered into the textboxes will be persisted across each postback. I added btnWriteValues_Click to quickly demonstrate how to read the values out of the textboxes.


EDIT
I updated the example to add a Panel containing a TextBox and a Remove Button. The trick here is that the Remove button does not delete the container Panel, it merely makes it not Visible. This is done so that all of the control IDs remain the same, so the data entered stays with each TextBox. If we were to remove the TextBox entirely, the data after the TextBox that was removed would shift down one TextBox on the next postback (just to explain this a little more clearly, if we have txt1, txt2 and txt3, and we remove txt2, on the next postback we'll create two textboxes, txt1 and txt2, and the value that was in txt3 would be lost).


public partial class DynamicControls : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
for (var i = 0; i < TextBoxCount; i++)
AddTextBox(i);
}
}

protected void btnAddTextBox_Click(object sender, EventArgs e)
{
AddTextBox(TextBoxCount);
TextBoxCount++;
}

protected void btnWriteValues_Click(object sender, EventArgs e)
{
foreach(var control in phControls.Controls)
{
var panel = control as Panel;
if (panel == null || !panel.Visible) continue;
foreach (var control2 in panel.Controls)
{
var textBox = control2 as TextBox;
if (textBox == null) continue;
Response.Write(string.Concat(textBox.Text, "
"));
}
}
}

private int TextBoxCount
{
get
{
var count = ViewState["txtBoxCount"];
return (count == null) ? 0 : (int) count;
}
set { ViewState["txtBoxCount"] = value; }
}

private void AddTextBox(int index)
{
var panel = new Panel();
panel.Controls.Add(new TextBox {ID = string.Concat("txtDynamic", index)});
var btn = new Button { Text="Remove" };
btn.Click += btnRemove_Click;
panel.Controls.Add(btn);
phControls.Controls.Add(panel);
}

private void btnRemove_Click(object sender, EventArgs e)
{
var btnRemove = sender as Button;
if (btnRemove == null) return;
btnRemove.Parent.Visible = false;
}
}

Best Regards.