Repeater question. Textboxes lose info

  • Thread starter Thread starter Fraggle
  • Start date Start date
F

Fraggle

I have a repeater with controls added at run time. the <template> also
contains a <asp:textbox that is made visible on some repeater
elements.

when I come to read the text info out it has disapeared. The read is
done on a button click.

I can read the selected items from the other controls in the repeater,

demo page here

http://bagheera.ncl.ac.uk/dps/DesktopDefault.aspx?TabID=97

Select some answers and write some text, you will see at the top some
numbers relating to what question you selected the "OTHER=" will
always be blank where it should be the text box contents.

The code is a bit (over) complex so if you have questions, or can see
how to do somthing better then please rip it appart :)

fragg
<%@ Control Language="VB" Inherits="ASPNetPortal.PortalModuleControl"
%>
<%@ Register TagPrefix="Portal" TagName="Title"
Src="~/DesktopModuleTitle.ascx" %>
<%@ import Namespace="ASPNetPortal" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">

'*******************************************************
'
' The Page_Load event handler on this User Control is
used to
' obtain a DataSet of Question information from the
'Question table, and ' then databind the results to a templated
'MyDataGridQuestions
' server control. It uses the ASPNetPortal.QuestionDB()
' data component to encapsulate all data functionality.
'
'*******************************************************


Dim Questions As New ASPNetPortal.QuestionDB()

'ID for person entered in database
Dim respondentID as Integer

Sub Page_Load(ByVal sender As Object, ByVal e As
EventArgs)




QuestionList.DataSource
=Questions.GetQuestions(ModuleId)
QuestionList.DataBind()


If not Page.IsPostBack Then
end if


End Sub



sub QuestionListDataBound(Sender As Object, e As
RepeaterItemEventArgs)

If (e.Item.ItemType = ListItemType.Item) Or _
(e.Item.ItemType =
ListItemType.AlternatingItem) Then

dim datasrc as new DataSet
datasrc = Questions.GetAnswersbyQuestion(
DataBinder.Eval(e.Item.DataItem, "itemID"))

select Case DataBinder.Eval(e.Item.DataItem,
"type")




case "radio" :
dim lab as new radiobuttonlist
lab.datasource = datasrc
lab.datatextfield="Answer"
lab.DataValueField="itemId"
' lab.RepeatDirection="0" '"0" for l/r "1"
for up/down.
lab.databind
CType(e.Item.FindControl("ph1"),
PlaceHolder).controls.add(lab)

case "checkbox" :
dim lab as new checkboxlist
lab.datasource = datasrc
lab.datatextfield="Answer"
lab.DataValueField="itemId"
lab.databind
CType(e.Item.FindControl("ph1"),
PlaceHolder).controls.add(lab)

end select
' loop through items to see if any are "other". if so
display other box.

Dim dt as DataTable = datasrc.Tables(0)
Dim row as DataRow

Dim i as Integer = 0

For Each row in dt.Rows

If cStr(row("Answer")).ToUpper = "OTHER" then
i += 1
End If

Next

if i > 0
CType(e.Item.FindControl("otherPanel"),
panel).visible=true
end if

end if

end sub

sub submit_click(sender As Object, e As
System.EventArgs)

'Add a person to the database.
dim respondentID as Integer
respondentID = Questions.addRespondent(moduleId
,now().gethashcode , true , Context.User.Identity.Name , now())

'get list of stuff in repeater
'go through each in list
' read out checked item(or items)
'read out "other" string
' enter all into DB

dim reptitem as RepeaterItem
For Each reptitem In QuestionList.items

If (reptitem.ItemType = ListItemType.Item) Or _
(reptitem.ItemType =
ListItemType.AlternatingItem) Then

dim i as integer = 0
dim ph as Placeholder
ph =
Ctype(Questionlist.items.item(reptItem.itemindex).findcontrol
("ph1"),placeholder)

dim Listx as System.Web.UI.WebControls.ListControl
Listx = ctype(ph.controls(0),
System.Web.UI.WebControls.ListControl)

' list through each item in the list to see if it is
selected.
dim lstItem As ListItem

For Each lstItem In listx.Items


