 |
|
 |
|
|
 |
DataGrid Placeholder using templateColumns |
 |
 |
|
|
09-15-05 01:49 AM
Hi there,
I am trying to built a custom Data Grid Place holder Control.
I am able to reach at a point where I can display customizable column in the
data grid. But update is not working.
Here is the description:
In CreateAuthoringChildControls(BaseModeCon
tainer authoringContainer) I
create the datagrid and update and delete buttons.
In LoadPlaceholderContentForAuthoring(Place
holderControlEventArgs e) I load
the content for the data grid, which means I create the columns from XML
Schema, rows from placeholder content if there is any otherwise from default
XML file using dataset.
OnUpdateCommand(object source, DataGridCommandEventArgs e) I try to update
the current data row.
Problem:
Since CreateAuthoringChildControls runs even if I click "Update" button link
so my grid is re-created and I lost all of my data.
So update fails.
Please suggest me what is wrong in this.
Thanks
Poonam
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
RE: DataGrid Placeholder using templateColumns |
 |
 |
|
|
09-16-05 10:59 PM
I have built a functioning Datagrid placeholder using an
XmlPlaceholderDefinition. Without seeing your code I can't say for certain
but I think the problem may be in where you create the datagrid.
I think you may be creating and instantiating the control in
CreateAuthoringChildControls (Datagrid grid = new DataGrid();).
In mine, I declare the datagrid in the Class (protected DataGrid grid;) and
instantiate it in the CreateAuthoringChildControls (grid=new DataGrid();).
Viewstate then maintains the data content of the grid.
"Poonam" wrote:
> Hi there,
> I am trying to built a custom Data Grid Place holder Control.
> I am able to reach at a point where I can display customizable column in t
he
> data grid. But update is not working.
>
> Here is the description:
>
> In CreateAuthoringChildControls(BaseModeCon
tainer authoringContainer) I
> create the datagrid and update and delete buttons.
>
> In LoadPlaceholderContentForAuthoring(Place
holderControlEventArgs e) I loa
d
> the content for the data grid, which means I create the columns from XML
> Schema, rows from placeholder content if there is any otherwise from defau
lt
> XML file using dataset.
>
> OnUpdateCommand(object source, DataGridCommandEventArgs e) I try to update
> the current data row.
>
> Problem:
>
> Since CreateAuthoringChildControls runs even if I click "Update" button li
nk
> so my grid is re-created and I lost all of my data.
>
> So update fails.
>
> Please suggest me what is wrong in this.
>
> Thanks
> Poonam
>
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
RE: DataGrid Placeholder using templateColumns |
 |
 |
