How to read values from dynamicly created controls when posted back?????

It seemed a good idea, making an “inquiry-tool”; users can put their questions + the way to answer (eg radiobuttons or textfields) into a database. Answers are written back into the same database.

Using a placeholder I managed to get dynamicly user-written questions + ways-to-answer to the client-side. But here is the problem:
How to read values from dynamicly created controls when posted back?????
I really tryed very hard but I can’t. ........
Can anyone help me please??

Here’s my code:

<%@ Page Language="VB" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">
'*************************************************************************************************************************************

Sub Page_Load(Sender As Object, e As EventArgs)

if not ispostback then
FillTheForm
end if

End Sub

'*************************************************************************************************************************************

Sub FillTheForm


Dim connFillTheFormStr as string = ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=") & (server.mappath("\InquiryTest1.mdb"))
Dim connFillTheForm= New OleDbConnection (connFillTheFormStr)
connFillTheForm.Open

Dim strSQL as string="SELECT * FROM tblInquiryQuestions"
Dim cmdFillTheForm = New OleDbCommand(strSQL,connFillTheForm)
Dim rdrFillTheForm as OleDbDataReader = cmdFillTheForm.ExecuteReader

'
Dim myNumberOfQuestions as Integer
Dim myLit as literal = new literal
Dim myLitTekst as String

While rdrFillTheForm.read

myNumberOfQuestions=myNumberOfQuestions+1 'used to make a groupname

'writing a introduction, if any
if rdrFillTheForm.Item("IntroductionText")&""<>"" then
myLitTekst=("<tr bgcolor=#FFCC00>" & vbCrLf)
myLitTekst+= ("<td colspan=2 align=left valign=top>" & rdrFillTheForm.Item("IntroductionText") & "</td>" & vbCrLf)
myLitTekst+=("</tr>" & vbCrLf)
end if

'writing the question
myLitTekst+=("<tr bgcolor=#FFCC00>" & vbCrLf)
myLitTekst+= ("<td align=left valign=top>" & rdrFillTheForm.Item("Question") & "</td>" & vbCrLf)
myLitTekst+= ("<td align=left valign=top>"& vbCrLf)
'in placeholder
myLit.text=myLitTekst
PlaceHolder1.Controls.Add(myLit)

'question-type; only radiobutton is worked out, others types of questions = other controls :
If rdrFillTheForm.Item("Vraagtype") = "radiobutton" then
'
dim myRadioButton as radiobutton
dim myCounter as integer
dim myAantalChoicemogelijkheden as integer = rdrFillTheForm.Item("AantalChoicemogelijkheden")

for myCounter = 1 to myAantalChoicemogelijkheden
myRadioButton = new radiobutton()
myRadioButton.id = myCounter
myRadioButton.groupname="Vraag"& myNumberOfQuestions
myRadioButton.text="" & rdrFillTheForm.Item("tekstbijChoice" & myCounter)
PlaceHolder1.Controls.Add(myRadioButton)
myLit= new literal
myLit.text=("<br>"& vbcr)
PlaceHolder1.Controls.Add(myLit)
next

end if

myLit= new literal
myLitTekst= ("</td>" & vbCrLf & "</tr>" & vbCrLf)

End While

myLit= new literal
myLitTekst=("<tr bgcolor=#FFCC00><td colspan=2 height=10></td></tr>" & vbCrLf)
'table-row button
myLitTekst+=("<tr bgcolor=#FFCC00><td align=right colspan=2 height=10>" & vbCrLf)
myLit.text=myLitTekst
PlaceHolder1.Controls.Add(myLit)

'button:doesn't work too: no event
'Dim myButton As Button = New Button()
'myButton.Text = "ready"
'myButton.id="btnDone"
'AddHandler mybutton.Click, AddressOf btnDone_Click
'PlaceHolder1.Controls.Add(myButton)

myLit= new literal
myLitTekst=("</td></tr>" & vbCrLf)
'in control stoppen
myLit.text=myLitTekst
PlaceHolder1.Controls.Add(myLit)


rdrFillTheForm.close
connFillTheForm.close
connFillTheForm.dispose

end sub

'*************************************************************************************************************************************

Sub btnDone_Click(sender As Object, e As System.EventArgs)

Dim connSaveTheAnswersStr as string = ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=") & (server.mappath("\InquiryTest1.mdb"))
Dim ConnSaveTheAnswers= New OleDbConnection (connSaveTheAnswersStr)
ConnSaveTheAnswers.Open

Dim intNumberRows as Integer
Dim strSQL as String

Dim Control as Control
Dim I as Integer
Dim Choice as string

For Each control In Page.Controls
Dim myControl1 As Control = FindControl("vraag"&I)
If (Not myControl1 Is Nothing)

'Choice= CType(Controls(0), myControl1).Text
'myControl1.text??????????????????????????????

strSQL="INSERT INTO tblInquiryAnswers (" & _
"Question, " & _
"Answer " & _
") VALUES ('" & _
"','This is my problem','I really don't know!')"

Dim cmdSaveTheAnswers as New OleDbCommand(strSQL,ConnSaveTheAnswers)

Try
intNumberRows=cmdSaveTheAnswers.ExecuteNonQuery()
Catch ex as exception
lblWrong.text=ex.message
End Try

I=I+1

End If
next

lblOK.text= (intNumberRows & " answers are written")

ConnSaveTheAnswers.close
ConnSaveTheAnswers.dispose
end sub

'*************************************************************************************************************************************
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<table class="tabel" cellspacing="1" cellpadding="2" width="100%" border="0">
<tbody>
<asp:PlaceHolder id="PlaceHolder1" runat="server"></asp:PlaceHolder>
</tbody>
</table>
<asp:Button id="Button1" onclick="btnDone_Click" runat="server" Text="Verzend"></asp:Button>
<asp:Label id="lblOK" runat="server"></asp:Label><asp:Label id="lblWrong" runat="server"></asp:Label>
</form>
</body>
</html>
0
FrieMaas
2/5/2004 10:50:00 AM
asp.net.web-forms 93655 articles. 5 followers. Follow

2 Replies
705 Views

Similar Articles

[PageSpeed] 31

I'm not a code inline person but one thing you must always do with dynamically loaded controls is to create them on every request using the same control names.

Drop the IsPostBack chack and always call your FillTheForm method.
Mike Schellenberger - MCAD
0
schellem
2/5/2004 12:49:14 PM
To restore the state of dynamically created controls, you must create them every time (but populate them only on the first load). The controls must be created in the same order and have the same ID for it to work (although some people say its not necessary). I would recommend using Denis Bauer's DynamicPlaceHolder which does all this for you.
0
AmitTheNoob
2/5/2004 2:22:42 PM
Reply:

Similar Artilces:

Reading form values in dynamically created child controls in a user control
I have a common problem for which I've searched the web for answers for hours to no avail. I have some radiobuttonlists ,dropdownlists,checkboxlists and text controls in a user control. The lists are created statically, however the items in the lists are populated at run time. The lists are displayed in default.aspx (the containing page) and populated on the Page_Load event of the user control .ascx file based on an external data source containing arrays of data. I want to be able to read the values selected by the user from deafult.aspx, the parent page. details: The .ascx...

Value of Control created dynamically is not getting populated on post back
Hi I am creating a table at run time. If I put some value to textbox, its not getting populated after post back. If I do it on Page onInit event everything works fine.. but I cant do this as I have to make rows on button click. The code is like this Sub MakeTable() Dim ht As New Hashtable ht("1") = "1" ht("2") = "2" For i As Integer = 0 To 2 Dim tr As New TableRow For j As Integer = 0 To 1 Dim td As New TableCell Select Case (j) ...

How to maintain form controls value after user control post back?
 hello experts, I have created filter grid in asp.net & C#.My form design has contains 3 dropdown controls and Filter grid(User control).Its working fine(filter), but one problem arise.the form controls are clear when i filter a grid(post back).I tried to reterive values using view state, hidden controls, i got only null values.Anyone please help, its urgent to me. Its my user control code.FilterGrid.aspx<%@ Control Language="C#" AutoEventWireup="true" CodeFile="FilterGrid.ascx.cs" Inherits="FilterGrid" %><style type = "text/css&...

How to do post back in a dynamically created user control [provided that update panel is in the master page of the page where user control is added] {B/C of FileUpload Control}
I have a register my scriptmanager and place the update panel around contenttempalte for my pages int the master page. Now in one of my pages i am adding some user controls dynamically. In two of my user user controls I have FileUpload control which will not work under partial post back. So I want's full postback for them How could I do that {Provided that i am on the user control,which is inside the page ,and page have the contenttemplat...

Problem With Dynamicially Created Web User Control.on post back all controls lost.
Hi All,         i m facing Problem With Dynamicially Created Web User Control.on post back all controls lost. I have Created a Web User Control in asp.net.on that user control i have a button which has some functionality.it works fine when i add this control. but when i create this control at run time ,problem arise when i click on any of the button on the control.all control lost.i load this control by writing this code private SqlBuilderResultsControl objSBRC = new SqlBuilderResultsControl(); objSBRC = (SqlBuilderResultsControl)LoadControl("SqlBuilderR...

Problem in accessing web form data in user controls when create user control dynamically
HI. I have a problem using web user control. I designed a user control named NewsForm. This user control contains some web form elements like <asp:textbox>. When I add it in an .ASPX page, all things may be true. I can know which user filles in textbox (for example and other controls) <%@ Register TagPrefix="uc1" TagName="NewsForm" Src="NewsForm.ascx" %> and <UC1:NewsForm id="newsForm" runat="server"></UC1:NewsForm> But when i create it in code behined dynamically, however user control has web form controls but their value are blank for example //aspx co...

when loading a previously created record on to form, some controls are missing values(controls are depended on values selected in other controls)
Hello - I have created a page for Editing records.  this page will open up a chosen record and allow user to edit the data.On the form, I have 3 drop down lists.   The second dropdown list should fill values depending on value chosen in first dropdown list.  The third dropdown list should show/fill values depending on value chosen in second dropdown list.So taking this into count, i have several records with various data.  When I open-up/load a record in Edit form, only the first dropdown list has a value selected that was the pre-existing va...

Dynamic control post back value
*** edited by: master4eva *** Please enclose your code in < code ></ code> tags (without the spaces). This will make your code easier to read online; therefore, encouraging a response to be faster. It is to your own benefit for your question to be answered in future. I have already done the editing to include the < code ></ code> tags for this post. ********* I'm builidng a dynamic page with server controls inside a table based on data from sql table. While myReader.Read() Dim lbl2 As Label = New Label lbl2.ID = "l...

How to read value from dynamically created control
  Hi there,  Any good advices how should I do this correct way.  I have a form with one table (one row three columns). Also there are two buttons. The first button have the following code: protected void Button1_Click(object sender, EventArgs e) {    string[] foodFiles;    foodFiles = Directory.GetFiles("C:\\Temp\\WebSite");    string[] foodPathParts;    Control targetCell = FindControl("Cell1");    foreach (string fileName in foodFiles)    {       foodPath...

Dynamically created controls in web form
Hi there. This is bit of an open ASP.Net (C#) question with regards to dynamically created controls and in particular tabular or panel controls. What I currently have is a web page that creates between 1 and 6  tables, each of which holds specific data about a set of products, each table (or panel, if you want) has a couple of multi-line TextBoxes and a few other controls, all lined up in a set format. The number is determined by the data records returned from a database table. My question is what is a best way to create these tables of data at run time?  Has anybody else had e...

Dynamically creating controls in Web forms
I am able to create a web control programmatically and display on the form. In the click event of the button then I am trying to capture the value entered in the above created textbox in another click of button but I am unable to obtain the reference of the control that has been created dynamically. Can anyone help me please? protected System.Web.UI.WebControls.Panel myPanel; private void btnMake_Click(object sender, System.EventArgs e) { TextBox txtBox = new TextBox(); myPanel.Controls.Add(txtBox); } private void btnGotValues_Click(object sender, System...

How to get value from dynamic control on Post-back
Hi All, This is my first post. The problem is about dynamic control. 1. In Page Load, I auto generated a datagrid.  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load         Dim lmws As New LIMSWSV10.LIMSService         Dim ds As New DataSet         If Not IsPostBack Then             ds = lmws.GetResultValueForShippingJob(Request.QueryString("ShippingJobID")) ...

Retreaving Values from dynamically created Web Controls
I have a tricky problem that has been stumping me. I have a multiview page.  One the 2nd view I need to dynamically add  web controls for the user to enter in data for a search.  I am unable to get the data from the web controls before they are wiped out in the page load.  When a page button is pushed the page reloads and my data is gone ( because it was created dynamically ). I can not add the web controls in the On_Init method because 1) it is a multiview and 2) the users must choose which ones they want at run time on view 2. Currently, I'm trying to out...

dynamically creating html controls on web form
hello all, I'm creating html <input type="file" name="test" id="test" runat="server"> dynamically using Javascript on a .aspx page. when i submit the form i get Javascript "Access is denied" error. if i take out name attribute from input tag in javascript -  it is not throwing any javascript error but the file is not being uploaded to the server. Can anyone point me in the right direction? Thanx in advance     Maybe this is a security restriction imposed by the browser? I know there are several constrain...

Web resources about - How to read values from dynamicly created controls when posted back????? - asp.net.web-forms

LoL Library, League of Legends Guide & Database for iPhone, iPad, and iPod touch on the iTunes App Store ...
Get LoL Library, League of Legends Guide & Database on the App Store. See screenshots and ratings, and read customer reviews.

Sony joins indies at Amie Street—without dynamic pricing
Amie Street is known for its indie music collection, the ease of music …

Posts tagged Alien swarm at Big Download Blog
It's hard to complain about a game that's completely free to download and play online but there have apparently been those folks who want more ...

Rc/Funge-98
Welcome to the official home for the Rc/Funge-98 Funge interpreter. Rc/Funge-98 was one of the first Funge interpreters to implement the Funge-98 ...

Legend of Dungeon
... + final relase + the two planned expansions! + Steam keys coming soon!) buy the soundtrack on: Amazon iTunes Rapsody The music in is dynamicly ...

First hands-on with the Kin One and Kin Two - TIME.com
Microsoft’s acquisition of Danger in February of 2008 renewed interest in the languishing mobile operating system formerly known as Windows Mobile. ...

Credits for the TerminatorFiles sites and services
Credits are due to those that helped make TerminatorFiles what it is today. A detailed Terminator fanportal cranked with Terminator movie info, ...

Anonymous Hamburg: English
For most of us there is only one Internet. The World Wide Web. We have learned but won't accept, that it is watched, monitored, filtered, depriorized ...

mediola® - Investor Relations
Our investors page provides you with a quick overview of the most important information about our company.


Resources last updated: 2/9/2016 5:59:07 AM