Asked by:
Displaying Dynamically Generated Text Fields values in edit screen

Question
-
User-190697402 posted
Hi i want to display the values of dynamically generated text fields ,which is stored in database to the edit screen of my application.
Here is the code for edit screen where i will display the other details.
protected void btnPreEventInviteeDetails() { SqlCommand cmd = new SqlCommand("SPPreEventInviteeDetails", conn); cmd.CommandType = CommandType.StoredProcedure; conn.Open(); cmd.Parameters.Add("@emailAddress", SqlDbType.VarChar).Value = Request.QueryString["inviteeEmailAddress"].ToString(); cmd.Parameters.Add("@event_name", SqlDbType.VarChar).Value = Request.QueryString["eventname"].ToString(); SqlDataAdapter editSDA = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); editSDA.Fill(ds); txtEventName.Text = ds.Tables[0].Rows[0]["event_name"].ToString(); txtEventDate.Text = Convert.ToDateTime(ds.Tables[0].Rows[0]["event_date"].ToString()).ToString("dd/MM/yyyy"); txtTotalCost.Text = ds.Tables[0].Rows[0]["total_cost_of_event"].ToString(); txtCostPerHead.Text = ds.Tables[0].Rows[0]["cost_per_head"].ToString(); txtSalesChannel.Text = ds.Tables[0].Rows[0]["sales_channel"].ToString(); txtSalesManager.Text = ds.Tables[0].Rows[0]["sales_manager"].ToString(); txtSalesPerson.Text = ds.Tables[0].Rows[0]["sales_person"].ToString(); string inviteTier = ds.Tables[0].Rows[0]["invitee_tier"].ToString(); rdInviteTier.Items.FindByValue(inviteTier).Selected = true; txtInviteeSalutation.Text = ds.Tables[0].Rows[0]["invitee_salutation"].ToString(); txtInviteeFirstName.Text = ds.Tables[0].Rows[0]["invitee_first_name"].ToString(); txtInviteeLastName.Text = ds.Tables[0].Rows[0]["invitee_last_name"].ToString(); txtOrgName.Text = ds.Tables[0].Rows[0]["invitee_organisation_name"].ToString(); txtInviteeDesignation.Text = ds.Tables[0].Rows[0]["invitee_designation"].ToString(); txtInviteeEmailAddress.Text = ds.Tables[0].Rows[0]["email_address"].ToString(); txtInviteeContact.Text = ds.Tables[0].Rows[0]["contact_no"].ToString(); txtSiteId.Text = ds.Tables[0].Rows[0]["site_id"].ToString(); txtRevenueSize.Text = ds.Tables[0].Rows[0]["revenue_size_per_annum"].ToString(); string noOfPersons = ds.Tables[0].Rows[0]["no_of_pax"].ToString(); lstNoOfPersons.Items.FindByText(noOfPersons).Selected = true; txtPartnerOrg.Text = ds.Tables[0].Rows[0]["partner_organisation"].ToString(); string isDHLCustomer = ds.Tables[0].Rows[0]["is_invitee_a_dhl_customer"].ToString(); rdDHLCustomer.Items.FindByValue(isDHLCustomer).Selected = true; string isCusBckForEvent = ds.Tables[0].Rows[0]["is_the_guest_a_backup_for_the_event"].ToString(); rdBckforEvent.Items.FindByValue(isCusBckForEvent).Selected = true; txtJustification.Text = ds.Tables[0].Rows[0]["justification"].ToString(); string attndPrvEvents = ds.Tables[0].Rows[0]["attended_previous_events"].ToString(); rdBtnAttndPrvEvents.Items.FindByValue(attndPrvEvents).Selected = true; string hodApproval = ds.Tables[0].Rows[0]["hod_approval"].ToString(); string mdApproval = ds.Tables[0].Rows[0]["md_approval"].ToString(); string coApproval = ds.Tables[0].Rows[0]["compliance_officer_approval"].ToString(); if ((hodApproval == "Yes") && (mdApproval == "Yes") && (coApproval == "Yes")) { pnlRSVPDetails.Visible = true; chkHODAprv.Checked = true; chkMDAprv.Checked = true; chkCOAprv.Checked = true; } conn.Close(); }
My dynamic contents are generated inside this panel
<asp:Panel runat="server" ID="container" ></asp:Panel>
This is the function for generating my dynamic controls
private void GenerateControls() { int numberOfControls = Convert.ToInt32(Session["ControlGenerated"]); container.Controls.Clear(); for (int i = 0; i < numberOfControls; i++) { Panel p = new Panel(); p.ID = $"container{i}"; container.Controls.Add(p); p.Controls.Add(new LiteralControl($"<tr>")); p.Controls.Add(new LiteralControl("<td class='auto-style9' style='text-align:left;'>")); AddLabel($"Partner Name {i + 1}", p); p.Controls.Add(new LiteralControl("</td>")); p.Controls.Add(new LiteralControl("<td class='auto-style12'>")); AddTextBox($"txtPartnerName{i}", p); p.Controls.Add(new LiteralControl("</td>")); p.Controls.Add(new LiteralControl("<td>")); //AddDeleteButton(i); p.Controls.Add(new LiteralControl("</td>")); p.Controls.Add(new LiteralControl("</tr>")); p.Controls.Add(new LiteralControl("<tr>")); p.Controls.Add(new LiteralControl("<td class='auto-style9' style='text-align:left;'>")); AddLabel($"Partner Email {i + 1}", p); p.Controls.Add(new LiteralControl("</td>")); p.Controls.Add(new LiteralControl("<td class='auto-style12'>")); AddTextBox($"txtPartnerEmail{i}", p); p.Controls.Add(new LiteralControl("</td>")); p.Controls.Add(new LiteralControl("<td>")); p.Controls.Add(new LiteralControl("</td>")); p.Controls.Add(new LiteralControl("</tr>")); p.Controls.Add(new LiteralControl("<tr>")); p.Controls.Add(new LiteralControl("<td class='auto-style9' style='text-align:left;'>")); AddLabel($"Partner Designation {i + 1}", p); p.Controls.Add(new LiteralControl("</td>")); p.Controls.Add(new LiteralControl("<td class='auto-style12'>")); AddTextBox($"txtPartnerDesignation{i}", p); p.Controls.Add(new LiteralControl("</td>")); p.Controls.Add(new LiteralControl("<td>")); p.Controls.Add(new LiteralControl("</td>")); p.Controls.Add(new LiteralControl("</tr>")); p.Controls.Add(new LiteralControl("<tr>")); p.Controls.Add(new LiteralControl("<td class='auto-style9' style='text-align:left;'>")); AddLabel($"Charge to the Main Invitee {i + 1}", p); p.Controls.Add(new LiteralControl("</td>")); p.Controls.Add(new LiteralControl("<td class='auto-style12'>")); AddRadioButton($"rdBtnChargeToInvitee{i}", p); p.Controls.Add(new LiteralControl("</td>")); p.Controls.Add(new LiteralControl("<td>")); p.Controls.Add(new LiteralControl("</td>")); p.Controls.Add(new LiteralControl("</tr>")); } pnlRSVPDetails.Visible = true; }
Thursday, July 23, 2020 10:42 AM
All replies
-
User-190697402 posted
Hi All,
Any help on this,its quite urgent.Thanks in advance!
Friday, July 24, 2020 1:54 AM -
User1535942433 posted
Hi teenajohn1989,
Accroding to your description,as far as I think,you could find the the textbox and use ExecuteReader to read from database and put into the textbox.
More details,you could refer to below codes:
string ConString = @"Data Source=(localDb)\MSSQLLocalDB;Initial Catalog=aspnet-TestApplicationWithDatabase-20190820030542;Integrated Security=true"; SqlConnection con = new SqlConnection(ConString); con.Open(); string CmdString = "Select * from Sum"; SqlCommand cmd = new SqlCommand(CmdString, con); SqlDataReader reader = cmd.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { //Find textbox TextBox1.Text = reader["Id"].ToString(); } } con.Close();
Best regards,
Yijing Sun
Friday, July 24, 2020 7:30 AM -
User-190697402 posted
Hi yij sun,
Thanks for your response.
Since the textfields were dynamically generated at runtime,i couldnt find it out on the edit screen.
when i get the text field i could assign the value from database.
Should i have to create the textbox again in this code?
Friday, July 24, 2020 10:08 AM -
User1535942433 posted
Hi teenajohn1989,
Accroding to your description,could you tell us how you add control?You need add control in Page_PreInit instead of Page_Load.
Since you don't post full codes,I create a demo.Just like this:
<div> <asp:Panel runat="server" ID="container"> </asp:Panel> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> </div>
Code-Behind:
protected void Page_PreInit(object sender, EventArgs e) { GenerateControls(); } protected void Page_Load(object sender, EventArgs e) { } private void AddLabel(string label, Panel p) { Label lbl = new Label(); lbl.Text = label; p.Controls.Add(lbl); } private void AddTextBox(string id, Panel p) { TextBox box = new TextBox(); box.ID = id; p.Controls.Add(box); } private void AddRadioButton(string id, Panel p) { RadioButton rbt = new RadioButton(); rbt.ID = id; p.Controls.Add(rbt); } private void GenerateControls() { int numberOfControls = 1; container.Controls.Clear(); for (int i = 0; i < numberOfControls; i++) { Panel p = new Panel(); p.ID = $"container{i}"; container.Controls.Add(p); p.Controls.Add(new LiteralControl($"<tr>")); p.Controls.Add(new LiteralControl("<td class='auto-style9' style='text-align:left;'>")); AddLabel($"Partner Name {i + 1}", p); p.Controls.Add(new LiteralControl("</td>")); p.Controls.Add(new LiteralControl("<td class='auto-style12'>")); AddTextBox($"txtPartnerName{i}", p); p.Controls.Add(new LiteralControl("</td>")); p.Controls.Add(new LiteralControl("<td>")); //AddDeleteButton(i); p.Controls.Add(new LiteralControl("</td>")); p.Controls.Add(new LiteralControl("</tr>")); p.Controls.Add(new LiteralControl("<tr>")); p.Controls.Add(new LiteralControl("<td class='auto-style9' style='text-align:left;'>")); AddLabel($"Partner Email {i + 1}", p); p.Controls.Add(new LiteralControl("</td>")); p.Controls.Add(new LiteralControl("<td class='auto-style12'>")); AddTextBox($"txtPartnerEmail{i}", p); p.Controls.Add(new LiteralControl("</td>")); p.Controls.Add(new LiteralControl("<td>")); p.Controls.Add(new LiteralControl("</td>")); p.Controls.Add(new LiteralControl("</tr>")); p.Controls.Add(new LiteralControl("<tr>")); p.Controls.Add(new LiteralControl("<td class='auto-style9' style='text-align:left;'>")); AddLabel($"Partner Designation {i + 1}", p); p.Controls.Add(new LiteralControl("</td>")); p.Controls.Add(new LiteralControl("<td class='auto-style12'>")); AddTextBox($"txtPartnerDesignation{i}", p); p.Controls.Add(new LiteralControl("</td>")); p.Controls.Add(new LiteralControl("<td>")); p.Controls.Add(new LiteralControl("</td>")); p.Controls.Add(new LiteralControl("</tr>")); p.Controls.Add(new LiteralControl("<tr>")); p.Controls.Add(new LiteralControl("<td class='auto-style9' style='text-align:left;'>")); AddLabel($"Charge to the Main Invitee {i + 1}", p); p.Controls.Add(new LiteralControl("</td>")); p.Controls.Add(new LiteralControl("<td class='auto-style12'>")); AddRadioButton($"rdBtnChargeToInvitee{i}", p); p.Controls.Add(new LiteralControl("</td>")); p.Controls.Add(new LiteralControl("<td>")); p.Controls.Add(new LiteralControl("</td>")); p.Controls.Add(new LiteralControl("</tr>")); } //pnlRSVPDetails.Visible = true; } protected void Button1_Click(object sender, EventArgs e) { //TextBox thisTB = this.Form.FindControl("container").FindControl("txtPartnerName0") as TextBox; int numberOfControls = 1; string ConString = @"Data Source=(localDb)\MSSQLLocalDB;Initial Catalog=aspnet-TestApplicationWithDatabase-20190820030542;Integrated Security=true"; SqlConnection con = new SqlConnection(ConString); con.Open(); string CmdString = "Select * from Sum"; SqlCommand cmd = new SqlCommand(CmdString, con); SqlDataReader reader = cmd.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { for (int i = 0; i < numberOfControls; i++) { TextBox thisTB = Page.FindControl("txtPartnerName" + i.ToString()) as TextBox; thisTB.Text = reader["Id"].ToString(); } } } con.Close(); }
Best regards,
Yijing Sun
Wednesday, August 5, 2020 8:28 AM -
User-190697402 posted
Hi
Thanks for your response.I managed to display the records.
Tuesday, August 11, 2020 10:23 AM -
User1535942433 posted
Hi teenajohn1989,
Could you tell us what do you do to display the records?It may could help other members who also have the same questions in this community.
Also,you could mark these answers which help you.
Best regards,
Yijing Sun
Thursday, August 13, 2020 8:49 AM