Gathering Data: Dynamic Text Boxes - asp.net

Edit: if someone could also suggest a more sensible way to make what I'm trying below to happen, that would also be very appreciated
I'm building an multiPage form that takes a quantity (of product) from a POST method, and displays a form sequence relying on that number. when the user goes to the next page, the form is supposed to collect this information and display it (for confirmation), which will then send this info to a service that will supply URL's to display.
Needless to say, I'm having problems making this work. Here is the relevant parts of my (anonymised) code:
public partial class foo : System.Web.UI.Page
{
Int quantityParam = 3;
ArrayList Users = new ArrayList();
//the information for each user is held in a hashtable the array list will be an array list of the user hashtables
protected void Page_Init(object sender, EventArgs e)
{
if(null != Request["quantity1"])
{
this.quantityParam = Request["quantity1"];
}
}
protected void Page_Load(object sender, EventArgs e)
{
int quantity = this.quantityParam;
if(quantity < 1){ mviewThankYou.SetActiveView(View4Error);}
else
{ //create a form for each user
mviewThankYou.SetActiveView(View1EnterUsers);
for(int user = 0;user < quantity; user++)
{
createUserForm(user);
}
}
}
protected void BtnNext1_Click(object sender, EventArgs e)
{
if(Page.IsValid)
{
for(int i = 0; i < quantity; i++)
{
String ctrlName = "txtUser" + i.ToString();
String ctrlEmail = "txtEmail" + i.ToString();
TextBox name = (TextBox)FindControl(ctrlName);
TextBox email = (TextBox)FindControl(ctrlEmail);
/*BONUS QUESTION: How can I add the Hashtables to the Users Array without them being destroyed when I leave the function scope?
this is where the failure occurs:
System.NullReferenceException: Object reference not set to an instance of an object. on: "tempUser.Add("name",name.Text);
*/
Hashtable tempUser = new Hashtable();
tempUser.Add("name",name.Text);
tempUser.Add("email",email.Text);
this.Users.Add(tempUser);
}
for(int i = 0; i < quantity; i++)
{
v2Content.Text +="<table><tr><td>Name: </td><td>"+
((Hashtable)Users[i])["name"]+
"</td></tr><tr><td>Email:</td><td>"+
((Hashtable)Users[i])["email"]+
"</td></tr></table>";
}
mviewThankYou.SetActiveView(View2Confirm);
}
}
private void createUserForm(int userNum){
DataTable objDT = new DataTable();
int rows = 2;
int cols = 2;
//create the title row..
TableRow title = new TableRow();
TableCell titleCell = new TableCell();
formTable.Rows.Add(title);
Label lblUser = new Label();
lblUser.Text = "<b>User "+ (userNum+1) + "</b>";
lblUser.ID = "lblTitle"+ userNum;
titleCell.Controls.Add(lblUser);
title.Cells.Add(titleCell);
for(int i = 0; i < rows; i++)
{
TableRow tRow = new TableRow();
formTable.Rows.Add(tRow);
for(int j = 0; j < cols; j++)
{
TableCell tCell = new TableCell();
if(j == 0){
Label lblTitle = new Label();
if(i == 0){
lblTitle.Text = "User Name:";
lblTitle.ID = "lblUser" + userNum;
}
else{
lblTitle.Text = "User Email:";
lblTitle.ID = "lblEmail" + userNum;
}
tCell.Controls.Add(lblTitle);
} else {
TextBox txt = new TextBox();
if(i==0){
txt.ID = "txtUser" + userNum;
}
else{
txt.ID = "txtEmail" + userNum;
}
RequiredFieldValidator val = new RequiredFieldValidator();
val.ID = txt.ID + "Validator";
val.ControlToValidate = txt.UniqueID;
val.ErrorMessage = "(required)";
tCell.Controls.Add(txt);
tCell.Controls.Add(val);
}
tRow.Cells.Add(tCell);
}//for(j)
}//for(i)
//create a blank row...
TableRow blank = new TableRow();
TableCell blankCell = new TableCell();
formTable.Rows.Add(blank);
Label blankLabel = new Label();
blankLabel.Text = " ";
blankLabel.ID = "blank" + userNum;
blankCell.Controls.Add(blankLabel);
blank.Cells.Add(blankCell);
}//CreateUserForm(int)
Sorry for the gnarly amount of (amateur code). What I suspect if failing is that FindControl() is not working, but I can't figure out why...
if any help can be given, I'd be very greatful.
Edit: showing the error might help:
Error (Line 112)
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 111: Hashtable tempUser = new Hashtable();
Line 112: tempUser.Add("name",name.Text);
Line 113: tempUser.Add("email",email.Text);
Line 114: this.Users.Add(tempUser);

You problem comes in the fact you are reloading the form every time in Page_Load. Make sure you only load the dynamic text boxes once and you will be able to find them when you need them for confirmation. As long as Page_Load rebuilds, you will not find the answer, and risk not finding anything.

I figured it out:
FindControl() works as a direct search of the children of the control it's called on.
when I was calling it, it was (automatically) Page.FindControl() I had nested the table creation inside a field and a Table control
when I called tableID.FindControl() it found the controls just as it should.
Thanks for the help, Gregory, and for all the comments everyone.
-Matt

Related

how to get value from dynamically created textbox in asp.net

hi i'm Cannot get value dynamically created textbox and save into database. plz help
his is code which I have created in this code text box are created but when I will input the value in the text and retrieve the value from dynamically created text box it give error
protected void btnAtt_Click(object sender, EventArgs e)
{
int DPLID = int.Parse(DPLCategory.Text);
var query = (from p in database.tbl_Attributes
where p.ProductTypeId_FK == DPLID
select new
{
p.Attribute_Id,
p.AttributeName,
p.ProductTypeId_FK,
}).ToArray();
for (int i = 0; i < query.Count(); i++)
{
Label lblatt = new Label();
lblatt.ID = query[i].AttributeName;
lblatt.Text = query[i].AttributeName + " : ";
lblatt.CssClass = "control-label";
TextBox txtatt = new TextBox();
txtatt.ID = "txtatt"+i;
txtatt.Attributes.Add("runat", "server");
txtatt.Text = String.Empty;
txtatt.CssClass = "form-control input-sm";
HtmlTextWriterTag.Br.ToString();
Place1.Controls.Add(lblatt);
HtmlTextWriterTag.Br.ToString();
Place1.Controls.Add(txtatt);
HtmlTextWriterTag.Br.ToString();
}
}
protected void lbtnSave_Click(object sender, EventArgs e)
{
int DPLID = int.Parse(DPLCategory.Text);
var query = (from p in database.tbl_Attributes
where p.ProductTypeId_FK == DPLID
select new
{
p.Attribute_Id,
p.AttributeName,
p.ProductTypeId_FK,
}).ToArray();
int LastId = database.tbl_Products.Max(p => p.ProductId);
for (int i = 0; i < query.Count(); i++)
{
database.tbl_ProductValue.Add(new Models.tbl_ProductValue()
{
ProductId_FK = LastId,
AttributeID_FK = query[i].Attribute_Id,
ProductValue = ??,
});
database.SaveChanges();
}
}
plz help me for how to get textbox?
I haven't worked with WebForms in a while but you can access the controls by their IDs like this:
ProductValue = ((TextBox)FindControl("txtatt" + i)).Text;

How to Create Dynamically Generated TextBoxes in ASP.NET

I have this code which generates TextBoxes in a Table dynamically, I got it from a website:
protected void AddStdBtn_Click(object sender, EventArgs e)
{
CreateDynamicTable();
}
private void CreateDynamicTable()
{
// Fetch the number of Rows and Columns for the table
// using the properties
if (ViewState["Stu"] != null)
Students = (DataTable)ViewState["Stu"];
int tblCols = 1;
int tblRows = Int32.Parse(NoTxt.Text);
// Now iterate through the table and add your controls
for (int i = 0; i < tblRows; i++)
{
TableRow tr = new TableRow();
for (int j = 0; j < tblCols; j++)
{
TableCell tc = new TableCell();
TextBox txtBox = new TextBox();
txtBox.ID = "txt-" + i.ToString() + "-" + j.ToString();
txtBox.Text = "RowNo:" + i + " " + "ColumnNo:" + " " + j;
// Add the control to the TableCell
tc.Controls.Add(txtBox);
// Add the TableCell to the TableRow
tr.Cells.Add(tc);
}
// Add the TableRow to the Table
tbl.Rows.Add(tr);
tbl.EnableViewState = true;
ViewState["tbl"] = true;
}
}
protected void CalculateBtn_Click(object sender, EventArgs e)
{
foreach (TableRow tr in tbl.Controls)
{
foreach (TableCell tc in tr.Controls)
{
if (tc.Controls[0] is TextBox)
{
Response.Write(((TextBox)tc.Controls[0]).Text);
}
}
Response.Write("<br/>");
}
}
protected override object SaveViewState()
{
object[] newViewState = new object[2];
List<string> txtValues = new List<string>();
foreach (TableRow row in tbl.Controls)
{
foreach (TableCell cell in row.Controls)
{
if (cell.Controls[0] is TextBox)
{
txtValues.Add(((TextBox)cell.Controls[0]).Text);
}
}
}
newViewState[0] = txtValues.ToArray();
newViewState[1] = base.SaveViewState();
return newViewState;
}
protected override void LoadViewState(object savedState)
{
//if we can identify the custom view state as defined in the override for SaveViewState
if (savedState is object[] && ((object[])savedState).Length == 2 && ((object[])savedState)[0] is string[])
{
object[] newViewState = (object[])savedState;
string[] txtValues = (string[])(newViewState[0]);
if (ViewState["Stu"] != null)
Students = (DataTable)ViewState["Stu"];
if (txtValues.Length > 0)
{
//re-load tables
CreateDynamicTable();
int i = 0;
foreach (TableRow row in tbl.Controls)
{
foreach (TableCell cell in row.Controls)
{
if (cell.Controls[0] is TextBox && i < txtValues.Length)
{
((TextBox)cell.Controls[0]).Text = txtValues[i++].ToString();
}
}
}
}
//load the ViewState normally
base.LoadViewState(newViewState[1]);
}
else
{
base.LoadViewState(savedState);
}
}
Now if you examine CreateDynamicTable() function, particularly the line:
int tblRows = Int32.Parse(NoTxt.Text);
you can see that the user can control the number of rows by entering a number in the TextBox NoTxt. When I run this code I get the following error:
An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code
Additional information: Input string was not in a correct format.
and the error location is in the same line above.
The question is: How can I let the user control the number of rows in the table?
After few days of reading and research, the solution can be done by using Session variables. Store the value obtained in NoTxt.Text as follows for example:
Session["V"] = Int32.Parse(NoTxt.Text);
and retrieve it once you need it.

Change the visibility of label within a datalist

How can I change the visibility of a label with a datalist using asp.net and C#-4.0 ? I tried the following code but unfortunately it is not working:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
viewall();
}
protected void viewall()
{
MySqlCommand objacess = new MySqlCommand();
objacess.CommandText = "select * from product_tbl ";
DataTable dobj = new DataTable();
dobj = objDataAccess.GetRecords(objacess);
for (int i = 0; i < dobj.Rows.Count; i++)
{
string d = dobj.Rows[i]["pdiscount"].ToString();
int di = Convert.ToInt32(d);
if (di > 0)
{
Label lbldisc = (Label)DataList1.FindControl("lbl_discount");
lbldisc.Visible=true;
}
}
DataList1.DataSource = dobj;
DataList1.DataBind();
}
When I try this I receive the following Error:
Error : Object reference not set to an instance of an object.
dear friend your code is not finding the lable control thats why its giving you this error and all our databound countrol are working on then indexes so change you this line
Label lbldisc = (Label)DataList1.FindControl("lbl_discount");
to the following
Label lbldisc = (Label)DataList1.item[i].FindControl("lbl_discount");
you need to write hide code after binding the values.
//bind value to datalist
DataList1.DataSource = dobj;
DataList1.DataBind();
//after binding hide the label
for (int i = 0; i < dobj.Rows.Count; i++)
{
string d = dobj.Rows[i]["pdiscount"].ToString();
int di = Convert.ToInt32(d);
if (di > 0)
{
Label lbldisc = (Label)DataList1.FindControl("lbl_discount");
lbldisc.Visible=true;
}
}

