Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
255
Get dynamically created control in the WebDataGrid on submit button
posted

Hi ,

I not able to find control from webdata through FindControl() method and below is the code.

protected void BtnUpdate_Click(object sender, EventArgs e)
{

if (this.wbDatGrid.Rows.Count > 0)
{
foreach (GridRecord row in wbDatGrid.Rows)
{
DropDownList cmbAccess = new DropDownList();
cmbAccess = (DropDownList)row.Items.FindItemByKey("cmbAccess"); // Get Error

SaveDatInBD(ID, cmbAccess.SelectedValue);

}

}
}

--------------------------------------------------- FULL CODE -------------------

-- ASPX Code
----------------
<%@ register="" assembly="Infragistics45.Web.v16.2, Version=16.2.20162.2013, Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb" namespace="Infragistics.Web.UI.GridControls" tagprefix="ig">
<%@ register="" assembly="Infragistics45.Web.v16.2, Version=16.2.20162.2013, Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb" namespace="Infragistics.Web.UI.LayoutControls" tagprefix="cc1">

OnInitializeRow="wbData_InitializeRow" EnableAjax="true" EnableDataViewState="true"
OnItemCommand="wbData_ItemCommand">














-- ASPX CS Code
----------------
protected void Page_Load(object sender, EventArgs e)
{
BindGrid();

if (IsPostBack)
{

this.wbData.EnsureTemplates();
}
}


public void BindGrid();
{
GenericHelper.ParseXML("PageTemplate", ref this.wbData);

this.wbData.DataSource = GetDataFromDB();
this.wbData.DataBind();
}


protected void wbData_InitializeRow(object sender, RowEventArgs e)
{
GridRecordItem gric = e.Row.Items[0];

string gridID = (gric.Record).Items[0].Value.ToString();
string clsID = (gric.Record).Items[2].Value.ToString();


List clsList = GetDropdoenValue();
e.Row.Items[3].Template = new DropDownCustomItemTemplate()
{
ID = "cmbAccess",
RowIndex = e.Row.Index.ToString(),
DTdatasource = clsList,
DdlDataTextField = "Name",
DdlDataValueField = "Id",
DdlSelectedValue = clsID;
};

e.Row.Items[4].Template = new DeleteCustomItemTemplate() { ID = "Remove", CommandArg = gridID, RowIndex = e.Row.Index.ToString() };
}


protected void BtnUpdate_Click(object sender, EventArgs e)
{

if (this.wbDatGrid.Rows.Count > 0)
{
foreach (GridRecord row in wbDatGrid.Rows)
{
DropDownList cmbAccess = new DropDownList();
cmbAccess = (DropDownList)row.Items.FindItemByKey("cmbAccess"); // Get Error

SaveDatInBD(ID, cmbAccess.SelectedValue);

}

}
}

-------------- Tempalte
public class CustomItemTemplate : ITemplate
{
public string ID { get; set; }
public string RowIndex { get; set; }
public string CommandArg { get; set; }

public CustomItemTemplate() {}

public CustomItemTemplate(string _Id, string _rowIndex, string _commandArg)
{
this.ID = _Id;
this.RowIndex = _rowIndex;
this.CommandArg = _commandArg;
}

public virtual void InstantiateIn(Control container)
{}
}

public class EditCustomItemTemplate : CustomItemTemplate
{
public EditCustomItemTemplate() {}

public EditCustomItemTemplate(string _Id, string _rowIndex, string _commandArg)
{
this.ID = _Id;
this.RowIndex = _rowIndex;
this.CommandArg = _commandArg;
}

public override void InstantiateIn(Control container)
{
ImageButton imgBtn = new ImageButton();
imgBtn.ID = ID;
imgBtn.ID = "imgEdit";
imgBtn.EnableViewState = false;
imgBtn.CommandArgument = CommandArg;
imgBtn.CommandName = "Edit";
imgBtn.ViewStateMode = ViewStateMode.Enabled;
imgBtn.ImageUrl = GenericGridConstants.CONTROL_EDITIMAGE;
container.Controls.Add(imgBtn);
}

}

public class DeleteCustomItemTemplate : CustomItemTemplate
{

public DeleteCustomItemTemplate() { }

public DeleteCustomItemTemplate(string _Id, string _rowIndex, string _commandArg)
{
this.ID = _Id;
this.RowIndex = _rowIndex;
this.CommandArg = _commandArg;
}


public override void InstantiateIn(Control container)
{
ImageButton imgDelete = new ImageButton();
imgDelete.ID = ID;
imgDelete.EnableViewState = false;
imgDelete.CommandArgument = CommandArg;
imgDelete.CommandName = "Remove";
//p.Click += p_Click;
imgDelete.ViewStateMode = ViewStateMode.Enabled;
imgDelete.ImageUrl = GenericGridConstants.CONTROL_REMOVEIMAGE;
imgDelete.OnClientClick = "return confirm('Are you sure you want to delete this record ?');";
//p.OnClientClick = "confirm ('Are you sure you want to delete this record ?');";
container.Controls.Add(imgDelete);

}
}

