Click here to Skip to main content
15,895,785 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone

Actually I Want to add TextBox to gridview.

I did this

C#
protected void Page_Load(object sender,  EventArgs e)
{
 GridView1.DataSource = Db.FillDataGrid("Select colName1, 1+1 as translate, .... ", " TableName ", GridView1);
 GridView1.DataBind();
 InitControls();
}

private void InitControls()
    {
        try
        {
            for (int j = 0; j < 1; j++)
            {
                for (int i = 2; i < 30; i += 2)
                {
                     TextBox textbox = new TextBox();
                    textbox.ID = "text" + (j+1);
                    textbox.Width = 200;
                    GridView1.Rows[j].Cells[i].Controls.Add(textbox);
                }
            }
        }
        catch (Exception e)
        {
           // ScriptManager.RegisterStartupScript(this.UpdatePanel1, UpdatePanel1.GetType(), "scriptname", "alert('"+e.Message+"')", true);
        }
        
    }



when I get TextBox

C#
protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox text = ((TextBox) GridView1.Rows[0].Cells[2].FindControl("text1"));

        string f = text.Text;
        return;
    }


give me Error Message TextBox

HTML
[NullReferenceException: Object reference not set to an instance of an object.]
   _Default.Button1_Click(Object sender, EventArgs e) in c:\Users\Aghadeer\Desktop\(Ammars' Computer)\Collection\CSS Template\EditableWesbsite\Default.aspx.cs:132
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9553594
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724


if there is another way can you tell me

sorry for English
Posted
Updated 4-Mar-15 6:50am
v3

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900