If lstItem.Selected Then

' list the selected item


'Enter "lstItem.Text" into the database
i= Questions.AddResponse(lstItem.value ,
respondentID)



End If
Next
if ctype(Questionlist.items.item(reptItem.itemindex).findcontrol
("otherpanel"), panel).visible=true and i>0

response.write(lstItem.value &" i=" & i &" other="
& ctype(Questionlist.items.item(reptItem.itemindex).findcontrol
("otherTb"), textbox).text & " | ")
Questions.AddResponseOther(i,
ctype(Questionlist.items.item(reptItem.itemindex).findcontrol
("otherTb"), textbox).text)

end if
end if

next


' response.redirect("http://bagheera.ncl.ac.uk/dps/DesktopModules/awsurvey/Thanks.aspx")
end sub

</script>
<portal:title EditText="Add New Question"
EditUrl="~/DesktopModules/awsurvey/Question_Edit.aspx" runat="server"
/>
<asp:repeater id="QuestionList" runat="server"
onitemdatabound="QuestionListDataBound" >
<headerTemplate>
<center>
<h2><b>Travel to work survey.</b>
</h2>
</center>
</headerTemplate>
<itemTemplate>
<table border="0">
<tr>
<td>
<asp:HyperLink id= "editLink"
ImageUrl="~/images/edit.gif" NavigateUrl='<%#
"~/DesktopModules/awsurvey/Question_Edit.aspx?ItemID=" &
DataBinder.Eval(Container.DataItem, "ItemID") & "&mid=" & ModuleId
%>' Visible= "<%# IsEditable %>" runat= "server" />
<span class="normal">Q<%#
Container.dataitem("questionNo") %></span></td>
<td>
<span class="normal" style="font-size:1.1em;"><%#
Container.dataitem("question") %> </span></td>
</tr>
<tr>
<td rowspan="2">
&nbsp;</td>
<td>
<span class="normal" style="font-size:0.7em;
color:blue;"> <%# Container.dataitem("Instruction") %> </span></td>
</tr>
<tr>
<td>
<asp:PlaceHolder id="ph1" runat="server" />
<asp:panel id="otherPanel" runat="server"
visible="false">
<span class="normal">(please specify) </span>
<asp:textbox id="otherTb" runat="server" />
</asp:panel>
</td>
</tr>
</table>
</itemTemplate>
<separatorTemplate>
<br />
</separatorTemplate>
<footerTemplate></footerTemplate>
</asp:repeater>
<asp:button id="submit" runat="server" text="Submit Survey"
onclick="submit_click" />
'button to test simple post back, keeps the textbox loses the
checkboxes
<asp:button id="false" runat="server" text="pressme"/>
 
I have a repeater with controls added at run time. the <template> also
contains a <asp:textbox that is made visible on some repeater
elements.

when I come to read the text info out it has disapeared. The read is
done on a button click.

I can read the selected items from the other controls in the repeater,

demo page here

http://bagheera.ncl.ac.uk/dps/DesktopDefault.aspx?TabID=97

Select some answers and write some text, you will see at the top some
numbers relating to what question you selected the "OTHER=" will
always be blank where it should be the text box contents.

The code is a bit (over) complex so if you have questions, or can see
how to do somthing better then please rip it appart :)

fragg
<snip code >
http://groups.google.co.uk/[email protected]


I knew posting 4 pages of code would scare people off ;)

so here is the thing

I have dynamicaly added controls in a repeater, either checkbox or
radiobox. on the click of a button i spool throuhg all the answers to
see if they are checked or not. This works

In the repeater I also have a text box, that i want to save the
entered text from.

when I click the button the page posts back *and i rebind the repeater
to the data source* if i dont do this then the controls are not
created and the values cannot be read. HOWEVER on the databind the
text boxes are cleared. I am unsure how the checkboxes remmeber there
status through the bind. but I do know that i must bind to get it to
work

If i do a simple postback with no binding then the textboxes remeber
there values, but no checkboxes are displayed

is there any way i can "cashe" teh repeaters data so it survives the
postback without needing to be re-bound to the data source?

Cheers

Fragg
 
Back
Top