public class DropDownCustomItemTemplate : CustomItemTemplate
{
public DataTable DTdatasource { get; set; }
public string DdlSelectedValue { get; set; }
public string DdlDataValueField { get; set; }
public string DdlDataTextField { get; set; }

public DropDownCustomItemTemplate() { }

public DropDownCustomItemTemplate(string _Id, string _rowIndex, string _commandArg, DataTable _DTdatasource, string _DdlDataValueField, string _DdlDataTextField)
{
this.ID = _Id;
this.RowIndex = _rowIndex;
this.CommandArg = _commandArg;
this.DTdatasource = _DTdatasource;
this.DdlDataValueField = _DdlDataValueField;
this.DdlDataTextField = _DdlDataTextField;
}

public override void InstantiateIn(Control container)
{
DropDownList ddl = new DropDownList();
ddl.ID = ID;
ddl.ID = "imgEdit";
ddl.DataSource = DTdatasource;
ddl.DataTextField = DdlDataTextField;
ddl.DataValueField = DdlDataValueField;
ddl.DataBind();
ddl.SelectedValue = DdlSelectedValue;
ddl.EnableViewState = false;
ddl.ViewStateMode = ViewStateMode.Enabled;
container.Controls.Add(ddl);
}

}


-------- ------
public class Helper
{

public static void ParseXML(string formID, ref WebDataGrid WebDataGrid1)
{
//if (WebDataGrid1.HasColumns == false)
if (WebDataGrid1.Columns.Count < 3)
{
WebDataGrid1.AutoGenerateColumns = false;
XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(GenericGridConstants.XML_TEMPLATE_PATH);

#region Grid Level Settings

//Set Grid Height.
WebDataGrid1.Height = Unit.Parse(xmlDoc.DocumentElement.SelectSingleNode("//form[@ID='" + formID + "']/GridName").Attributes[GenericGridConstants.ATTRIBUTES_HEIGHT].Value.ToString().Trim());

//Set Grid Width.
WebDataGrid1.Width = Unit.Parse(xmlDoc.DocumentElement.SelectSingleNode("//form[@ID='" + formID + "']/GridName").Attributes[GenericGridConstants.ATTRIBUTES_WIDTH].Value.ToString().Trim());


//Check if Sorting is enabled or no.
if (Convert.ToBoolean(xmlDoc.DocumentElement.SelectSingleNode("//form[@ID='" + formID + "']/GridName").Attributes[GenericGridConstants.ATTRIBUTES_ALLOWSORTING].Value.ToString().Trim()) == true)
{
WebDataGrid1.Behaviors.CreateBehavior();
}


//Check if Export functionality is required or not.
//if (Convert.ToBoolean(xmlDoc.DocumentElement.SelectSingleNode("//form[@ID='" + formID + "']/GridName").Attributes[GenericGridConstants.ATTRIBUTES_EXPORT].Value.ToString().Trim()) == true)
// btnExportToExcel.Visible = true;
//else
// btnExportToExcel.Visible = false;


//check if paging is required or not.
if (Convert.ToBoolean(xmlDoc.DocumentElement.SelectSingleNode("//form[@ID='" + formID + "']/GridName").Attributes[GenericGridConstants.ATTRIBUTES_ALLOWPAGING].Value.ToString().Trim()) == true)
{
WebDataGrid1.Behaviors.CreateBehavior();
//Set the page size.
WebDataGrid1.Behaviors.Paging.PageSize = Convert.ToInt32(xmlDoc.DocumentElement.SelectSingleNode("//form[@ID='" + formID + "']/GridName").Attributes[GenericGridConstants.ATTRIBUTES_PAGESIZE].Value.ToString().Trim());
WebDataGrid1.Behaviors.Paging.PagerMode = Infragistics.Web.UI.GridControls.PagerMode.NumericFirstLast;
}


//Get the KeyDataField
//Priya to uncomment afterwards. This will be used while we are working on Editable Grids.
//string KeyDataField = xmlDoc.DocumentElement.SelectSingleNode("//form[@ID='" + formID + "']/GridName").Attributes[GenericGridConstants.ATTRIBUTES_KEYDATAFIELD].Value.ToString().Trim() != "NULL" ? xmlDoc.DocumentElement.SelectSingleNode("//form[@ID='" + formID + "']/GridName").Attributes["KeyDataField"].Value.ToString().Trim() : string.Empty;
//if (KeyDataField != "NULL")
//{
// this.WebDataGrid1.DataKeyFields = KeyDataField;
//}

#endregion Grid Level Settings

#region Column Settings

XmlNodeList ColList = xmlDoc.DocumentElement.SelectNodes("//form[@ID='" + formID + "']/GridName/Columns/Col");

foreach (XmlNode node in ColList)
{

switch (node.Attributes[GenericGridConstants.ATTRIBUTES_TYPEOFCOL].Value.ToString().ToUpper().Trim())
{

case GenericGridConstants.CONTROL_LABEL:
case GenericGridConstants.CONTROL_TEXTBOX:
Infragistics.Web.UI.GridControls.BoundDataField f = new Infragistics.Web.UI.GridControls.BoundDataField();
f.Key = node.Attributes[GenericGridConstants.ATTRIBUTES_KEY].Value;
f.Header.Text = node.InnerText;
WebDataGrid1.Columns.Add(f, true);
break;

case GenericGridConstants.CONTROL_CHECKBOX:
BoundCheckBoxField chk1 = new BoundCheckBoxField();
chk1.Key = node.Attributes[GenericGridConstants.ATTRIBUTES_KEY].Value;
chk1.Header.Text = node.InnerText;
chk1.CheckBox.CheckedImageUrl = "..\\ig_res\\Default\\images\\ig_checkbox_on.gif";
chk1.CheckBox.UncheckedImageUrl = "..\\ig_res\\Default\\images\\ig_checkbox_off.gif";
chk1.CheckBox.PartialImageUrl = "..\\ig_res\\Default\\images\\ig_checkbox_partial.gif";
WebDataGrid1.Columns.Add(chk1);
break;

case GenericGridConstants.CONTROL_RADIOBUTTON:
//TemplateDataField tdl = new TemplateDataField();
//tdl.Key = node.Attributes["Key"].Value;
//tdl.Header.Text = node.InnerText;
//RadioButton radbtn = new RadioButton();
//radbtn.GroupName = node.Attributes["Key"].Value;
//tdl.ItemTemplate = ITemplatedGridObject obj;
//obj.TemplateContainer.Controls.Add(radbtn);
break;

case GenericGridConstants.CONTROL_IMAGE:
case GenericGridConstants.CONTROL_LINKBUTTON:
UnboundField img = new UnboundField();
img.Key = node.Attributes[GenericGridConstants.ATTRIBUTES_KEY].Value;
img.Header.Text = node.InnerText;
img.HtmlEncode = false;
WebDataGrid1.Columns.Add(img, true);
break;
}//Switch Case ends

#region Common Column Properties

//check if the column is hidden.
if (node.Attributes[GenericGridConstants.ATTRIBUTES_HIDDEN].Value.ToString().Trim() == "true")
{
WebDataGrid1.Columns[node.Attributes[GenericGridConstants.ATTRIBUTES_KEY].Value].Hidden = true;
}

//Set width to Columns.
WebDataGrid1.Columns[node.Attributes[GenericGridConstants.ATTRIBUTES_KEY].Value].Width = Unit.Parse(node.Attributes[GenericGridConstants.ATTRIBUTES_COLWIDTH].Value.ToString());

//Add Editing behavior to the Grid by default. Then depending on the isEditable flag from XML Template set/reset the editing behavior
WebDataGrid1.Behaviors.CreateBehavior();
WebDataGrid1.Behaviors.EditingCore.Behaviors.CreateBehavior();
// Create column settings
EditingColumnSetting settingColumn1 = new EditingColumnSetting();
settingColumn1.ColumnKey = node.Attributes[GenericGridConstants.ATTRIBUTES_KEY].Value;

//Check if column is Editable. If yes enable the editing behavior.
if (Convert.ToBoolean(node.Attributes[GenericGridConstants.ATTRIBUTES_ISEDITABLE].Value.ToString().Trim()) == true)
{
settingColumn1.ReadOnly = false;
WebDataGrid1.Behaviors.EditingCore.Behaviors.CellEditing.ColumnSettings.Add(settingColumn1);
}
else
{
settingColumn1.ReadOnly = true;
WebDataGrid1.Behaviors.EditingCore.Behaviors.CellEditing.ColumnSettings.Add(settingColumn1);
}

#endregion Common Column Properties

}//Foreach loop ends

#endregion Column Settings
}
}

}

Thansk,

Santosh.

  • 25665
    Offline posted

    Hello Santosh,

    Thank you for contacting Infragistics!

    The reason you are getting the error is when you are looking for the control by ID/Key you are calling the method that gets the cell. So your code should look like this instead:

    Control = row.Items.FindItemByKey(“ColumnKey”).FindControl(“ControlID”);