How to access dynmaically added Textbox values ASP.Net - asp.net

I have a gridview placed on a ASP.Net WebSite, and added a column dynamically like this:
protected void gvGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (DataControlRowType.DataRow == e.Row.RowType && e.Row.RowState != DataControlRowState.Edit &&
(e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
{
TextBox tb = new TextBox();
tb.ID = "tb";
tb.Attributes.Add("runat", "server");
int i = e.Row.Cells.Count;
i = i - 1;
e.Row.Cells[i].Controls.Add(tb);
}
}
The gridview is populated, and at the end of every row the TextBox is added. When I look at the HTML Code that is delived to the Browser, I can see that the ID of every Textbox is "gv_Items_tb_X" with X being the current row index.
Now I would like to access the content that the user types in the tb on a button click. I tried following code, but it I get an exception becuase the textbox is null.
protected void btn_UpdateCount_Click(object sender, EventArgs e)
{
OI_Data_Service.OI_Data_ServiceClient sc = new OI_Data_Service.OI_Data_ServiceClient();
foreach (GridViewRow row in gv_Items.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
TextBox tb_value = (TextBox)gv_Items.Rows[row.RowIndex].Cells[5].FindControl("gv_Items_tb_" + row.RowIndex);
sc.UpdateItemOnOpening("1", row.Cells[0].Text, tb_value.Text);
}
}
Response.Redirect("~/okay.aspx");
}
Can anyone tell my what I am doing wrong?
Thanks!

Related

Dropdown Save in a gridview

I have a GRID that consist of columns that are populated and recently I have added some drop down columns as well to the grid. My drop down is populating with the options from the backend, but now I need to save those answers to the page itself, so the user can see it on the screen next time they arrive on the page.
GRID
protected void gvViolationsCited_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
{
TableCell idCell = GridViewFunctions.FindCellByDataField(e.Row, "ID");
idCell.Visible = false;
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton lb = (ImageButton)e.Row.FindControl("lbShowModalDialog");
long id = long.Parse(idCell.Text);
DropDownList hpvcriteria = (DropDownList)e.Row.FindControl("hpvcriteria");
hpvcriteria.DataSource = LookupDataLoaderService.Instance.LoadLookupData(LookupTables.HPV_VIOLATION_CODE);
hpvcriteria.DataBind();
}
}
}
private void PopulateViolationGrid(long caseID)
{
IList<EnforcementViolation> enforcementViolations = LovService.Instance.GetEnforcementLovList(caseID);
IEnumerator<EnforcementViolation> evEnumerator = enforcementViolations.GetEnumerator();
while (evEnumerator.MoveNext())
{
EnforcementViolation ev = evEnumerator.Current;
//txtOtherDocsViolated.Text += ev.Permit + " ";
}
gvViolationsCited.DataSource = GridViewFunctions.GenericListToDataTable<EnforcementViolation>(enforcementViolations);
gvViolationsCited.DataBind();
upnlViolationsCited.Update();
evEnumerator.Dispose();
}

Retrieving a Dynamically Generated TextBox Content in a GridView in ASP.NET

I have the following RowDataBound method for GridView2
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
List<TextBox> list = new List<TextBox>();
if (ViewState["Table"] != null)
Assessments = (DataTable)ViewState["Table"];
int count = 1;
foreach (DataRow row in Assessments.Rows)
{
TextBox txt = new TextBox();
txt.ID = "AsTxt";
txt.Text = string.Empty;
txt.TextChanged += OnTextChanged;
e.Row.Cells[count].Controls.Add(txt);
count += 2;
listd.Add((e.Row.DataItem as DataRowView).Row[0].ToString() + "Txt");
}
}
}
And the following event (Button Click) to retrieve whatever written in the text box in the GridView
protected void CalculateBtn_Click(object sender, EventArgs e)
{
GridViewRow rr = GridView2.Rows[0];
TextBox rrrr = (rr.FindControl("AsTxt") as TextBox);
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + rrrr.Text + "')", true);
}
I always get NullReferenceException. That mean the TextBox object (rrrr) is null always. I am sure that the text object sits in GridView2.Rows[0].
Why is this happening?
This is known issue with the Dynamical created controls in asp. So if you want to use the created control on your postback then I suggest that you declare your controls outside the page_int and do your initialization in the init then use them with their name instead of find control.
Look at this blog this might help you
http://techbrij.com/retrieve-value-of-dynamic-controls-in-asp-net

