Hi,
in my aspx page I have somting like that: -
<asp:Content ID="Content1" ContentPlaceHolderID="Body" Runat="Server">
-
<asp:UpdatePanel ID="UpdatePanel1" ChildrenAsTriggers="true" UpdateMode="Conditional" runat="server" >
-
<ContentTemplate>
-
<asp:Panel ID="Panel1" runat="server">
-
-
<asp:Table ID="PO_Tags" Width="90%" BorderWidth="1" HorizontalAlign="Center"
-
CellPadding ="2" CellSpacing ="1" visible="false" runat="server">
-
<asp:TableHeaderRow>
-
<asp:TableHeaderCell BackColor="AliceBlue" >Tag Name<br /><br /></asp:TableHeaderCell>
-
<asp:TableHeaderCell BackColor="AliceBlue" >Association Type<br /><br /></asp:TableHeaderCell>
-
<asp:TableHeaderCell BackColor="AliceBlue" >Tag Description<br /><br /></asp:TableHeaderCell>
-
<asp:TableHeaderCell BackColor="AliceBlue" >Tag Value<br /><br /></asp:TableHeaderCell>
-
</asp:TableHeaderRow>
-
</asp:Table>
-
-
<br />
-
<asp:Button ID="Submit" Text="Submit" OnClick="Submit_Click" Visible="false" runat="server" />
-
-
</asp:Panel>
-
</ContentTemplate>
-
</asp:UpdatePanel>
-
</asp:Content>
from c# code I'm building my table rows dynamicly: -
while (rdr.Read())
-
{
-
trRow = new TableRow();
-
-
tcCell = new TableCell();
-
tcCell.Text = rdr["Label"].ToString();
-
trRow.Cells.Add(tcCell);
-
-
tcCell = new TableCell();
-
switch (rdr["ControlType"].ToString())
-
{
-
case "Text":
-
case "Integer":
-
case "Real":
-
TextBox ctrl1 = new TextBox();
-
ctrl1.ID = String.Format("tagCtrl{0}" ,countTagsControls);
-
ctrl1.Text = "Test";
-
tcCell.Controls.Add(ctrl1);
-
//this.UpdatePanel1.ContentTemplateContainer.Controls.Add(ctrl1);
-
break;
-
-
.......
-
-
}
-
-
trRow.Cells.Add(tcCell);
-
PO_Tags.Rows.Add(trRow);
Then I'm trying to get the values of those controls: -
TextBox txtCtl = (TextBox)UpdatePanel1.FindControl(String.Format("tagCtrl{0}", i));
-
switch (rdr["ControlType"].ToString())
-
{
-
case "Text":
-
string v1 = txtCtl.Text.ToString();
-
break;
-
But it's not working!!
What I'm missing?!
Thanks in advance.
8 1793 Frinavale 9,735
Recognized Expert Moderator Expert
You need to specify what isn't working.
But I think I know what's wrong.
You aren't able to find the TextBox are you?
Well, that's because you declared the TextBoxes dynamically within the function that creates the DataTable. That means that these TextBoxes have a scope of that function...they no longer exist after the function is finished.
Before I continue you need to specify what your problem is.
Check out this article on How to use dynamic controls in ASP.NET
Also research using Templates.
Hi again,
Well the problem is here: -
TextBox txtCtl = (TextBox)UpdatePanel1.FindControl(String.format("tagCtrl{0}", i));
-
string v1 = txtCtl.Text.ToString();
The controls are created, but I'm not succeeding to get there values, If I'm trying to retrieve: -
UpdatePanel1.FindControl(String.format("tagCtrl{0}", i)).ID
I'm getting null!
I tried declaring my textbox ctrl1 outside the function as public but I'm still facing the same issue!
I'll research about Templates, thanks.
If you have any other ideas please let me know.
When I put my code of building dynamic controls inside page_load it worked fine, but I need to run that code from another function, so I tried: -
public partial class PO_Insert : System.Web.UI.Page
-
{
-
-
public TextBox ctrl1 = null;
-
public TableRow trRow = null;
-
public TableCell tcCell = null;
-
but still have a problem!
Frinavale 9,735
Recognized Expert Moderator Expert
You should be creating your new TextBox in the Page Init event (not the Page Load) or else you wont be able to retrieve the content from the dynamically created TextBox.
You need to retrieve your TextBox from the control that it exists within....in this case it's part of the Table control (not the UpdatePanel).
Don't you need more than one TextBox in your table?
How many TextBoxes do you need?
I still don't know what you're trying to do....
Is the column displaying the "Associatio n Type" (the "ControlTyp e") supposed to be displaying data in TextBoxes for all rows?
Hi,
The number of my controls is changing dynamically according to association mechanism (I'm taking part of developing of system that simulating XFactory if you know it) .
Those controls can be TextBoxes or CheckBoxes or ComboBoxes .. according to a controlType field stored in the db.
So what I'm trying to do is:
1) Fill a form with new production order for a factory (Line, CatalogID,...)
2) Retrieve the Tags that are relevant to that PO
3) Building the controls dynamically according to the tags hierarchy
4) Filling values into those controls (manually) and submitting
5) Build an XML file with those values
6) Send that xml to a stored procedure that verify the data and it types (Via web service)
7) Inserting that data into my tables if every thing is ok
Frinavale 9,735
Recognized Expert Moderator Expert
Ok, this is going to be a tricky one to implement.
I'm not entirely sure how you're going to do this... or even if what I'm about to recommend is going work.
I think you're going to have to do is create something to hold the dynamically created controls so that you can reference it later.
I'm going to recommend creating a generic List of Web.UI.Control types: -
public partial class PO_Insert : System.Web.UI.Page
-
{
-
List<Web.UI.Control> webControlContent;
-
}
Now in your Page Init event you're going to have poll the database in order to dynamically create your controls. Each control that you create you're going to have add to the the generic list and to your page.
For now let's forget the Table all together.
Let's just add the controls as one big mess to the page as we create them.
So in the method that handles the Page Init class you'll have something like: -
public partial class PO_Insert : System.Web.UI.Page
-
{
-
List<Web.UI.Control> webControlContent;
-
-
private void PO_Insert_Init(Object sender, System.EventArgs e)
-
{
-
//Initializing the generic list of web controls
-
webControlContent = new List<Web.UI.Control>();
-
-
/*
-
Now you need to call the database to figure out
-
what type of controls to create.
-
-
You should look into using Reflection to make this
-
process easier and cleaner.
-
*/
-
-
/*
-
Add the controls to the webControlContent list and
-
also add them Page.
-
-
Make sure to give the controls an ID so that
-
you can access them later.
-
-
You also need to specify any methods used to handle
-
any postback events that the controls may cause.
-
*/
-
-
-
}
-
-
}
You always need to recreate the controls used on the page in the Page Init event.
After the Page Init event they are loaded with the data that was entered by the user. If the control does not exists when ASP.NET is trying to load the data you are going to have problems.
Now that you have your controls available for you to use you can use them: -
string v1 = "";
-
foreach (Web.UI.Control ctrl in webControlContent )
-
{
-
if(ctrl.ID == "txtCtrl")
-
{
-
TextBox txtCtl = (TextBox) ctrl;
-
v1 = txtCtl.Text;
-
}
-
}
That's what I did, and it works: -
protected void Page_PreLoad(object sender, EventArgs e)
-
{
-
SqlConnection conn = null;
-
SqlCommand cmd = null;
-
SqlDataReader rdr = null;
-
int i = 0;
-
-
conString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
-
if (ViewState["isTextBoxCreated"] != null && (int)ViewState["isTextBoxCreated"] == 1)
-
{
-
try
-
{
-
TableRow trRow = null;
-
TableCell tcCell = null;
-
-
conn = new SqlConnection(conString);
-
conn.Open();
-
-
cmd = new SqlCommand("sp_BuildPODataTags", conn);
-
cmd.CommandType = CommandType.StoredProcedure;
-
-
cmd.Parameters.Add(new SqlParameter("@POID", txtPOID.Text));
-
cmd.Parameters.Add(new SqlParameter("@LineUID", lstLines.SelectedValue.ToString()));
-
-
rdr = cmd.ExecuteReader();
-
-
while (rdr.Read())
-
{
-
trRow = new TableRow();
-
-
tcCell = new TableCell();
-
tcCell.Text = rdr["Label"].ToString();
-
trRow.Cells.Add(tcCell);
-
-
tcCell = new TableCell();
-
if (Convert.ToInt16(rdr["AssociationType"]) == 100)
-
tcCell.Text = "Global";
-
else
-
tcCell.Text = "Line";
-
trRow.Cells.Add(tcCell);
-
-
tcCell = new TableCell();
-
tcCell.Text = rdr["Description"].ToString();
-
trRow.Cells.Add(tcCell);
-
-
tcCell = new TableCell();
-
switch (rdr["ControlType"].ToString())
-
{
-
case "Text":
-
case "Integer":
-
case "Real":
-
TextBox ctrl1 = new TextBox();
-
ctrl1.ID = "tagCtrl" + i.ToString();
-
tcCell.Controls.Add(ctrl1);
-
break;
-
case "Combo Box":
-
break;
-
case "Check Box":
-
break;
-
}
-
i += 1;
-
-
trRow.Cells.Add(tcCell);
-
PO_Tags.Rows.Add(trRow);
-
-
}
-
-
}
-
-
finally
-
{
-
if (conn != null)
-
{
-
conn.Close();
-
}
-
if (rdr != null)
-
{
-
rdr.Close();
-
}
-
}
-
}
-
}
-
-
-
protected void GetTags_Click(object sender, EventArgs e)
-
{
-
if (Validate_Data())
-
{
-
SqlConnection conn = null;
-
SqlCommand cmd = null;
-
SqlDataReader rdr = null;
-
int countTagsControls = 0;
-
-
this.UpdatePanel1.FindControl("PO_Tags").Visible = true;
-
this.Submit.Visible = true;
-
-
//Create_XML();
-
if (ViewState["isTextBoxCreated"] == null)
-
{
-
-
try
-
{
-
TableRow trRow = null;
-
TableCell tcCell = null;
-
-
conn = new SqlConnection(conString);
-
conn.Open();
-
-
cmd = new SqlCommand("sp_BuildPODataTags", conn);
-
cmd.CommandType = CommandType.StoredProcedure;
-
-
cmd.Parameters.Add(new SqlParameter("@POID", txtPOID.Text));
-
cmd.Parameters.Add(new SqlParameter("@LineUID", lstLines.SelectedValue.ToString()));
-
-
rdr = cmd.ExecuteReader();
-
while (rdr.Read())
-
{
-
trRow = new TableRow();
-
-
tcCell = new TableCell();
-
tcCell.Text = rdr["Label"].ToString();
-
trRow.Cells.Add(tcCell);
-
-
tcCell = new TableCell();
-
if (Convert.ToInt16(rdr["AssociationType"]) == 100)
-
tcCell.Text = "Global";
-
else
-
tcCell.Text = "Line";
-
trRow.Cells.Add(tcCell);
-
-
tcCell = new TableCell();
-
tcCell.Text = rdr["Description"].ToString();
-
trRow.Cells.Add(tcCell);
-
-
tcCell = new TableCell();
-
switch (rdr["ControlType"].ToString())
-
{
-
case "Text":
-
case "Integer":
-
case "Real":
-
TextBox ctrl1 = new TextBox();
-
ctrl1.ID = "tagCtrl" + countTagsControls.ToString();
-
ctrl1.Text = rdr["Value"].ToString ();
-
tcCell.Controls.Add(ctrl1);
-
ViewState["isTextBoxCreated"] = 1;
-
break;
-
case "Combo Box":
-
break;
-
case "Check Box":
-
break;
-
}
-
countTagsControls += 1;
-
-
trRow.Cells.Add(tcCell);
-
PO_Tags.Rows.Add(trRow);
-
-
}
-
}
-
-
finally
-
{
-
if (conn != null)
-
{
-
conn.Close();
-
}
-
if (rdr != null)
-
{
-
rdr.Close();
-
}
-
}
-
}
-
}
-
-
}
-
-
public void Create_XML()
-
{
-
SqlConnection conn = null;
-
SqlCommand cmd = null;
-
SqlDataReader rdr = null;
-
-
// Create a new XmlTextWriter instance
-
writer = new XmlTextWriter(Server.MapPath(@"XML\POInfo.xml"), Encoding.UTF8);
-
-
// Creating <POInfo> element
-
writer.WriteStartDocument();
-
writer.WriteStartElement("POInfo");
-
-
// Creating the <PO> element
-
writer.WriteStartElement("PO");
-
-
writer.WriteElementString("POID", txtPOID.Text);
-
writer.WriteElementString("LineID", lstLines.SelectedItem.ToString());
-
writer.WriteElementString("CatalogID", lstCatalogs.SelectedValue.ToString());
-
writer.WriteElementString("RequestedQty", txtRequestedQty1.Text + "." + txtRequestedQty2.Text);
-
writer.WriteElementString("LotID", txtLotID.Text);
-
writer.WriteElementString("Priority", txtPriority.Text);
-
writer.WriteElementString("UserID", Session["User"].ToString());
-
-
writer.WriteEndElement();
-
// </PO>
-
-
// Creating <POTags> element
-
writer.WriteStartElement("POTags");
-
try
-
{
-
conn = new SqlConnection(conString);
-
conn.Open();
-
-
cmd = new SqlCommand("sp_BuildPODataTags", conn);
-
cmd.CommandType = CommandType.StoredProcedure;
-
-
cmd.Parameters.Add(new SqlParameter("@POID", txtPOID.Text));
-
cmd.Parameters.Add(new SqlParameter("@LineUID", lstLines.SelectedValue.ToString()));
-
-
rdr = cmd.ExecuteReader();
-
-
int i = 0;
-
String ctlValue = "";
-
while (rdr.Read())
-
{
-
switch (rdr["ControlType"].ToString())
-
{
-
case "Text":
-
case "Real":
-
case "Integer":
-
TextBox txtCtl = (TextBox)UpdatePanel1.FindControl("tagCtrl" + i);
-
ctlValue = txtCtl.Text.ToString();
-
break;
-
}
-
i += 1;
-
-
writer.WriteStartElement("Tag");
-
writer.WriteAttributeString("ID", rdr["TagDefId"].ToString());
-
writer.WriteElementString("Value", ctlValue);
-
writer.WriteEndElement();
-
}
-
}
-
finally
-
{
-
if (conn != null)
-
conn.Close();
-
if (rdr != null)
-
rdr.Close();
-
}
-
writer.WriteEndElement();
-
// </POTags>
-
-
writer.WriteEndElement();
-
writer.WriteEndDocument();
-
writer.Close();
-
// </POInfo>
-
}
-
Thanks for your efforts.
Frinavale 9,735
Recognized Expert Moderator Expert @pisgasys
I'm glad you got it working :)
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Will |
last post by:
Hi all.
I'm learning VB.Net and am developing a WinForms app.
I'm trying to make an app that I will use to scan in one or more than
on image. I want to use a tabbed interface to hold each image. Here's
the code I'm using for testing purposes. I've got the code in the
form's load event, but I think I'd have the same problems no matter
where the code existed. Right now, the form has an empty tab control,
everthing else is dynamic.
<code>
|
by: theComputer7 |
last post by:
I cut down the code to make this half way understandable... I have
read Data Grid girls caution about over use of dynamic controls. I
truly believe what I am doing requires dynamically inserted user
controls.
Worse I'm trying to add dynamic user controls from within a repeater
loop (looping through attributes)... I bind to a function in the code
behind and pass in the attribute.
<asp:repeater id="rAttributes" Runat="server">
|
by: hybrid |
last post by:
I have problems in understanding the behavior of the events triggered by
dynamically created controls over a webform.
Could you help me?
In a webform, I have a static PlaceHolder PH containing a table of
controls, dynamically generated. Among these, a linkbutton (LinkBtn) to
which have attached a click handler method, LinkBtn_Click(...) .
Generally it works fine, but if in the LinkBtn_Click(...) code I try to
access a webform control,...
|
by: Dave Williamson |
last post by:
When a ASPX page is created with dynamic controls based on what the
user is doing the programmer must recreate the dynamic controls again
on PostBack in the Page_Load so that it's events are wired and are
called like a static control.
Here is the problem that I need to solve. The processing overhead
that occurs to determine what dynamic controls need to be added
involves business logic and a query or queries of data in a sql server...
|
by: Leo J. Hart IV |
last post by:
OK, here's another question for the experts:
I am building a multi-step (3 steps actually) form using a panel for
each step and hiding/displaying the appropriate panel/panels depending
on which step you're on. This all works fine, but I ran into some
trouble when I started creating controls dynamically in my code-behind
file.
Each panel contains a table which is filled with various radio buttons,
text fields and the such which are...
| |
by: mytestemailaccount |
last post by:
Hi,
Hope you can help.
I am relatively new to all this but would appreciate the groups help.
The scenario:
I am c# and asp.net to create a web application. The web page contains
a user control (.ascx).
|
by: pbb |
last post by:
I'm creating a set of dynamic controls on a webpage by calling my
BuildControls sub in the Page_Init sub. I recreate the controls by calling
the BuildControls sub in the LoadViewState override procedure. The set of
controls will not always be the same (based on the selection the user makes
in a static ddl). The controls are persisted perfectly.
The problem I'm experiencing is that when one of the dynamic ddl's (let's
say ddl1) is...
|
by: WebBuilder451 |
last post by:
I have a series of dynamic link buttons created based upon a datareader. I've
added a click event and it calls the sub ok:
example: "while loop through the reader"
Dim ltrCtrl As New LiteralControl
Dim lbtnCtrl As New LinkButton
ltrCtrl.Text = "<br>"
lbtnCtrl.Text = "WE: " & dtrCal(10).ToString
lbtnCtrl.ToolTip = dtrCal(10).ToString & " these events"
& dtrCal(1)
lbtnCtrl.ID = "wecc" & i.ToString
|
by: Diffident |
last post by:
Hello All,
I am trying to add dynamic controls onto my page and here is how I am doing
that.
I have a page which has a button called as "AddMoreControls" and in this
button's event handler I am creating controls dynamically and adding them to
a panel on the page. For example, if the button is clicked once, the page is
posted back and the controls are added properly. However, if I click the
"AddMoreControls" for the second time the...
|
by: Tarscher |
last post by:
hi all,
I have this seemingly simple problem. I have lost a lot of time on it
though.
When a user selects a value from a dropdownlist (static control) a
dynamic control is generated. I have to create the dynamic controls
in the OnInit stage of the lifecycle. Since data from static controls
is not yet available in the OnInit stage I can't know what dynamic
control I have to create.
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |