Hi
On this line i am getting above error - txtSessionDate.Enabled = false;
C#
protected void gr_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType != DataControlRowType.DataRow)
return;
e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Center;
TextBox txtSessionDate = new TextBox();
txtSessionDate.Text = "";
txtSessionDate.CssClass = "form-control daterange-single";
e.Row.Cells[10].Controls.Add(txtSessionDate);
e.Row.Cells[10].HorizontalAlign = HorizontalAlign.Center;
txtSessionDate = (TextBox)e.Row.FindControl("txtSessionDate");
string Remark = DataBinder.Eval(e.Row.DataItem, "Remarks").ToString();
if (Remark == "Done" || Remark == "Scheduled")
{
txtSessionDate.Enabled = false;
}
}
}
------------------
SQLUtility sQLUtility = new SQLUtility();
DataTable dt = sQLUtility.GeBookPlanningRecommendation(HRpl, BookCollection, StudentCollection).Tables[0];
if (dt != null)
{
DataColumn Remarks = new DataColumn("Remarks", typeof(string));
dt.Columns.Add(Remarks);
DataColumn Dates = new DataColumn("SessionDt", typeof(string));
dt.Columns.Add(Dates);
}Thanks

Amit MohantyRank #112 · 20K reputationPosted May 8, 2023, 1:53 AM EDT
TextBox txtSessionDate = new TextBox(); txtSessionDate.Text = ""; txtSessionDate.CssClass = "form-control daterange-single"; e.Row.Cells[10].Controls.Add(txtSessionDate);The above code creates a new
TextBoxcontrol, sets its properties, and adds it to theGridViewrow. But, when you try to access thisTextBoxcontrol later in the code using theFindControlmethod, it returnsnull. Because theFindControlmethod only searches for controls that were added to the page declaratively, i.e., controls that were defined in the .aspx markup. But you are adding theTextBoxcontrol dynamically in code-behind, it is not part of the page's control hierarchy, andFindControlcannot find it.Amit MohantyRank #112 · 20K reputationPosted May 8, 2023, 1:38 AM EDT
You can check if
txtSessionDateis not null before attempting to set itsEnabledproperty.if (Remark == "Done" || Remark == "Scheduled") { if (txtSessionDate != null) { txtSessionDate.Enabled = false; } }Ramco RamcoRank #462 · 5.4K reputationPosted May 8, 2023, 1:41 AM EDT
Hi AMit
If i am adding Textbox Control then why it is showing NULL.
Thanks
Ramco RamcoRank #462 · 5.4K reputationPosted May 8, 2023, 2:20 AM EDT
Hi AMit
o.k But how in below code i will get SessionDate value in a variable
protected void lnkAdd_Click(object sender, EventArgs e) { try { foreach (GridViewRow gr in grdPlanning.Rows) { if (gr.RowType == DataControlRowType.DataRow) { } } } }Thanks
Ramco RamcoRank #462 · 5.4K reputationPosted May 8, 2023, 2:28 AM EDT
Hi AMit
When i write this line i get below error - e.Row.DataItem = txtSessionDate;
DataBinding System.Web.UI.WebControls.TextBox does not contain a property with the name Remarks.
Thanks
Amit MohantyRank #112 · 20K reputationPosted May 8, 2023, 3:16 AM EDT
Try this
protected void gr_RowDataBound(object sender, GridViewRowEventArgs e) { try { if (e.Row.RowType != DataControlRowType.DataRow) return; e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Center; TextBox txtSessionDate = new TextBox(); txtSessionDate.ID = "txtSessionDate"; txtSessionDate.Text = ""; txtSessionDate.CssClass = "form-control daterange-single"; e.Row.Cells[10].Controls.Add(txtSessionDate); e.Row.Cells[10].HorizontalAlign = HorizontalAlign.Center; txtSessionDate = (TextBox)e.Row.FindControl("txtSessionDate"); string Remark = DataBinder.Eval(e.Row.DataItem, "Remarks").ToString(); if (Remark == "Done" || Remark == "Scheduled") { if (txtSessionDate != null) { txtSessionDate.Enabled = false; } } } catch { } } protected void linkAdd_Click(object sender, EventArgs e) { try { foreach (GridViewRow row in gr.Rows) { if (row.RowType == DataControlRowType.DataRow) { TextBox txtDynamic = row.FindControl("txtSessionDate") as TextBox; if (txtDynamic != null) { string dynamicValue = txtDynamic.Text; // Do something with the dynamicValue } } } } catch { } }Ramco RamcoRank #462 · 5.4K reputationPosted May 8, 2023, 3:40 AM EDT
Hi Amit
I have below code . Even if enter Data in one of the rows it is not retreiving. Add functionality is not able to retreive TextBox value.
protected void gr_RowDataBound(object sender, GridViewRowEventArgs e) { try { if (e.Row.RowType != DataControlRowType.DataRow) return; e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Center; TextBox txtSessionDate = new TextBox(); txtSessionDate.Text = ""; txtSessionDate.CssClass = "form-control daterange-single"; e.Row.Cells[10].Controls.Add(txtSessionDate); e.Row.Cells[10].HorizontalAlign = HorizontalAlign.Center; txtSessionDate = (TextBox)e.Row.FindControl("txtSessionDate"); } }protected void lnkAdd_Click(object sender, EventArgs e) { try { string sessionDate = ""; Boolean success = false; BALGroup objGroup = new BALGroup(); Group Result = objGroup.GetGroupDetail(Convert.ToInt32(hdfGroupId.Value)); foreach (GridViewRow gr in grdPlanning.Rows) { if (gr.RowType == DataControlRowType.DataRow) { TextBox txtDynamic = gr.FindControl("txtSessionDate") as TextBox; if (txtDynamic != null) { sessionDate = txtDynamic.Text; } } } } }SQLUtility sQLUtility = new SQLUtility(); DataTable dt = sQLUtility.GeBookPlanningRecommendation(HRpl, BookCollection, StudentCollection).Tables[0]; if (dt != null) { DataColumn Remarks = new DataColumn("Remarks", typeof(string)); dt.Columns.Add(Remarks); DataColumn Dates = new DataColumn("SessionDate", typeof(string)); dt.Columns.Add(Dates); }Thanks
Ramco RamcoRank #462 · 5.4K reputationPosted May 8, 2023, 4:03 AM EDT
Hi Amit
In Add I have written below code but it is returning Null . I have entered date in this row
if(gr.Cells[0].Text == "70") { TextBox txtDynamic = gr.FindControl("txtSessionDate") as TextBox; if (txtDynamic != null) { sessionDate = txtDynamic.Text; } }Thanks