Bind a multi-dimensional ArrayList to a Gridview - asp.net

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.

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;

dynamic data binding gridview

my datatable formed as follows
DataTable dtFinalData = new DataTable();
//Adding columns for BR details
dtFinalData.Columns.Add("SupId", typeof(string));
dtFinalData.Columns.Add("SupName", typeof(string));
DateTime dt = DateTime.Today;
int num = DateTime.DaysInMonth(DateTime.Today.Year, DateTime.Today.Month);
//--> adding columns for date part (1-31)
for (int i = 1; i < num + 1; i++)
{
dtFinalData.Columns.Add(i.ToString(), typeof(bool));
}
//<-- adding columns for date part (1-31)
#endregion
#region Fill dtFinalData
//--> Looping each BR from tblBrList
foreach (DataRow BrRow in dtBrList.Rows)
{
DataRow dr = dtFinalData.NewRow();
int SupId = Convert.ToInt32(BrRow[0]); //retrieve BR ID from dtBrList
String supName = BrRow[1].ToString(); //retreive Supervisor name from dtBrList
dr["SupId"] = SupId.ToString();
dr["SupName"] = supName;
for (int i = 1; i < num + 1; i++)
{
DateTime dtRunningDate = new DateTime(2013, 5, i);
//select BR_SUP_CODE,
DataRow[] drAttendance = dtAttendance.Select("BR_SUP_CODE=" + SupId + " AND SMS_DATE=#" + dtRunningDate + "#", string.Empty);
if (drAttendance.Length == 1)
{
//CheckBox chkbx = new CheckBox();
//chkbx.Checked = true;
dr[i.ToString()] = true;
}
else
{
//CheckBox chkbx = new CheckBox();
//chkbx.Checked = false;
dr[i.ToString()] = false;
}
}
dtFinalData.Rows.Add(dr);
}
//<-- Looping each BR from tblBrList
#endregion
GridView1.DataSource = dtFinalData;
GridView1.DataBind();
Now i want to add checked image in place of true and unchecked image in place of false.how to bind grid view dynamically such that in place of disable check box i want to insert two types of image?
Your DataTable part is fine and continue to add the True/False text as per the logic. Now you should handle the GridView part. So, define an event handler for the OnRowDataBound event of GridView.
In this event only, check for the Text property of the cells, and if True/False, clear the cells and add the required image.
<asp:GridView ID="GridView1" OnRowDataBound="GridView1_RowDataBound" ... />
And your event handler will have code as below:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
Image chkImage = new Image();
chkImage.ImageUrl="~/images/Checked.png";
Image UnchkImage = new Image();
UnchkImage.ImageUrl = "~/images/UnChecked.png";
if (e.Row.RowType == DataControlRowType.DataRow)
{
// We will start from 3rd cell because the first 2 cells are
// for SupID & SupName, where we don't need to place images
for (int cellIndex = 2; cellIndex < GridView1.Columns.Count; cells++)
{
// Add Checked Image when cell text is true
if (e.Row.Cells[cellIndex].Text == "true")
{
// clear the cell and add only the image
e.Row.Cells[cellIndex].Controls.Clear();
e.Row.Cells[cellIndex].Controls.Add(chkImage);
}
// Add Unchecked Image when cell text is false
if (e.Row.Cells[cellIndex].Text == "false")
{
// clear the cell and add only the image
e.Row.Cells[cellIndex].Controls.Clear();
e.Row.Cells[cellIndex].Controls.Add(unchkImage);
}
}
}
}

ASP .NET Wizard Control and View State

I have a form with 3 wizard steps, and when i click the button to dynamically add text boxes, that works fine, but when i go to the next step and i click on add to add more text boxes, it automatically adds all the text boxes from the previous steps and then continues to add if i keep click on it.
How do i prevent that from happening.
private List ControlsList
{
get
{
if (ViewState["controls"] == null)
{
ViewState["controls"] = new List();
}
return (List)ViewState["controls"];
}
}
private int NextID
{
get
{
return ControlsList.Count + 1;
}
}
protected override void LoadViewState(object savedState)
{
string section = Wizard1.ActiveStep.ID;
int sectionNum = Wizard1.ActiveStepIndex;
var control = Wizard1.ActiveStep.FindControl("Place" + sectionNum) as PlaceHolder;
base.LoadViewState(savedState);
int count = 0;
foreach (string txtID in ControlsList)
{
if (count == 0)
{
control.Controls.Add(new LiteralControl("<tr>"));
}
TextBox txt = new TextBox();
control.Controls.Add(new LiteralControl("<td>"));
txt.ID = txtID;
control.Controls.Add(txt);
control.Controls.Add(new LiteralControl("</td>"));
count = count + 1;
if (count == 3)
{
control.Controls.Add(new LiteralControl("</tr>"));
count = 0;
}
}
}
protected void AddControlButton_Click(object sender, EventArgs e)
{
string section = Wizard1.ActiveStep.ID;
int sectionNum = Wizard1.ActiveStepIndex;
var control = Wizard1.ActiveStep.FindControl("Place" + sectionNum) as PlaceHolder;
TextBox txt1 = new TextBox();
TextBox txt2 = new TextBox();
TextBox txt3 = new TextBox();
txt1.ID = section.ToString() + "Size" + NextID.ToString();
control.Controls.Add(new LiteralControl("<td>"));
control.Controls.Add(txt1);
control.Controls.Add(new LiteralControl("</td>"));
ControlsList.Add(txt1.ID);
txt2.ID = section.ToString() + "Description" + NextID.ToString();
control.Controls.Add(new LiteralControl("<td>"));
control.Controls.Add(txt2);
control.Controls.Add(new LiteralControl("</td>"));
ControlsList.Add(txt2.ID);
txt3.ID = section.ToString() + "Quantity" + NextID.ToString();
control.Controls.Add(new LiteralControl("<td>"));
control.Controls.Add(txt3);
control.Controls.Add(new LiteralControl("</td></tr>"));
ControlsList.Add(txt3.ID);
}
You are storing all of the dynamic textboxes in ViewState and then the ControlsList property getter is returning the whole list when it is building the textboxes.
My recommendation is to use Session cache instead of ViewState, because it will allow you to differentiate the textbox controls from each of the wizard steps, like this:
Session["WizardStep1"] = listOfTextBoxesFromStep1;
Session["WizardStep2"] = listOfTextBoxesFromStep2;
Session["WizardStep3"] = listOfTextBoxesFromStep3;

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

Gathering Data: Dynamic Text Boxes

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

Resources