|
|
09-16-05 10:59 PM
this is what exactly i am also doing.
Here is the code I have in my grid:
using System;
// Add references to the Microsoft.ContentManagement Namespace
using Microsoft.ContentManagement.Publishing;
using Microsoft.ContentManagement.Publishing.Extensions.Placeholders;
using Microsoft.ContentManagement.WebControls;
using Microsoft.ContentManagement.WebControls.Design;
// Add additional namespaces
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Data;
using System.Text.RegularExpressions;
using System.IO;
using System.Xml;
namespace Adaptec.MCMS.CustomPlaceholders
{
/// <summary>
/// Summary description for DataGridCPlaceHolder.
/// </summary>
public class DataGridCPlaceHolder : BasePlaceholderControl
{
public DataGridCPlaceHolder()
{
//
// TODO: Add constructor logic here
//
}
private string xmlAsString = "";
[
Bindable(false),
Browsable(false),
DesignerSerializationVisibility(Designer
SerializationVisibility.Hidden),
]
public string XmlAsString
{
get
{
ds = (DataSet)ViewState["ds"];
if(ds!=null)
{
return ds.GetXml();
}
else
{
return xmlAsString;
}
}
set
{
xmlAsString = value;
}
}
private DataGrid dg;
private Label lblMessage;
protected override void CreateAuthoringChildControls(BaseModeCon
tainer
authoringContainer)
{
DataGrid tmpGrid = (DataGrid) ViewState["dg"];
if (tmpGrid == null)
{
// Add a DataGrid control to the authoring container
dg = new DataGrid();
// Add an Edit/Update column to the DataGrid
EditCommandColumn c0 = new EditCommandColumn();
c0.EditText = "Edit";
c0.UpdateText = "Update";
dg.Columns.Add(c0);
dg.EditCommand += new DataGridCommandEventHandler(this.OnEditCommand);
dg.UpdateCommand += new
DataGridCommandEventHandler(this.OnUpdateCommand);
// Add a Delete button to the DataGrid
ButtonColumn c1 = new ButtonColumn();
c1.CommandName = "Delete";
c1.Text = "Delete";
dg.Columns.Add(c1);
dg.DeleteCommand += new DataGridCommandEventHandler(this.OnDeleteCommand);
dg.AutoGenerateColumns = false;
//ViewState["dg"] = dg;
}
else
{
dg = tmpGrid;
}
authoringContainer.Controls.Add(dg);
//Add a Label Control for displaying messages
lblMessage = new Label();
authoringContainer.Controls.Add(lblMessage);
//Add an "Add" button to the DataGrid
Button add = new Button();
add.Text = "Add";
add.Click += new EventHandler(this.OnAddCommand);
authoringContainer.Controls.Add(add);
}
private DataSet ds;
protected override void
LoadPlaceholderContentForAuthoring(Place
holderControlEventArgs e)
{
EnsureChildControls();
XmlDocument xd = new XmlDocument();
lblMessage.Text = "";
string savedXMLStr = ((XmlPlaceholder)this.BoundPlaceholder).XmlAsString ;
//Retrieved previously saved XML
bool isEmpty = false;
if(savedXMLStr.Trim()=="")
{
//Default Content is read off from the Description
//property of the bound placeholder
isEmpty = true;
}
else
{
xmlAsString = savedXMLStr;
xd.LoadXml(xmlAsString);
if(xd.DocumentElement.ChildNodes.Count==0)
{
isEmpty = true;
}
}
if (isEmpty)
{
xd.Load(" c:\\inetpub\\wwwroot\\AdaptecCom\\XML\\P
roductVariations.xml");
xmlAsString = xd.InnerXml;
}
try
{
//Convert the loaded XML to a DataSet
System.IO.StringReader sr = new System.IO.StringReader(xmlAsString);
ds = new DataSet();
ds.ReadXmlSchema(" c:\\inetpub\\wwwroot\\AdaptecCom\\XML\\P
roductVarValidatio
n.xsd");
ds.ReadXml(sr, System.Data.XmlReadMode.ReadSchema);
//Store is as part of the View State
ViewState["ds"] = ds;
//Bind the Data to the DataGrid
if(ds.Tables.Count>0)
{
/////////////////////
DataTable dt = ds.Tables[0];
for(int i=0 ; i < dt.Columns.Count ; i++)
{
// Creating Template Column
TemplateColumn tc1 = new TemplateColumn();
tc1.HeaderTemplate = new
DataGridTemplate(ListItemType.Header, dt.Columns[i].ColumnName, "",
null);
// Adding the Rows to the DataGrid
for(int j=0; j< dt.Rows.Count ; j++)
{
string score = dt.Rows[j][i].ToString();
tc1.ItemTemplate = new
DataGridTemplate(ListItemType.Item, score, "STRING", "");
tc1.EditItemTemplate = new
DataGridTemplate(ListItemType.EditItem, score, "STRING", "");
}
dg.Columns.Add(tc1);
}
/////////////////
//Make the first item editable if the
//list is empty
if(isEmpty)
{
dg.EditItemIndex = 0;
}
dg.EnableViewState = true;
dg.DataSource = ds;
dg.DataBind();
}
}
catch(Exception ex)
{
//Uh-oh. We have an error. Write the error details to the Label
lblMessage.Text = "Failed to load placeholder content for authoring" +
ex.Message;
}
}
protected override void
SavePlaceholderContent(PlaceholderContro
lSaveEventArgs e)
{
EnsureChildControls();
try
{
((XmlPlaceholder)this.BoundPlaceholder).XmlAsString = this.XmlAsString;
}
catch(Exception ex)
{
lblMessage.Text = "Save Placeholder Failed: " + ex.Message;
}
}
protected void OnAddCommand(object source, EventArgs e)
{
//Retrieve the DataSet from the ViewState
ds = (DataSet)ViewState["ds"];
//Add a new row to the table
DataTable dt = ds.Tables[0];
DataRow r = dt.NewRow();
dt.Rows.Add(r);
dt.AcceptChanges();
//Save the modified DataSet
ViewState["ds"] = ds;
//Bind the Data to the DataGrid
BindDataToGrid(ds);
/////////////////
dg.DataSource = ds;
dg.DataBind();
//Make the newly added item editable
dg.EditItemIndex = dt.Rows.Count -1;
}
protected void OnDeleteCommand(object source, DataGridCommandEventArgs e)
{
//Retrieve the DataSet from the ViewState
ds = (DataSet)ViewState["ds"];
//Remove the selected row
ds.Tables[0].Rows.RemoveAt(e.Item.ItemIndex);
dg.EditItemIndex = -1;
//Save the modified DataSet
ViewState["ds"] = ds;
//Bind data to the DataGrid
dg.DataSource = ds;
dg.DataBind();
}
protected void OnEditCommand(object source, DataGridCommandEventArgs e)
{
//Set the selected row to be editable
dg.EditItemIndex = e.Item.ItemIndex;
//Retrieve the DataSet from the ViewState
ds = (DataSet)ViewState["ds"];
//Bind data to the DataGrid
dg.DataSource = ds;
dg.DataBind();
}
protected void OnUpdateCommand(object source, DataGridCommandEventArgs e)
{
//Retrieve the DataSet from the ViewState
ds = (DataSet)ViewState["ds"];
//Update the row that has been modified by the author
DataRow row = ds.Tables[0].Rows[e.Item.ItemIndex];
//Retrieve the value of each TextBox
System.Web.UI.Control childcontrol;
for(int i=2;i<e.Item.Cells.Count;i++)
{
childcontrol = e.Item.Cells[i].Controls[0];
if (childcontrol.GetType().Name == "TextBox")
{
row[i-2] = ((TextBox)childcontrol).Text;
}
else if (childcontrol.GetType().Name == "DropDownList")
{
row[i-2] = ((ListBox)childcontrol).SelectedItem.Value ;
}
}
//Save the modified DataSet
row.AcceptChanges();
ds.AcceptChanges();
ViewState["ds"] = ds;
//Bind data to the DataGrid
BindDataToGrid(ds);
dg.DataBind();
//Set all rows to be un-editable
dg.EditItemIndex = -1;
}
protected override void CreatePresentationChildControls(BaseMode
Container
presentationContainer)
{
//Add the DataGrid
dg = new DataGrid();
presentationContainer.Controls.Add(dg);
//Add the Label for error messages
lblMessage = new Label();
presentationContainer.Controls.Add(lblMessage);
}
protected override void
LoadPlaceholderContentForPresentation(Pl
aceholderControlEventArgs e)
{
EnsureChildControls();
lblMessage.Text = "";
try
{
xmlAsString = ((XmlPlaceholder)this.BoundPlaceholder).XmlAsString;
if(xmlAsString!=String.Empty)
{
//Convert the loaded XML to a DataSet
System.IO.StringReader sr = new System.IO.StringReader(xmlAsString);
ds = new DataSet();
ds.ReadXml(sr);
//Bind the Data to the DataGrid
if(ds.Tables.Count>0)
{
dg.DataSource = ds;
dg.DataBind();
}
}
}
catch(Exception ex)
{
//Oops! We have an error. Write the error details to the Label
//In an actual implementation, you may want to hide the DataGrid
altogether
//and not display an error message.
lblMessage.Text = "Failed to load placeholder content for presentation:
" +
ex.Message;
}
}
protected override void Render(System.Web.UI.HtmlTextWriter output)
{
// only do a change if in Authoring mode
if (((WebAuthorContext.Current.Mode ==
WebAuthorContextMode.AuthoringReedit)
||(WebAuthorContext.Current.Mode == WebAuthorContextMode.AuthoringNew)))
{
TextWriter tempWriter = new StringWriter();
base.Render(new System.Web.UI.HtmlTextWriter(tempWriter));
string orightml= tempWriter.ToString();
// This regular expression grabs everthing within the href attribute,
including
//the href attribute itself
Regex hrefRegex = new Regex("href=\"javascript(?<PostBackScript>.*?)\"",
RegexOptions.Singleline);
//Replace the href to point back to the page itself without changing its
URL
//Get the onclick() event to perform the postback instead
string newhtml = hrefRegex.Replace(orightml, "href=\"#\"" +
"onclick=\"${PostBackScript};return false;\"");
output.Write(newhtml);
}
else
{
base.Render(output);
}
}
private void BindDataToGrid(DataSet ds)
{
if(ds.Tables.Count>0)
{
/////////////////////
DataTable dt = ds.Tables[0];
for(int i=0 ; i < dt.Columns.Count ; i++)
{
// Creating Template Column
TemplateColumn tc1 = new TemplateColumn();
tc1.HeaderTemplate = new
DataGridTemplate(ListItemType.Header, dt.Columns[i].ColumnName, "",
null);
// Adding the Rows to the DataGrid
for(int j=0; j< dt.Rows.Count ; j++)
{
string score = dt.Rows[j][i].ToString();
tc1.ItemTemplate = new
DataGridTemplate(ListItemType.Item, score, "STRING", "");
tc1.EditItemTemplate = new
DataGridTemplate(ListItemType.EditItem, score, "STRING", "");
}
dg.Columns.Add(tc1);
}
}
}
}
}
"Tim Pacl" wrote:
[vbcol=seagreen]
> I have built a functioning Datagrid placeholder using an
> XmlPlaceholderDefinition. Without seeing your code I can't say for certain
> but I think the problem may be in where you create the datagrid.
>
> I think you may be creating and instantiating the control in
> CreateAuthoringChildControls (Datagrid grid = new DataGrid();).
>
> In mine, I declare the datagrid in the Class (protected DataGrid grid;) an
d
> instantiate it in the CreateAuthoringChildControls (grid=new DataGrid();).
> Viewstate then maintains the data content of the grid.
>
> "Poonam" wrote:
>
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
RE: DataGrid Placeholder using templateColumns |
 |
 |
|
|
09-16-05 10:59 PM
more than I am doing and that may be the problem:
namespace Dell.HR.Web.CMS.Placeholders
{
/// <summary>
/// Summary description for PlainTextPlaceholder Control.
/// </summary>
[ SupportedPlaceholderDefinitionType(typeo
f(XmlPlaceholderDefinition)) ]
public class HyperLinkPlaceholder : BasePlaceholderControl
{
protected XmlDocument itemXmlDocument;
protected TextBox xmlstruct;
protected DataGrid linkdisplay;
protected DataSet ds;
protected override void CreateAuthoringChildControls(BaseModeCon
tainer
authoringContainer)
{
itemXmlDocument = new XmlDocument();
Label discussion = new Label();
discussion.Width = 600;
discussion.Text = "This MultiLink control allows you to enter as many
hyperlinks as you wish. To add a link, click on \"Edit\" for the bottom
(empty) row, type in your hyperlink information, then click \"Update\" to
save your additions.";
linkdisplay = new DataGrid();
linkdisplay.HeaderStyle.CssClass = "HlinkHeader";
linkdisplay.Width = 600;
xmlstruct = new TextBox();
xmlstruct.TextMode = TextBoxMode.MultiLine;
xmlstruct.Width = 600;
xmlstruct.Height = 400;
EditCommandColumn ecc = new EditCommandColumn();
ecc.HeaderText = "";
ecc.EditText = "Edit";
ecc.UpdateText = "Update";
ecc.CancelText = "Cancel";
linkdisplay.Columns.Add(ecc);
this.linkdisplay.EditCommand += new
DataGridCommandEventHandler(linkdisplay_
EditCommand);
this.linkdisplay.CancelCommand += new
DataGridCommandEventHandler(linkdisplay_
CancelCommand);
this.linkdisplay.UpdateCommand += new
DataGridCommandEventHandler(linkdisplay_
UpdateCommand);
ButtonColumn del = new ButtonColumn();
del.HeaderText = "";
del.Text = "Delete";
del.CommandName = "Delete";
linkdisplay.Columns.Add(del);
this.linkdisplay.DeleteCommand += new
DataGridCommandEventHandler(linkdisplay_
DeleteCommand);
xmlstruct.Visible = false;
authoringContainer.Controls.Add(discussion);
authoringContainer.Controls.Add(linkdisplay);
authoringContainer.Controls.Add(xmlstruct);
}
protected override void
LoadPlaceholderContentForAuthoring(Place
holderControlEventArgs e)
{
EnsureChildControls();
LoadXmlFromPlaceholder();
xmlstruct.Text = itemXmlDocument.OuterXml.ToString();
ds = new DataSet();
System.IO.StringReader xmlSR = new System.IO.StringReader(xmlstruct.Text);
ds.ReadXml(xmlSR);
linkdisplay.DataSource = ds;
linkdisplay.DataBind();
}
This inherits the viewstate from postback to postback. In short, linkdisplay
is declared globally, instantiated in CreateAuthoringChildControls, and is
bound to it's datasource in LoadPlaceholderContentForAuthoring (runs only
once when enter authoring mode). Viewstate will populate the grid on
postbacks.
"Poonam" wrote:
> this is what exactly i am also doing.
> Here is the code I have in my grid:
>
> using System;
>
> // Add references to the Microsoft.ContentManagement Namespace
> using Microsoft.ContentManagement.Publishing;
> using Microsoft.ContentManagement.Publishing.Extensions.Placeholders;
> using Microsoft.ContentManagement.WebControls;
> using Microsoft.ContentManagement.WebControls.Design;
>
> // Add additional namespaces
> using System.Web.UI.WebControls;
> using System.ComponentModel;
> using System.Data;
> using System.Text.RegularExpressions;
> using System.IO;
> using System.Xml;
>
> namespace Adaptec.MCMS.CustomPlaceholders
> {
> /// <summary>
> /// Summary description for DataGridCPlaceHolder.
> /// </summary>
> public class DataGridCPlaceHolder : BasePlaceholderControl
> {
> public DataGridCPlaceHolder()
> {
> //
> // TODO: Add constructor logic here
> //
> }
> private string xmlAsString = "";
> [
> Bindable(false),
> Browsable(false),
> DesignerSerializationVisibility(Design
erSerializationVisibility.Hidden),
> ]
> public string XmlAsString
> {
> get
> {
> ds = (DataSet)ViewState["ds"];
> if(ds!=null)
> {
> return ds.GetXml();
> }
> else
> {
> return xmlAsString;
> }
> }
> set
> {
> xmlAsString = value;
> }
> }
>
> private DataGrid dg;
> private Label lblMessage;
> protected override void CreateAuthoringChildControls(BaseModeCon
tainer
> authoringContainer)
> {
> DataGrid tmpGrid = (DataGrid) ViewState["dg"];
>
> if (tmpGrid == null)
> {
> // Add a DataGrid control to the authoring container
> dg = new DataGrid();
>
> // Add an Edit/Update column to the DataGrid
> EditCommandColumn c0 = new EditCommandColumn();
> c0.EditText = "Edit";
> c0.UpdateText = "Update";
> dg.Columns.Add(c0);
> dg.EditCommand += new DataGridCommandEventHandler(this.OnEditCommand);
> dg.UpdateCommand += new
> DataGridCommandEventHandler(this.OnUpdateCommand);
>
> // Add a Delete button to the DataGrid
> ButtonColumn c1 = new ButtonColumn();
> c1.CommandName = "Delete";
> c1.Text = "Delete";
> dg.Columns.Add(c1);
> dg.DeleteCommand += new DataGridCommandEventHandler(this.OnDeleteComma
nd);
>
> dg.AutoGenerateColumns = false;
> //ViewState["dg"] = dg;
> }
> else
> {
> dg = tmpGrid;
> }
>
> authoringContainer.Controls.Add(dg);
>
> //Add a Label Control for displaying messages
> lblMessage = new Label();
> authoringContainer.Controls.Add(lblMessage);
>
> //Add an "Add" button to the DataGrid
> Button add = new Button();
> add.Text = "Add";
> add.Click += new EventHandler(this.OnAddCommand);
> authoringContainer.Controls.Add(add);
> }
>
>
> private DataSet ds;
> protected override void
> LoadPlaceholderContentForAuthoring(Place
holderControlEventArgs e)
> {
> EnsureChildControls();
> XmlDocument xd = new XmlDocument();
>
> lblMessage.Text = "";
> string savedXMLStr = ((XmlPlaceholder)this.BoundPlaceholder).XmlAsStrin
g ;
> //Retrieved previously saved XML
>
> bool isEmpty = false;
> if(savedXMLStr.Trim()=="")
> {
> //Default Content is read off from the Description
> //property of the bound placeholder
> isEmpty = true;
> }
> else
> {
> xmlAsString = savedXMLStr;
> xd.LoadXml(xmlAsString);
> if(xd.DocumentElement.ChildNodes.Count==0)
> {
> isEmpty = true;
> }
> }
>
> if (isEmpty)
> {
> xd.Load(" c:\\inetpub\\wwwroot\\AdaptecCom\\XML\\P
roductVariations.xml"
);
> xmlAsString = xd.InnerXml;
> }
>
>
> try
> {
> //Convert the loaded XML to a DataSet
> System.IO.StringReader sr = new System.IO.StringReader(xmlAsString);
> ds = new DataSet();
> ds.ReadXmlSchema(" c:\\inetpub\\wwwroot\\AdaptecCom\\XML\\P
roductVarVal
idation.xsd");
> ds.ReadXml(sr, System.Data.XmlReadMode.ReadSchema);
>
> //Store is as part of the View State
> ViewState["ds"] = ds;
>
> //Bind the Data to the DataGrid
> if(ds.Tables.Count>0)
> {
> /////////////////////
> DataTable dt = ds.Tables[0];
> for(int i=0 ; i < dt.Columns.Count ; i++)
> {
> // Creating Template Column
> TemplateColumn tc1 = new TemplateColumn();
>
> tc1.HeaderTemplate = new
> DataGridTemplate(ListItemType.Header, dt.Columns[i].ColumnName,
"",
> null);
>
> // Adding the Rows to the DataGrid
> for(int j=0; j< dt.Rows.Count ; j++)
> {
>
> string score = dt.Rows[j][i].ToString();
> tc1.ItemTemplate = new
> DataGridTemplate(ListItemType.Item, score, "STRING", "");
>
> tc1.EditItemTemplate = new
> DataGridTemplate(ListItemType.EditItem, score, "STRING", "");
>
> }
> dg.Columns.Add(tc1);
> }
> /////////////////
>
> //Make the first item editable if the
> //list is empty
> if(isEmpty)
> {
> dg.EditItemIndex = 0;
> }
> dg.EnableViewState = true;
> dg.DataSource = ds;
> dg.DataBind();
> }
> }
> catch(Exception ex)
> {
> //Uh-oh. We have an error. Write the error details to the Label
> lblMessage.Text = "Failed to load placeholder content for authoring" +
> ex.Message;
> }
>
> }
>
> protected override void
> SavePlaceholderContent(PlaceholderContro
lSaveEventArgs e)
> {
> EnsureChildControls();
> try
> {
> ((XmlPlaceholder)this.BoundPlaceholder).XmlAsString = this.XmlAsString
;
> }
> catch(Exception ex)
> {
> lblMessage.Text = "Save Placeholder Failed: " + ex.Message;
> }
> }
>
> protected void OnAddCommand(object source, EventArgs e)
> {
> //Retrieve the DataSet from the ViewState
> ds = (DataSet)ViewState["ds"];
>
> //Add a new row to the table
> DataTable dt = ds.Tables[0];
> DataRow r = dt.NewRow();
> dt.Rows.Add(r);
> dt.AcceptChanges();
>
> //Save the modified DataSet
> ViewState["ds"] = ds;
>
> //Bind the Data to the DataGrid
> BindDataToGrid(ds);
> /////////////////
>
> dg.DataSource = ds;
> dg.DataBind();
>
> //Make the newly added item editable
> dg.EditItemIndex = dt.Rows.Count -1;
> }
>
> protected void OnDeleteCommand(object source, DataGridCommandEventArgs e
)
> {
> //Retrieve the DataSet from the ViewState
> ds = (DataSet)ViewState["ds"];
>
> //Remove the selected row
> ds.Tables[0].Rows.RemoveAt(e.Item.ItemIndex);
> dg.EditItemIndex = -1;
>
> //Save the modified DataSet
> ViewState["ds"] = ds;
>
> //Bind data to the DataGrid
> dg.DataSource = ds;
> dg.DataBind();
> }
>
> protected void OnEditCommand(object source, DataGridCommandEventArgs e)
> {
> //Set the selected row to be editable
> dg.EditItemIndex = e.Item.ItemIndex;
>
> //Retrieve the DataSet from the ViewState
> ds = (DataSet)ViewState["ds"];
>
> //Bind data to the DataGrid
> dg.DataSource = ds;
> dg.DataBind();
> }
>
> protected void OnUpdateCommand(object source, DataGridCommandEventArgs e
)
> {
> //Retrieve the DataSet from the ViewState
> ds = (DataSet)ViewState["ds"];
>
> //Update the row that has been modified by the author
> DataRow row = ds.Tables[0].Rows[e.Item.ItemIndex];
>
> //Retrieve the value of each TextBox
> System.Web.UI.Control childcontrol;
> for(int i=2;i<e.Item.Cells.Count;i++)
> {
> childcontrol = e.Item.Cells[i].Controls[0];
>
> if (childcontrol.GetType().Name == "TextBox")
> {
> row[i-2] = ((TextBox)childcontrol).Text;
>
> }
> else if (childcontrol.GetType().Name == "DropDownList")
> {
> row[i-2] = ((ListBox)childcontrol).SelectedItem.Value ;
> }
> }
>
> //Save the modified DataSet
> row.AcceptChanges();
> ds.AcceptChanges();
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
RE: DataGrid Placeholder using templateColumns |
 |
 |
|
|
09-16-05 10:59 PM
My problem is since CreateAuthoringChildControl runs everytime I click on
Update button so I loose all my data when "Update" Function runs.
"Tim Pacl" wrote:
[vbcol=seagreen]
> more than I am doing and that may be the problem:
>
> namespace Dell.HR.Web.CMS.Placeholders
> {
> /// <summary>
> /// Summary description for PlainTextPlaceholder Control.
> /// </summary>
> [ SupportedPlaceholderDefinitionType(typeo
f(XmlPlaceholderDefinition)
) ]
> public class HyperLinkPlaceholder : BasePlaceholderControl
> {
> protected XmlDocument itemXmlDocument;
> protected TextBox xmlstruct;
> protected DataGrid linkdisplay;
> protected DataSet ds;
>
> protected override void CreateAuthoringChildControls(BaseModeCon
tainer
> authoringContainer)
> {
> itemXmlDocument = new XmlDocument();
> Label discussion = new Label();
> discussion.Width = 600;
> discussion.Text = "This MultiLink control allows you to enter as many
> hyperlinks as you wish. To add a link, click on \"Edit\" for the bottom
> (empty) row, type in your hyperlink information, then click \"Update\" to
> save your additions.";
> linkdisplay = new DataGrid();
> linkdisplay.HeaderStyle.CssClass = "HlinkHeader";
> linkdisplay.Width = 600;
>
> xmlstruct = new TextBox();
> xmlstruct.TextMode = TextBoxMode.MultiLine;
> xmlstruct.Width = 600;
> xmlstruct.Height = 400;
>
> EditCommandColumn ecc = new EditCommandColumn();
> ecc.HeaderText = "";
> ecc.EditText = "Edit";
> ecc.UpdateText = "Update";
> ecc.CancelText = "Cancel";
> linkdisplay.Columns.Add(ecc);
> this.linkdisplay.EditCommand += new
> DataGridCommandEventHandler(linkdisplay_
EditCommand);
> this.linkdisplay.CancelCommand += new
> DataGridCommandEventHandler(linkdisplay_
CancelCommand);
> this.linkdisplay.UpdateCommand += new
> DataGridCommandEventHandler(linkdisplay_
UpdateCommand);
>
> ButtonColumn del = new ButtonColumn();
> del.HeaderText = "";
> del.Text = "Delete";
> del.CommandName = "Delete";
> linkdisplay.Columns.Add(del);
> this.linkdisplay.DeleteCommand += new
> DataGridCommandEventHandler(linkdisplay_
DeleteCommand);
>
> xmlstruct.Visible = false;
> authoringContainer.Controls.Add(discussion);
> authoringContainer.Controls.Add(linkdisplay);
> authoringContainer.Controls.Add(xmlstruct);
> }
>
> protected override void
> LoadPlaceholderContentForAuthoring(Place
holderControlEventArgs e)
> {
> EnsureChildControls();
> LoadXmlFromPlaceholder();
> xmlstruct.Text = itemXmlDocument.OuterXml.ToString();
> ds = new DataSet();
> System.IO.StringReader xmlSR = new System.IO.StringReader(xmlstruct.Tex
t);
> ds.ReadXml(xmlSR);
> linkdisplay.DataSource = ds;
> linkdisplay.DataBind();
> }
>
> This inherits the viewstate from postback to postback. In short, linkdispl
ay
> is declared globally, instantiated in CreateAuthoringChildControls, and is
> bound to it's datasource in LoadPlaceholderContentForAuthoring (runs only
> once when enter authoring mode). Viewstate will populate the grid on
> postbacks.
>
> "Poonam" wrote:
>
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: DataGrid Placeholder using templateColumns |
 |
 |
|
|
09-16-05 10:59 PM
Hi Poonam,
that is required - otherwise the control would not exist after the postback.
As Tim indicated: Viewstate should refill the content automatically between
CreateAuthoringChildControls and LoadPaceholderContentForAuthoring.
Cheers,
Stefan
--
This posting is provided "AS IS" with no warranties, and confers no rights
New to MCMS?
Check out this book: Building Websites Using MCMS: http://tinyurl.com/6zj44
----------------------
"Poonam" <Poonam@discussions.microsoft.com> wrote in message
news:7CC976B4-D25C-4F69-AFD3-8A35FB70B48D@microsoft.com...[vbcol=seagreen]
> My problem is since CreateAuthoringChildControl runs everytime I click on
> Update button so I loose all my data when "Update" Function runs.
>
> "Tim Pacl" wrote:
>
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
RE: DataGrid Placeholder using templateColumns |
 |
 |
|
|
09-16-05 10:59 PM
I don't think you need this:
DataGrid tmpGrid = (DataGrid) ViewState["dg"];
if (tmpGrid == null)
{
}
else
{
dg = tmpGrid;
}
Just start with dg=new DataGrid(). Also, it looks like you are trying to
store your dataset in the viewstate (ViewState["ds"] = ds;). In my examp
le I
create a TextBox (xmlstruct) to load/store my XML into and use as my
datasource. I simply set visibility to none.
"Poonam" wrote:
[vbcol=seagreen]
> My problem is since CreateAuthoringChildControl runs everytime I click on
> Update button so I loose all my data when "Update" Function runs.
>
> "Tim Pacl" wrote:
>
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
RE: DataGrid Placeholder using templateColumns |
 |
 |
|
|
09-16-05 10:59 PM
This code I added very recently when nothing worked.
Could you please send me your working example so that I can look at the
whole code and try to understand what am i missing here?
"Tim Pacl" wrote:
[vbcol=seagreen]
> I don't think you need this:
>
> DataGrid tmpGrid = (DataGrid) ViewState["dg"];
> if (tmpGrid == null)
> {
> }
> else
> {
> dg = tmpGrid;
> }
>
> Just start with dg=new DataGrid(). Also, it looks like you are trying to
> store your dataset in the viewstate (ViewState["ds"] = ds;). In my exa
mple I
> create a TextBox (xmlstruct) to load/store my XML into and use as my
> datasource. I simply set visibility to none.
>
> "Poonam" wrote:
>
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
RE: DataGrid Placeholder using templateColumns |
 |
 |
|
|
09-19-05 12:49 PM
I am including the custom placeholder code below. But keep in mind that this
is not ready for primetime. Some of Stefan's suggestions for accessing custo
m
properties and other best practices have not been incorporated. Also this
code uses a SiteFunctions class to access custom properties. This class is
not included here. For accessing custom properties, I recommend you read
Stefan's article
(http://blogs.technet.com/stefan_gos.../25/408178.aspx)
about avoiding performance problems (this article refers you to Chester's
article for more tips). I am intending to post a finished version of this
hyperlink placeholder to gotdotnet soon.
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Xml;
using System.Collections;
using System.Text.RegularExpressions;
using Microsoft.ContentManagement.Publishing;
using Microsoft.ContentManagement.Publishing.Extensions.Placeholders;
using Microsoft.ContentManagement.WebControls;
using Microsoft.ContentManagement.WebControls.Design;
namespace Dell.HR.Web.CMS.Placeholders
{
/// <summary>
/// Summary description for PlainTextPlaceholder Control.
/// </summary>
[ SupportedPlaceholderDefinitionType(typeo
f(XmlPlaceholderDefinition)) ]
public class HyperLinkPlaceholder : BasePlaceholderControl
{
protected XmlDocument itemXmlDocument;
protected TextBox xmlstruct;
protected DataGrid linkdisplay;
protected ArrayList hl;
protected DataSet ds;
protected displaytype _displayType;
protected behavior _displayBehavior;
protected string _height;
protected string _width;
public enum displaytype
{
AdBanner,
LinkList
}
public enum behavior
{
All,
First,
Key,
Random
}
[Bindable(true),
Category("Behavior"),
DefaultValue("75px"),
Description("Used only when DisplayType is set to AdBanner. Determines the
height of the ad frame(s).")]
public string FrameHeight
{
get
{
return _height;
}
set
{
if(Regex.IsMatch(value,"\\D"))
_height = value;
else
_height = value + "px";
}
}
[Bindable(true),
Category("Behavior"),
DefaultValue("250px"),
Description("Used only when DisplayType is set to AdBanner. Determines the
width of the ad frame(s).")]
public string FrameWidth
{
get
{
return _width;
}
set
{
if(Regex.IsMatch(value,"\\D"))
_width = value;
else
_width = value + "px";
}
}
[Bindable(true),
Category("Behavior"),
DefaultValue("LinkList"),
Description("Determines whether a link or the content of the specified URL
is displayed.")]
public displaytype DisplayType
{
get
{
return _displayType;
}
set
{
_displayType = value;
}
}
[Bindable(true),
Category("Behavior"),
DefaultValue("First"),
Description("Determines which hyperlinks are used by the control.")]
public behavior DisplayBehavior
{
get
{
return _displayBehavior;
}
set
{
_displayBehavior = value;
}
}
protected override void CreateAuthoringChildControls(BaseModeCon
tainer
authoringContainer)
{
//will serve to populate datasource
itemXmlDocument = new XmlDocument();
//Build control to store XML datasource for persistence on postback
xmlstruct = new TextBox();
xmlstruct.TextMode = TextBoxMode.MultiLine;
xmlstruct.Width = 600;
xmlstruct.Height = 400;
xmlstruct.Visible = false;
//Create datagrid and its label
Label discussion = new Label();
discussion.Width = 600;
discussion.Text = "This MultiLink control allows you to enter as many
hyperlinks as you wish. To add a link, click on \"Edit\" for the bottom
(empty) row, type in your hyperlink information, then click \"Update\" to
save your additions.";
linkdisplay = new DataGrid();
linkdisplay.HeaderStyle.CssClass = "HlinkHeader";
linkdisplay.Width = 600;
//create command columns for datagrid
EditCommandColumn ecc = new EditCommandColumn();
ecc.HeaderText = "";
ecc.EditText = "Edit";
ecc.UpdateText = "Update";
ecc.CancelText = "Cancel";
linkdisplay.Columns.Add(ecc);
ButtonColumn del = new ButtonColumn();
del.HeaderText = "";
del.Text = "Delete";
del.CommandName = "Delete";
linkdisplay.Columns.Add(del);
//provide postback event handlers for command buttons
this.linkdisplay.DeleteCommand += new
DataGridCommandEventHandler(linkdisplay_
DeleteCommand);
this.linkdisplay.EditCommand += new
DataGridCommandEventHandler(linkdisplay_
EditCommand);
this.linkdisplay.CancelCommand += new
DataGridCommandEventHandler(linkdisplay_
CancelCommand);
this.linkdisplay.UpdateCommand += new
DataGridCommandEventHandler(linkdisplay_
UpdateCommand);
//add controls to authoring container
authoringContainer.Controls.Add(discussion);
authoringContainer.Controls.Add(linkdisplay);
authoringContainer.Controls.Add(xmlstruct);
}
protected override void
LoadPlaceholderContentForAuthoring(Place
holderControlEventArgs e)
{
EnsureChildControls();
//establish intial XML on entering authoring mode
LoadXmlFromPlaceholder();
xmlstruct.Text = itemXmlDocument.OuterXml.ToString();
//populate datasource from textbox
ds = new DataSet();
System.IO.StringReader xmlSR = new System.IO.StringReader(xmlstruct.Text);
ds.ReadXml(xmlSR);
linkdisplay.DataSource = ds;
linkdisplay.DataBind();
}
protected override void CreatePresentationChildControls(BaseMode
Container
presentationContainer)
{
itemXmlDocument = new XmlDocument();
hl = new ArrayList();
LoadXmlFromPlaceholder();
//Set properties from Posting Custom Properties if they are set.
string cpFrameHeight =
SiteFunctions.GetCustomPropertyValue(CmsHttpContext.Current.Posting,"FrameHe
ight");
if(cpFrameHeight.Length!=0)
this.FrameHeight = cpFrameHeight;
string cpFrameWidth =
SiteFunctions.GetCustomPropertyValue(CmsHttpContext.Current.Posting,"cpFrame
Width");
if(cpFrameWidth.Length!=0)
this.FrameWidth = cpFrameWidth;
string cpDisplayType =
SiteFunctions.GetCustomPropertyValue(CmsHttpContext.Current.Posting,"Display
Type");
if(cpDisplayType.Length!=0)
{
if(cpDisplayType.ToLower()=="linklist")
this.DisplayType = displaytype.LinkList;
if(cpDisplayType.ToLower()=="adbanner")
this.DisplayType = displaytype.AdBanner;
}
string cpDisplayBehavior =
SiteFunctions.GetCustomPropertyValue(CmsHttpContext.Current.Posting,"Display
Behavior");
if(cpDisplayBehavior.Length!=0)
{
if(cpDisplayBehavior.ToLower()=="all")
this.DisplayBehavior = behavior.All;
if(cpDisplayBehavior.ToLower()=="first")
this.DisplayBehavior = behavior.First;
if(cpDisplayBehavior.ToLower()=="key")
this.DisplayBehavior = behavior.Key;
if(cpDisplayBehavior.ToLower()=="random")
this.DisplayBehavior = behavior.Random;
}
//create controls as determined by properties
if(this.DisplayType==displaytype.LinkList)
{
if(this.DisplayBehavior==behavior.All)
{
//create a hyperlink collection
XmlNodeList hlinkNodes = itemXmlDocument.SelectNodes("//hyperlink");
if(hlinkNodes!=null)
{
for(int x=0;x<hlinkNodes.Count;x++)
{
HyperLink hlink = new HyperLink();
hl.Add(hlink);
presentationContainer.Controls.Add(hlink);
presentationContainer.Controls.Add(new LiteralControl("<br />"));
}
}
}
else
{
//create a single hyperlink
HyperLink hlink = new HyperLink();
hl.Add(hlink);
presentationContainer.Controls.Add(hlink);
presentationContainer.Controls.Add(new LiteralControl("<br />"));
}
}
else
{
if(this.DisplayBehavior==behavior.All)
{
//create an IFRAME collection
XmlNodeList hlinkNodes = itemXmlDocument.SelectNodes("//hyperlink");
if(hlinkNodes!=null)
{
for(int x=0;x<hlinkNodes.Count;x++)
{
HtmlGenericControl iframe = new HtmlGenericControl();
iframe.TagName = "IFRAME";
iframe.Attributes["Scrolling"] = "no";
iframe.Attributes["Frameborder"] = "no";
iframe.Attributes["Height"] = this.FrameHeight;
iframe.Attributes["Width"] = this.FrameWidth;
hl.Add(iframe);
presentationContainer.Controls.Add(iframe);
presentationContainer.Controls.Add(new LiteralControl("<br />"));
}
}
}
else
{
//create an IFRAME
HtmlGenericControl iframe = new HtmlGenericControl();
iframe.TagName = "IFRAME";
iframe.Attributes["Scrolling"] = "no";
iframe.Attributes["Frameborder"] = "no";
iframe.Attributes["Height"] = this.FrameHeight;
iframe.Attributes["Width"] = this.FrameWidth;
hl.Add(iframe);
presentationContainer.Controls.Add(iframe);
presentationContainer.Controls.Add(new LiteralControl("<br />"));
}
}
}
protected override void
LoadPlaceholderContentForPresentation(Pl
aceholderControlEventArgs e)
{
EnsureChildControls();
LoadXmlFromPlaceholder();
//get all hyperlink info
XmlNodeList hlinkNodes = itemXmlDocument.SelectNodes("//hyperlink");
if(hlinkNodes!=null)
{
//if working with hyperlink(s)
if(this.DisplayType==displaytype.LinkList)
{
if(this.DisplayBehavior==behavior.All)
{
//populate hyperlink collection from XML
int index = 0;
foreach(XmlNode hlinkNode in hlinkNodes)
{
HyperLink hlink = (HyperLink)hl[index];
hlink.NavigateUrl = hlinkNode.ChildNodes[0].InnerText;
hlink.Target = hlinkNode.ChildNodes[1].InnerText;
hlink.CssClass = hlinkNode.ChildNodes[2].InnerText;
hlink.Attributes["name"] = hlinkNode.ChildNodes[3].InnerText;
hlink.Text = hlinkNode.ChildNodes[4].InnerText;
index++;
}
}
else if(this.DisplayBehavior==behavior.First)
{
//populate hyperlink from first node
HyperLink hlink = (HyperLink)hl[0];
hlink.NavigateUrl = hlinkNodes[0].ChildNodes[0].InnerText;
hlink.Target = hlinkNodes[0].ChildNodes[1].InnerText;
hlink.CssClass = hlinkNodes[0].ChildNodes[2].InnerText;
hlink.Attributes["name"] = hlinkNodes[0].ChildNodes[3].InnerText
;
hlink.Text = hlinkNodes[0].ChildNodes[4].InnerText;
}
else if(this.DisplayBehavior==behavior.Key)
{
//populate hyperlink from node identified by querystring parameter "key"
string keyvalue = Page.Request["key"].ToLower();
int index = 0;
//get corresponding node
foreach(XmlNode hlink in hlinkNodes)
{
if(hlink.ChildNodes[4].InnerText.ToLower()==keyvalue)
break;
index++;
}
//if corresponding node does not exist, use first
if(index>hlinkNodes.Count)
index = 0;
//populate hyperlink from XML
HyperLink hlinkkey = (HyperLink)hl[0];
hlinkkey.NavigateUrl = hlinkNodes[index].ChildNodes[0].InnerText;
hlinkkey.Target = hlinkNodes[index].ChildNodes[1].InnerText;
hlinkkey.CssClass = hlinkNodes[index].ChildNodes[2].InnerText;
hlinkkey.Attributes["name"] = hlinkNodes[index].ChildNodes[3].In
nerText;
hlinkkey.Text = hlinkNodes[index].ChildNodes[4].InnerText;
}
else
{
//populate hyperlink from a random node
HyperLink hlink = (HyperLink)hl[0];
Random rnd = new Random();
int rndnum = rnd.Next(hlinkNodes.Count);
hlink.NavigateUrl = hlinkNodes[rndnum].ChildNodes[0].InnerText;
hlink.Target = hlinkNodes[rndnum].ChildNodes[1].InnerText;
hlink.CssClass = hlinkNodes[rndnum].ChildNodes[2].InnerText;
hlink.Attributes["name"] = hlinkNodes[rndnum].ChildNodes[3].Inne
rText;
hlink.Text = hlinkNodes[rndnum].ChildNodes[4].InnerText.ToString();
}
}
else
{
//working with IFRAME(s)
if(this.DisplayBehavior==behavior.All)
{
//populate IFRAME collection from XML
int index = 0;
foreach(XmlNode hlinkNode in hlinkNodes)
{
HtmlGenericControl iframe = (HtmlGenericControl)hl[index];
iframe.Attributes["src"] = hlinkNode.ChildNodes[0].InnerText;
iframe.ID = hlinkNode.ChildNodes[1].InnerText+index.ToString();
iframe.Attributes["class"] = hlinkNode.ChildNodes[2].InnerText;
iframe.Attributes["name"] = hlinkNode.ChildNodes[3].InnerText;
iframe.Attributes["title"] = hlinkNode.ChildNodes[4].InnerText;
index++;
}
}
else if(this.DisplayBehavior==behavior.First)
{
//populate IFRAME from first node information
HtmlGenericControl iframe = (HtmlGenericControl)hl[0];
iframe.Attributes["src"] = hlinkNodes[0].ChildNodes[0].InnerText
;
iframe.ID = hlinkNodes[0].ChildNodes[1].InnerText.ToString();
iframe.Attributes["class"] = hlinkNodes[0].ChildNodes[2].InnerTe
xt;
iframe.Attributes["name"] = hlinkNodes[0].ChildNodes[3].InnerTex
t;
iframe.Attributes["title"] = hlinkNodes[0].ChildNodes[4].InnerTe
xt;
}
else if(this.DisplayBehavior==behavior.Key)
{
//populate IFRAME from node identified by querystring parameter "key"
string keyvalue = Page.Request["key"].ToLower();
int index = 0;
//get corresponding node
foreach(XmlNode hlink in hlinkNodes)
{
if(hlink.ChildNodes[4].InnerText.ToLower()==keyvalue)
break;
index++;
}
//if corresponding node does not exist, use first
if(index>hlinkNodes.Count)
index = 0;
//populate IFRAME from XML
HtmlGenericControl iframe = (HtmlGenericControl)hl[0];
iframe.Attributes["src"] = hlinkNodes[index].ChildNodes[0].Inner
Text;
iframe.ID = hlinkNodes[index].ChildNodes[1].InnerText.ToString();
iframe.Attributes["class"] = hlinkNodes[index].ChildNodes[2].Inn
erText;
iframe.Attributes["name"] = hlinkNodes[index].ChildNodes[3].Inne
rText;
iframe.Attributes["title"] =
hlinkNodes[index].ChildNodes[4].InnerText+index.ToString();
}
else
{
//populate IFRAME from a random node
HtmlGenericControl iframe = (HtmlGenericControl)hl[0];
Random rnd = new Random();
int rndnum = rnd.Next(hlinkNodes.Count-1);
iframe.Attributes["src"] = hlinkNodes[rndnum].ChildNodes[0].Inne
rText;
iframe.ID = hlinkNodes[rndnum].ChildNodes[1].InnerText.ToString();
iframe.Attributes["class"] = hlinkNodes[rndnum].ChildNodes[2].In
nerText;
iframe.Attributes["name"] = hlinkNodes[rndnum].ChildNodes[3].Inn
erText;
iframe.Attributes["title"] =
hlinkNodes[rndnum].ChildNodes[4].InnerText+rndnum.ToString();
}
}
}
}
//save XML to placeholder
protected override void
SavePlaceholderContent(PlaceholderContro
lSaveEventArgs e)
{
itemXmlDocument.LoadXml(xmlstruct.Text);
((XmlPlaceholder)this.BoundPlaceholder).XmlAsString =
itemXmlDocument.OuterXml.ToString();
}
//retrieve XML from placeholder
private void LoadXmlFromPlaceholder()
{
string tempXml;
tempXml = ((XmlPlaceholder)this.BoundPlaceholder).XmlAsString;
if(tempXml != null && tempXml != "")
{
//load XML from placeholder
itemXmlDocument.LoadXml(tempXml);
}
else
{
//create basic XML node structure required for this control
if(itemXmlDocument.OuterXml.ToString().Length==0)
CreateBasicXml();
}
}
//create basic XML node structure
private void CreateBasicXml()
{
XmlNode rootNode =
itemXmlDocument.CreateNode(XmlNodeType.Element,"links","");
itemXmlDocument.AppendChild(rootNode);
rootNode.AppendChild(AppendHyperLinkNodeSet());
}
//defines all xml nodes needed to store info for one hyperlink
private XmlNode AppendHyperLinkNodeSet()
{
XmlNode linkNode =
itemXmlDocument.CreateNode(XmlNodeType.Element,"hyperlink","");
XmlNode hrefNode =
itemXmlDocument.CreateNode(XmlNodeType.Element,"href","");
XmlNode targetNode =
itemXmlDocument.CreateNode(XmlNodeType.Element,"target","");
XmlNode classNode =
itemXmlDocument.CreateNode(XmlNodeType.Element,"class","");
XmlNode nameNode =
itemXmlDocument.CreateNode(XmlNodeType.Element,"name","");
XmlNode textNode =
itemXmlDocument.CreateNode(XmlNodeType.Element,"linktext","");
linkNode.AppendChild(hrefNode);
linkNode.AppendChild(targetNode);
linkNode.AppendChild(classNode);
linkNode.AppendChild(nameNode);
linkNode.AppendChild(textNode);
return linkNode;
}
//generate a random number from 0 to seed
private int RandomNumber(int seed)
{
Random rnd = new Random();
int rndnum = rnd.Next(seed);
if(rndnum==seed)
rndnum = rnd.Next(seed);
return rndnum;
}
void linkdisplay_EditCommand(Object sender, DataGridCommandEventArgs e)
{
// Set the EditItemIndex property to the index of the item clicked
// in the DataGrid control to enable editing for that item. Be sure
// to rebind the DateGrid to the data source to refresh the control.
DataGrid temp = (DataGrid)e.Item.Parent.NamingContainer;
temp.EditItemIndex = e.Item.ItemIndex;
ds = new DataSet();
System.IO.StringReader xmlSR = new System.IO.StringReader(xmlstruct.Text);
ds.ReadXml(xmlSR);
temp.DataSource = ds;
temp.DataBind();
}
void linkdisplay_CancelCommand(Object sender, DataGridCommandEventArgs e)
{
// Set the EditItemIndex property to the index of the item clicked
// in the DataGrid control to enable editing for that item. Be sure
// to rebind the DateGrid to the data source to refresh the control.
DataGrid temp = (DataGrid)e.Item.Parent.NamingContainer;
temp.EditItemIndex = -1;
ds = new DataSet();
System.IO.StringReader xmlSR = new System.IO.StringReader(xmlstruct.Text);
ds.ReadXml(xmlSR);
temp.DataSource = ds;
temp.DataBind();
}
void linkdisplay_DeleteCommand(Object sender, DataGridCommandEventArgs e)
{
// Get the index of the item clicked in the DataGrid control. Be sure
// to rebind the DateGrid to the data source to refresh the control.
itemXmlDocument.LoadXml(xmlstruct.Text);
int index = e.Item.ItemIndex;
//make sure selected index is not last (empty) hyperlink
if(index<itemXmlDocument.SelectNodes("//hyperlink").Count-1)
{
//remove the selected node from the source XML
XmlNode hlinkNode = itemXmlDocument.SelectNodes("//hyperlink")[index];
if(hlinkNode!=null)
{
hlinkNode.ParentNode.RemoveChild(hlinkNode);
//save XML
xmlstruct.Text = itemXmlDocument.OuterXml.ToString();
SavePlaceholderContent(null);
}
}
DataGrid temp = (DataGrid)e.Item.Parent.NamingContainer;
temp.EditItemIndex = -1;
ds = new DataSet();
System.IO.StringReader xmlSR = new System.IO.StringReader(xmlstruct.Text);
ds.ReadXml(xmlSR);
temp.DataSource = ds;
temp.DataBind();
}
void linkdisplay_UpdateCommand(Object sender, DataGridCommandEventArgs e)
{
// Get the index of the item clicked in the DataGrid control. Be sure
// to rebind the DateGrid to the data source to refresh the control.
itemXmlDocument.LoadXml(xmlstruct.Text);
int index = e.Item.ItemIndex;
//ensure a valid index
if(index<itemXmlDocument.SelectNodes("//hyperlink").Count)
{
XmlNode hlinkNode = itemXmlDocument.SelectNodes("//hyperlink")[index];
//update XML for selected node. start at 2 because first 2 columns are
buttons
int x = 2;
foreach(XmlNode node in hlinkNode.ChildNodes)
{
node.InnerText = ((TextBox)e.Item.Cells[x].Controls[0]).Text;
x++;
}
//if selected index is last (empty) node, create new empty node.
if(index==itemXmlDocument.SelectNodes("//hyperlink").Count-1)
itemXmlDocument.ChildNodes[0].AppendChild(AppendHyperLinkNodeSet());
//save XML
xmlstruct.Text = itemXmlDocument.OuterXml.ToString();
SavePlaceholderContent(null);
}
DataGrid temp = (DataGrid)e.Item.Parent.NamingContainer;
temp.EditItemIndex = -1;
ds = new DataSet();
System.IO.StringReader xmlSR = new System.IO.StringReader(xmlstruct.Text);
ds.ReadXml(xmlSR);
temp.DataSource = ds;
temp.DataBind();
}
}
}
"Poonam" wrote:
[vbcol=seagreen]
> This code I added very recently when nothing worked.
> Could you please send me your working example so that I can look at the
> whole code and try to understand what am i missing here?
>
> "Tim Pacl" wrote:
>
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
RE: DataGrid Placeholder using templateColumns |
 |
 |
|
|
09-19-05 11:00 PM
Hi Tim,
I saw your complete code. and it works fine. My previous version of Grid
Control was working fine because I was not using Template columns. The momen
t
I started to use template column I ran into trouble.
"Tim Pacl" wrote:
> I am including the custom placeholder code below. But keep in mind that th
is
> is not ready for primetime. Some of Stefan's suggestions for accessing cus
tom
> properties and other best practices have not been incorporated. Also this
> code uses a SiteFunctions class to access custom properties. This class is
> not included here. For accessing custom properties, I recommend you read
> Stefan's article
> (http://blogs.technet.com/stefan_gos.../25/408178.aspx)
> about avoiding performance problems (this article refers you to Chester's
> article for more tips). I am intending to post a finished version of this
> hyperlink placeholder to gotdotnet soon.
>
> using System;
> using System.Data;
> using System.Web.UI;
> using System.Web.UI.HtmlControls;
> using System.Web.UI.WebControls;
> using System.ComponentModel;
> using System.Xml;
> using System.Collections;
> using System.Text.RegularExpressions;
> using Microsoft.ContentManagement.Publishing;
> using Microsoft.ContentManagement.Publishing.Extensions.Placeholders;
> using Microsoft.ContentManagement.WebControls;
> using Microsoft.ContentManagement.WebControls.Design;
>
>
> namespace Dell.HR.Web.CMS.Placeholders
> {
> /// <summary>
> /// Summary description for PlainTextPlaceholder Control.
> /// </summary>
> [ SupportedPlaceholderDefinitionType(typeo
f(XmlPlaceholderDefinition)
) ]
> public class HyperLinkPlaceholder : BasePlaceholderControl
> {
> protected XmlDocument itemXmlDocument;
> protected TextBox xmlstruct;
> protected DataGrid linkdisplay;
> protected ArrayList hl;
> protected DataSet ds;
> protected displaytype _displayType;
> protected behavior _displayBehavior;
> protected string _height;
> protected string _width;
>
> public enum displaytype
> {
> AdBanner,
> LinkList
> }
> public enum behavior
> {
> All,
> First,
> Key,
> Random
> }
>
> [Bindable(true),
> Category("Behavior"),
> DefaultValue("75px"),
> Description("Used only when DisplayType is set to AdBanner. Determines t
he
> height of the ad frame(s).")]
> public string FrameHeight
> {
> get
> {
> return _height;
> }
>
> set
> {
> if(Regex.IsMatch(value,"\\D"))
> _height = value;
> else
> _height = value + "px";
> }
> }
>
> [Bindable(true),
> Category("Behavior"),
> DefaultValue("250px"),
> Description("Used only when DisplayType is set to AdBanner. Determines t
he
> width of the ad frame(s).")]
> public string FrameWidth
> {
> get
> {
> return _width;
> }
>
> set
> {
> if(Regex.IsMatch(value,"\\D"))
> _width = value;
> else
> _width = value + "px";
> }
> }
>
> [Bindable(true),
> Category("Behavior"),
> DefaultValue("LinkList"),
> Description("Determines whether a link or the content of the specified U
RL
> is displayed.")]
> public displaytype DisplayType
> {
> get
> {
> return _displayType;
> }
>
> set
> {
> _displayType = value;
> }
> }
>
> [Bindable(true),
> Category("Behavior"),
> DefaultValue("First"),
> Description("Determines which hyperlinks are used by the control.")]
> public behavior DisplayBehavior
> {
> get
> {
> return _displayBehavior;
> }
>
> set
> {
> _displayBehavior = value;
> }
> }
>
> protected override void CreateAuthoringChildControls(BaseModeCon
tainer
> authoringContainer)
> {
> //will serve to populate datasource
> itemXmlDocument = new XmlDocument();
>
> //Build control to store XML datasource for persistence on postback
> xmlstruct = new TextBox();
> xmlstruct.TextMode = TextBoxMode.MultiLine;
> xmlstruct.Width = 600;
> xmlstruct.Height = 400;
> xmlstruct.Visible = false;
>
> //Create datagrid and its label
> Label discussion = new Label();
> discussion.Width = 600;
> discussion.Text = "This MultiLink control allows you to enter as many
> hyperlinks as you wish. To add a link, click on \"Edit\" for the bottom
> (empty) row, type in your hyperlink information, then click \"Update\" to
> save your additions.";
> linkdisplay = new DataGrid();
> linkdisplay.HeaderStyle.CssClass = "HlinkHeader";
> linkdisplay.Width = 600;
>
> //create command columns for datagrid
> EditCommandColumn ecc = new EditCommandColumn();
> ecc.HeaderText = "";
> ecc.EditText = "Edit";
> ecc.UpdateText = "Update";
> ecc.CancelText = "Cancel";
> linkdisplay.Columns.Add(ecc);
>
> ButtonColumn del = new ButtonColumn();
> del.HeaderText = "";
> del.Text = "Delete";
> del.CommandName = "Delete";
> linkdisplay.Columns.Add(del);
>
> //provide postback event handlers for command buttons
> this.linkdisplay.DeleteCommand += new
> DataGridCommandEventHandler(linkdisplay_
DeleteCommand);
> this.linkdisplay.EditCommand += new
> DataGridCommandEventHandler(linkdisplay_
EditCommand);
> this.linkdisplay.CancelCommand += new
> DataGridCommandEventHandler(linkdisplay_
CancelCommand);
> this.linkdisplay.UpdateCommand += new
> DataGridCommandEventHandler(linkdisplay_
UpdateCommand);
>
> //add controls to authoring container
> authoringContainer.Controls.Add(discussion);
> authoringContainer.Controls.Add(linkdisplay);
> authoringContainer.Controls.Add(xmlstruct);
> }
>
> protected override void
> LoadPlaceholderContentForAuthoring(Place
holderControlEventArgs e)
> {
> EnsureChildControls();
>
> //establish intial XML on entering authoring mode
> LoadXmlFromPlaceholder();
> xmlstruct.Text = itemXmlDocument.OuterXml.ToString();
>
> //populate datasource from textbox
> ds = new DataSet();
> System.IO.StringReader xmlSR = new System.IO.StringReader(xmlstruct.Tex
t);
> ds.ReadXml(xmlSR);
> linkdisplay.DataSource = ds;
> linkdisplay.DataBind();
> }
>
> protected override void CreatePresentationChildControls(BaseMode
Containe
r
> presentationContainer)
> {
> itemXmlDocument = new XmlDocument();
> hl = new ArrayList();
> LoadXmlFromPlaceholder();
>
> //Set properties from Posting Custom Properties if they are set.
> string cpFrameHeight =
> SiteFunctions.GetCustomPropertyValue(CmsHttpContext.Current.Posting,"Frame
Height");
> if(cpFrameHeight.Length!=0)
> this.FrameHeight = cpFrameHeight;
> string cpFrameWidth =
> SiteFunctions.GetCustomPropertyValue(CmsHttpContext.Current.Posting,"cpFra
meWidth");
> if(cpFrameWidth.Length!=0)
> this.FrameWidth = cpFrameWidth;
> string cpDisplayType =
> SiteFunctions.GetCustomPropertyValue(CmsHttpContext.Current.Posting,"Displ
ayType");
> if(cpDisplayType.Length!=0)
> {
> if(cpDisplayType.ToLower()=="linklist")
> this.DisplayType = displaytype.LinkList;
> if(cpDisplayType.ToLower()=="adbanner")
> this.DisplayType = displaytype.AdBanner;
> }
> string cpDisplayBehavior =
> SiteFunctions.GetCustomPropertyValue(CmsHttpContext.Current.Posting,"Displ
ayBehavior");
> if(cpDisplayBehavior.Length!=0)
> {
> if(cpDisplayBehavior.ToLower()=="all")
> this.DisplayBehavior = behavior.All;
> if(cpDisplayBehavior.ToLower()=="first")
> this.DisplayBehavior = behavior.First;
> if(cpDisplayBehavior.ToLower()=="key")
> this.DisplayBehavior = behavior.Key;
> if(cpDisplayBehavior.ToLower()=="random")
> this.DisplayBehavior = behavior.Random;
> }
>
> //create controls as determined by properties
> if(this.DisplayType==displaytype.LinkList)
> {
> if(this.DisplayBehavior==behavior.All)
> {
> //create a hyperlink collection
> XmlNodeList hlinkNodes = itemXmlDocument.SelectNodes("//hyperlink");
> if(hlinkNodes!=null)
> {
> for(int x=0;x<hlinkNodes.Count;x++)
> {
> HyperLink hlink = new HyperLink();
> hl.Add(hlink);
> presentationContainer.Controls.Add(hlink);
> presentationContainer.Controls.Add(new LiteralControl("<br />"));
> }
> }
> }
> else
> {
> //create a single hyperlink
> HyperLink hlink = new HyperLink();
> hl.Add(hlink);
> presentationContainer.Controls.Add(hlink);
> presentationContainer.Controls.Add(new LiteralControl("<br />"));
> }
> }
> else
> {
> if(this.DisplayBehavior==behavior.All)
> {
> //create an IFRAME collection
> XmlNodeList hlinkNodes = itemXmlDocument.SelectNodes("//hyperlink");
> if(hlinkNodes!=null)
> {
> for(int x=0;x<hlinkNodes.Count;x++)
> {
> HtmlGenericControl iframe = new HtmlGenericControl();
> iframe.TagName = "IFRAME";
> iframe.Attributes["Scrolling"] = "no";
> iframe.Attributes["Frameborder"] = "no";
> iframe.Attributes["Height"] = this.FrameHeight;
> iframe.Attributes["Width"] = this.FrameWidth;
> hl.Add(iframe);
> presentationContainer.Controls.Add(iframe);
> presentationContainer.Controls.Add(new LiteralControl("<br />"));
> }
> }
> }
> else
> {
> //create an IFRAME
> HtmlGenericControl iframe = new HtmlGenericControl();
> iframe.TagName = "IFRAME";
> iframe.Attributes["Scrolling"] = "no";
> iframe.Attributes["Frameborder"] = "no";
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
|
Sponsored Links |
 |
 |
|
|
 |
All times are GMT. The time now is 06:47 PM. |
 |
|
|
 |
|
 |
|
|
 |
|
| |
|