Hi All,
I really need help to solve my problem.
My case is on looping through textbox in gridview.
I had a gridview placed in my page. The gridview contains template field
which I created it during runtime. I also had a generic list to fetch few items. Number of item that i got from the list will be the number of template that being created during runtime(eg : if the list return 3 items, meaning there will be 3 template field created).
The problem arise when I place each template field with tbox control. Im not able to loop through template and find the tbox control inside it as it(tbox) doesnt have ID yet. I cannot name the tbox ID as each tbox would have multiple ID name soon. Below I had attached my code for u to take a look.Hope anyone can give solution.
I called the BindTemplate method under page load
[code=cpp]
public void BindTemplate()
{
SkillSetBL skillsetBL = new SkillSetBL();
List<SkillSet> list = new List<SkillSet>( );
list = skillsetBL.GetS killType();
SkillSet[] empArray = list.ToArray();
foreach (SkillSet item in empArray)
{
string a = item.ID.ToStrin g();
TemplateField templateField = new TemplateField() ;
templateField.H eaderText = item.SkillName. ToString();
gvwData.Columns .Add(templateFi eld);
templateField.F ooterTemplate = new TextboxTemplate ();
templateField.I temTemplate = new LabelTemplate() ;
}
}
private class TextboxTemplate : ITemplate
{
public void InstantiateIn(S ystem.Web.UI.Co ntrol container)
{
TextBox tbx = new TextBox();
tbx.Width = 12;
tbx.DataBind();
container.Contr ols.Add(tbx);
}
}
private class LabelTemplate : ITemplate
{
public void InstantiateIn(S ystem.Web.UI.Co ntrol container)
{
Label lbl = new Label();
lbl.DataBind();
container.Contr ols.Add(lbl);
}
}
[/code]
my problem is at this stage
[code=cpp]
protected void gvwData_RowComm and(object sender, GridViewCommand EventArgs e)
{
GridViewRow row = gvwData.FooterR ow;
if (e.CommandName == "Insert")
{
SkillSetDatabas e entity = new SkillSetDatabas e();
SkillSetDatabas eBL skillSetDatabas eBL = new SkillSetDatabas eBL();
try
{
if (((DropDownList )row.FindContro l("ddlstEmploye eID")).Selected Value == "")
{
_masterPage.Get NotifyLabel().T ext = "Employee name is not selected. Please provide related information.";
}
else
{
entity.Creator = Convert.ToInt16 (Session["Creator"].ToString());
// this is where is should get the texbox value
entity.RatingVa lue = masterPage.Bind UIControlsToObj ect(entity, gvwData.FooterR ow.Controls);
//before insert new record, i have to iterate the template field to find textbox, and then during the iteration only the ID of tbox be given
skillSetDatabas eBL.Insert(enti ty);
_masterPage.Get ConfirmLabel(). Text = "New record added.";
gvwData.DataBin d();
}
}
catch (Exception ex)
{
throw new Exception(ex.Me ssage);
}
}
}[/code]
I really need help to solve my problem.
My case is on looping through textbox in gridview.
I had a gridview placed in my page. The gridview contains template field
which I created it during runtime. I also had a generic list to fetch few items. Number of item that i got from the list will be the number of template that being created during runtime(eg : if the list return 3 items, meaning there will be 3 template field created).
The problem arise when I place each template field with tbox control. Im not able to loop through template and find the tbox control inside it as it(tbox) doesnt have ID yet. I cannot name the tbox ID as each tbox would have multiple ID name soon. Below I had attached my code for u to take a look.Hope anyone can give solution.
I called the BindTemplate method under page load
[code=cpp]
public void BindTemplate()
{
SkillSetBL skillsetBL = new SkillSetBL();
List<SkillSet> list = new List<SkillSet>( );
list = skillsetBL.GetS killType();
SkillSet[] empArray = list.ToArray();
foreach (SkillSet item in empArray)
{
string a = item.ID.ToStrin g();
TemplateField templateField = new TemplateField() ;
templateField.H eaderText = item.SkillName. ToString();
gvwData.Columns .Add(templateFi eld);
templateField.F ooterTemplate = new TextboxTemplate ();
templateField.I temTemplate = new LabelTemplate() ;
}
}
private class TextboxTemplate : ITemplate
{
public void InstantiateIn(S ystem.Web.UI.Co ntrol container)
{
TextBox tbx = new TextBox();
tbx.Width = 12;
tbx.DataBind();
container.Contr ols.Add(tbx);
}
}
private class LabelTemplate : ITemplate
{
public void InstantiateIn(S ystem.Web.UI.Co ntrol container)
{
Label lbl = new Label();
lbl.DataBind();
container.Contr ols.Add(lbl);
}
}
[/code]
my problem is at this stage
[code=cpp]
protected void gvwData_RowComm and(object sender, GridViewCommand EventArgs e)
{
GridViewRow row = gvwData.FooterR ow;
if (e.CommandName == "Insert")
{
SkillSetDatabas e entity = new SkillSetDatabas e();
SkillSetDatabas eBL skillSetDatabas eBL = new SkillSetDatabas eBL();
try
{
if (((DropDownList )row.FindContro l("ddlstEmploye eID")).Selected Value == "")
{
_masterPage.Get NotifyLabel().T ext = "Employee name is not selected. Please provide related information.";
}
else
{
entity.Creator = Convert.ToInt16 (Session["Creator"].ToString());
// this is where is should get the texbox value
entity.RatingVa lue = masterPage.Bind UIControlsToObj ect(entity, gvwData.FooterR ow.Controls);
//before insert new record, i have to iterate the template field to find textbox, and then during the iteration only the ID of tbox be given
skillSetDatabas eBL.Insert(enti ty);
_masterPage.Get ConfirmLabel(). Text = "New record added.";
gvwData.DataBin d();
}
}
catch (Exception ex)
{
throw new Exception(ex.Me ssage);
}
}
}[/code]
Comment