Create Dynamic Textbox and Get values

I want to create dynamic text box when user click on Add more link button.
For this I am using this code. And I have to mention that I am using master page.
protected void lnkAddMore_Click(object sender, EventArgs e)
{
if (Request.Cookies["value"] != null)
{
i = Convert.ToInt32(Request.Cookies["value"].Value) + 1 ;
}
for (int k = 1; k <= i; k++)
{
LiteralControl literal = new LiteralControl();
literal.Text = "<br /><br />";
Label newLabel = new Label();
newLabel.Text = "Choice" + " " + k.ToString();
newLabel.ID = "lblChoice_" + k.ToString();
newLabel.Attributes.Add("runat", "Server");
this.panelLabel.Controls.Add(newLabel);
this.panelLabel.Controls.Add(literal);
LiteralControl literal1 = new LiteralControl();
literal1.Text = "<br /><br />";
TextBox nexText = new TextBox();
nexText.ID = "txtChoice_" + k.ToString();
nexText.Attributes.Add("TextMode", "MultiLine");
nexText.Attributes.Add("runat", "Server");
panelTextbox.Controls.Add(nexText);
this.panelTextbox.Controls.Add(literal1);
Response.Cookies["value"].Value = i.ToString();
Session["Panel"] = panelTextbox;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["Panel"] != null)
{
ContentPlaceHolder content=new ContentPlaceHolder();
content.Controls.Add(Session["Panel"] as Panel);
}
}
}
Now I am facing trouble how to retrieve the data of the these text boxes after the clicking on the submit button so that I can store the values of there text boxes to database.
What will be code written for the click event of btnSave
protected void btnSave_Click(object sender, EventArgs e)
{
if (Session["Panel"] != null)
{
ContentPlaceHolder content_new = new ContentPlaceHolder();
for (int i = 1; i <= count; i++)
{
strControlName = "txtChoice_" + i.ToString();
TextBox objTextBox = (TextBox)content_new.FindControl(strControlName);
strTextBoxValues[i] = objTextBox.Text;
string str3 = strTextBoxValues[2];
}
}
}
This code is showing error for objTextBox. The error is NullReferenceException.
How to write stored procedure for saving data of above code?
The main problem is handling the parameter declaration, how to declare dynamic parameter for passing values so that value is saved for dynamic textbox?
Thanks.
I have already answered it here.
Lost dynamically created text box values
You can try this.
private string GetValue(string ControlID)
{
string[] keys = Request.Form.AllKeys;
string value = string.Empty;
foreach (string key in keys)
{
if (key.IndexOf(ControlID) >= 0)
{
value = Request.Form[key].ToString();
break;
}
}
return value;
}
Then to get the value
string txtChoice1value = GetValue("txtChoice1");
First of all when you dynamically create a control it doesn't need to be set "runat = sever".
Problem is in this line `ContentPlaceHolder content_new = new ContentPlaceHolder();` you make a new ContentPlaceHolder, this mean it doesn't have any control to be found.
Check this page. How To Create TextBox Control Dynamically at Runtime
You need to find the reference of your already created ContentPlaceHolder like-
ContentPlaceHolder cnt =(ContentPlaceHolder)this.Master.FindControl("ContentPlaceHolder1");
and then add the dynamically created Control in that ContentPlaceHolder as-
cnt.Controls.Add(Session["Panel"] as Panel);
Why you creating a new ContentPlaceHolder each time even when you have mentioned that you are using masterPage, so there must exists a ContentPlaceHolder..
Controls wont persist on postback have a look at http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx

Bind a multi-dimensional ArrayList to a Gridview

I have a DataGrid of seats available, each with a checkbox to be able to reserve the seat. In the button click event, if the CheckBox is clicked, I am adding the contents of the row to an ArrayList, then adding the ArrayList to a session before redirecting to the confirmation page:
protected void Reserve_Click(object sender, EventArgs e)
{
{
ArrayList seatingArreaList = new ArrayList();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
Guid SeatId = (Guid)GridView1.DataKeys[i][0];
CheckBox cbReserve = (CheckBox)GridView1.Rows[i].FindControl("cbReserve");
Label lblSection = (Label)GridView1.Rows[i].FindControl("lblSection");
Label lblRow = (Label)GridView1.Rows[i].FindControl("lblRow");
Label lblPrice = (Label)GridView1.Rows[i].FindControl("lblPrice");
if (cbReserve.Checked)
{
string tempRowInfo = lblSection.Text + "|" + lblRow.Text + "|" + lblPrice.Text;
seatingArreaList.Add(tempRowInfo);
}
}
// Add the selected seats to a session
Session["Seating"] = seatingArreaList;
}
Response.Redirect("Confirm.aspx?concertId=" + Request.QueryString["concertId"]);
}
On the confirmation page, Id like to split this array up and bind it to another gridview in their individual columns.
On the confirmation page, a session exists that has three columns separated with a pipe, I am struggling to split this up and bind it to a confirmation grid.
Please help!
This would probably be easier to just create a DataTable, then add it to the session variable. Once redirected to the confirmation page just bind GridView to the DataTable pulled from the session variable.
protected void Reserve_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Section");
dt.Columns.Add("Row");
dt.Columns.Add("Price");
{
ArrayList seatingArreaList = new ArrayList();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
Guid SeatId = (Guid)GridView1.DataKeys[i][0];
CheckBox cbReserve = (CheckBox)GridView1.Rows[i].FindControl("cbReserve");
Label lblSection = (Label)GridView1.Rows[i].FindControl("lblSection");
Label lblRow = (Label)GridView1.Rows[i].FindControl("lblRow");
Label lblPrice = (Label)GridView1.Rows[i].FindControl("lblPrice");
if (cbReserve.Checked)
{
DataRow dr = dt.NewRow();
dr["Section"] = lblSection.Text;
dr["Row"] = lblRow.Text;
dr["Price"] = lblPrice.Text;
dt.Rows.Add(dr);
}
}
// Add the selected seats to a session
Session["Seating"] = dt;
}
Response.Redirect("Confirm.aspx?concertId=" + Request.QueryString["concertId"]);
}
var q = from dto in seatingArreaList
let z = dto.Split("|".ToCharArray())
select z;
and then just bing q to the grid.

Resources