views:

76

answers:

0

Good day all,

I am having problems trying to use FindControl with my gridview. I am generating the gridview columns using the ITemplate function. This generates a bunch of textbox fields for me that the user has to input some data.

The number of columns vary depending on user requirements, so I have to generate the grid dynamically.

I have a submit button at the bottom of the page which I want to use to iterate through the user fields and write this back to the database.

The generation of the page etc works fine, the problem happens when I press the submit button. I am not able to find the text box controls that I generated and I need to access these to get the user input code.

Here is the code for the submit button:

protected void btnSubmitLMF_Click(object sender, EventArgs e)
{
    try
    {
        string str = string.Format("{0:c}", txtOrderResidual.Text);
        double residual = double.Parse(str, System.Globalization.NumberStyles.Any);

        if (residual < 0)
        {
            // Save and redirect
            EHL.BusFunc.ProjectDashBoardCollection pdc = new EHL.BusFunc.ProjectDashBoardCollection();

            for (int i = gvLMF.Columns.Count - 3 - Convert.ToInt32(txtDuration.Text); i < gvLMF.Columns.Count - 3; i++)
            {
                ContentPlaceHolder cph = (ContentPlaceHolder)Page.Master.Controls[0].FindControl("mainContent");
                UpdatePanel up = (UpdatePanel)cph.FindControl("udpgvLMF");
                GridView gv = (GridView)up.FindControl("gvLMF");
                TextBox rubbish = (TextBox)gv.Rows[1].Cells[29].FindControl("txtBox");

                foreach (GridViewRow row in gv.Rows)
                {
                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        TextBox tb = (TextBox)row.Cells[i].FindControl("ctl02");                           
                    }
                }
                for (int k = 0; k < this.gvLMF.Rows.Count; k++)
                {
                    string temp = this.gvLMF.Rows[k].Cells[i].ToString();                        
                }

                foreach (GridViewRow gvr in gvLMF.Rows)
                {
                    if (gvr.RowType == DataControlRowType.DataRow)
                    {
                        TableCellCollection tcc = gvr.Cells;
                        if (tcc[i].Text != null)
                        {
                            string test = tcc[i].Text;
                        }
                        TextBox tb = (TextBox)gvr.Cells[i].FindControl("txtBox");
                    }
                }
            }

            if (ddlJobNo.Enabled)
            {
                Response.Redirect("LMFReasons.aspx?JobID=" + Convert.ToInt32(ddlJobNo.SelectedValue) + "&Residual=" + residual);
            }
            else
            {
                Response.Redirect("LMFReasons.aspx?JobID=" + Convert.ToInt32(Request.Params["JobID"].ToString()) + "&Residual=" + residual);
            }
        }
        else
        {
            // Save and redirect

            Response.Redirect("PDBIntro.aspx");
        }

    }
    catch (Exception ex)
    {
        lblError.Visible = true;
        lblError.Text = "Message: " + ex.Message.ToString() + "<br />" + "Stack: " + ex.StackTrace.ToString();
    }
}

And here is the code for the ITemplate:

public class TextTemplate : ITemplate

{ private string colname; private ITemplate template;

public TextTemplate(string colname)
{       
    this.colname = colname;        
}

[PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(typeof(TextTemplate))]
public ITemplate item
{
    get { return template; }
    set { template = value; }
}


public void InstantiateIn(Control container)
{
    TemplateField tf = new TemplateField();
    PlaceHolder ph = new PlaceHolder();
    TextBox tb = new TextBox();
    tb.ID = "txtBox";
    tb.Enabled = true;
    tb.ReadOnly = false;
    tb.DataBinding += new EventHandler(this.OnDataBinding);
    container.Controls.Add(tb);
}

public void OnDataBinding(object sender, EventArgs e)
{        
    TextBox tb = (TextBox)sender;
    GridViewRow container = (GridViewRow)tb.NamingContainer;
    object dataValue = DataBinder.Eval(container.DataItem, colname);
    tb.Text = ((DataRowView)container.DataItem)[colname].ToString();
    TableCell tc = (TableCell)tb.Parent;
    int x = container.RowIndex;
    int y = container.Cells.GetCellIndex(tc);
    tb.Attributes.Add("onfocus", "this.oldvalue = this.value; this.id");
    tb.Attributes.Add("onchange", "LMFUpdater(this, " + (x + 1) + ", " + y + ");");
    tb.Attributes.Add("runat", "Server");

    tb.Width = System.Web.UI.WebControls.Unit.Percentage(100);
    tb.Height = System.Web.UI.WebControls.Unit.Percentage(100);
    tb.Font.Name = "Arial Narrow";
    tb.Font.Size = 8;
}

}

I call the procedure to generate the gridview in the PageLoad() if(!Page.IsPostback). I have tried to move it, but then when I press the submit button it throws an exception about multiple controls found with the same id.

Any help with this is appreciated as I am about ready to throw the computer out of the window.

Regards,

Craig