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
Expand|Select|Wrap|Line Numbers
- public void BindTemplate()
- {
- SkillSetBL skillsetBL = new SkillSetBL();
- List<SkillSet> list = new List<SkillSet>();
- list = skillsetBL.GetSkillType();
- SkillSet[] empArray = list.ToArray();
- foreach (SkillSet item in empArray)
- {
- string a = item.ID.ToString();
- TemplateField templateField = new TemplateField();
- templateField.HeaderText = item.SkillName.ToString();
- gvwData.Columns.Add(templateField);
- templateField.FooterTemplate = new TextboxTemplate();
- templateField.ItemTemplate = new LabelTemplate();
- }
- }
- private class TextboxTemplate : ITemplate
- {
- public void InstantiateIn(System.Web.UI.Control container)
- {
- TextBox tbx = new TextBox();
- tbx.Width = 12;
- tbx.DataBind();
- container.Controls.Add(tbx);
- }
- }
- private class LabelTemplate : ITemplate
- {
- public void InstantiateIn(System.Web.UI.Control container)
- {
- Label lbl = new Label();
- lbl.DataBind();
- container.Controls.Add(lbl);
- }
- }
Expand|Select|Wrap|Line Numbers
- protected void gvwData_RowCommand(object sender, GridViewCommandEventArgs e)
- {
- GridViewRow row = gvwData.FooterRow;
- if (e.CommandName == "Insert")
- {
- SkillSetDatabase entity = new SkillSetDatabase();
- SkillSetDatabaseBL skillSetDatabaseBL = new SkillSetDatabaseBL();
- try
- {
- if (((DropDownList)row.FindControl("ddlstEmployeeID")).SelectedValue == "")
- {
- _masterPage.GetNotifyLabel().Text = "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.RatingValue = masterPage.BindUIControlsToObject(entity, gvwData.FooterRow.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
- skillSetDatabaseBL.Insert(entity);
- _masterPage.GetConfirmLabel().Text = "New record added.";
- gvwData.DataBind();
- }
- }
- catch (Exception ex)
- {
- throw new Exception(ex.Message);
- }
- }
- }