how can get column value in row edit event of gridview

This is my editing code in rowdatabound event.
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList dl = (DropDownList)e.Row.FindControl("Sectionname");
comp.MEDIUM = Convert.ToString(e.Row.FindControl("Medium"));
comp.CLASSNAME = Convert.ToString(e.Row.FindControl("ClassName"));
comp.ACADAMICYEAR = Convert.ToString(e.Row.FindControl("AcademicYear"));
DataTable worktype = inter.bindsectionforgird(comp);
dl.DataSource = worktype;
dl.DataTextField = "SectionName";
dl.DataValueField = "SectionId";
dl.DataBind();
}
}
still I am not able to get the value of those fields.
I suggest you to write your code in RowDataBound event
void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Write your code here
}
}
And you can find your Grid's columns as
e.Row.FindControl("yourControlID")
You can see detail in
MSDN reference 1
MSDN reference 2
CodeProject

how to retain viewstate for the dynamic controls created in GRIDVIEW

i am creating dynamic textbox onRowCreated event in gridview control, however when i try to findcontrol i get null
here is how i am dong...
protected void gvORg_RowCreated(object sender, GridViewRowEventArgs e)
{
if ((e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate)) || (e.Row.RowState == DataControlRowState.Edit))
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
txBox txtReg = new TextBox();
txtReg.ID = "_registration" + e.Row.RowIndex + rowId.ToString();
txtReg.Text = reg.RegistrationToken;
e.Row.Cells[7].Controls.Add(txtReg);
}
}
}
protected void gvOrg_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
.....
....
TextBox _registration1 = gvOrg.Rows[e.RowIndex].Cells[7].FindControl("_registration" + e.RowIndex + rowId) as TextBox;
}
Have you tried to find it by:
GridView gv = (GridView)sender;
GridViewRow gvr = (GridViewRow)gv.Rows[e.RowIndex];
(TextBox)gvr.FindControl("_registration" + e.Row.RowIndex + "_" + reg.RegistrationId.ToString())
i able to fix my problem here

How to add a row in asp.net grid view

So far I have done this, I am not sure whether this is right or wrong
public partial class _Default : System.Web.UI.Page
{
Label l = new Label();
GridView gv = new GridView();
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 5; i++)
{
GridViewRow gvr = new GridViewRow(i, i, DataControlRowType.DataRow, DataControlRowState.Normal);
gvr.Controls.Add(l);
gv. (what to do here)
}
this.Controls.Add(gv);
}
}
please help
gv.Rows.Add(gvr);
If you're starting with an empty GridView, an easier way to dynamically create x rows is to create a dummy list and then set it to the data source:
var list = new List<string>(10); // replace 10 with number of empty rows you want
// for loop to add X items to the list
gv.DataSource = list;
gv.DataBind();
If you are doing this, I'd recommend doing it with a Repeater. It's a lot easier to manage.
The DataGrid fires the RowCreate event when a new row is created.
Collapse
//OnRowCreated="GridView3_RowCreated"
protected void GridView3_RowCreated(object sender, GridViewRowEventArgs e)
{
//When a child checkbox is unchecked then the header checkbox will also be unchecked.
if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState ==
DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
{
CheckBox chkBxSelect = (CheckBox)e.Row.Cells[1].FindControl("chkselect");
CheckBox chkBxHeader = (CheckBox)this.GridView3.HeaderRow.FindControl("chkHeader");
chkBxSelect.Attributes["onclick"] = string.Format("javascript:ChildClick(
this,'{0}');", chkBxHeader.ClientID);
}
}

Resources