Solved

How to get Control values from repeater

Posted on 2009-04-03
30
440 Views
Last Modified: 2012-05-06
Hi this is my code for a repeater contol on a page which is in itself on a master page.

                    <asp:Repeater ID="repAnswers" runat="server" >
                       
                        <HeaderTemplate><table width="100%" cellpadding="0" cellspacing="0"></HeaderTemplate>
                       
                        <ItemTemplate>
                            <tr>
                                <td class="answer" width="33%"><asp:label ID="lblhAnswer_ID" runat="Server" Text='<%#DataBinder.Eval(Container.DataItem, "Answer_ID")%>' Visible="true" ></asp:label><%#DataBinder.Eval(Container.DataItem, "Answer")%></td>
                                <td class="nextquestion" width="33%"><%#DataBinder.Eval(Container.DataItem,"NextQuestion_ID")%></td>
                                <td class="nextquestion" width="33%"><asp:DropDownList ID="ddlNextQuestion" runat="server"  DataSourceID="sdsNextQuestion" DataValueField="Question_ID" DataTextField="DisplayOrder" AutoPostBack="true" OnSelectedIndexChanged="UpdateNextQuestion"></asp:DropDownList></td>
                            </tr>
                        </ItemTemplate>
                       
                        <FooterTemplate></table></FooterTemplate>
                       
                    </asp:Repeater>

The dropdown is filled from an SQLDataSource on the page. My question is how do I get the values for answerid and the dropdown in my event.

I have tried this but no luck:
Dim key As String = CType(Page.Master.FindControl("lblhAnswer_ID"), Label).Text
Dim NextQuestion = CType(Page.Master.FindControl("ddlNextQuestion"), DropDownList).SelectedValue

And takin account of the master page tried this.
Dim key As String = CType(Page.Master.FindControl("ctl00$cph_Main$lblhAnswer_ID"), Label).Text
Dim NextQuestion = CType(Page.Master.FindControl("ctl00$cph_Main$ddlNextQuestion"), DropDownList).SelectedValue
0
Comment
Question by:Andy Green
  • 15
  • 9
  • 4
  • +1
30 Comments
 
LVL 8

Expert Comment

by:Faheem Shaikh
Comment Utility
0
 
LVL 8

Expert Comment

by:Faheem Shaikh
Comment Utility
foreach (RepeaterItem item in repAnswers.Items)
{
Label lblAnsId = (Label)item.FindControl("lblhAnswer_ID");
Response.Write(lblAnsId.Text);
}
0
 
LVL 8

Expert Comment

by:Faheem Shaikh
Comment Utility
For VB.net:
Dim ddl as DropDownList = Ctype(item.FindControl("ddlNextQuestion"),
DropDownList)
0
 
LVL 3

Author Comment

by:Andy Green
Comment Utility
faheem29484 -  in the for each i get the error repAnswers not declared and in your last post for 'item' I get not declared.
The findcontrol doesn't work like this with master pages, that is my problem.
One thing to mention, my repeater controls are nested, I have an outer repeater for questions, and an inner one for the questions answers. This is the answers one, so is nested inside the questions one.
I need to pass the answer ID and next question from the dropdown into my sever side event.
Andy
 
0
 
LVL 8

Expert Comment

by:Faheem Shaikh
Comment Utility
1. repAnswers is the name of the repeater control right?
2. In my third post:

Dim ddl as DropDownList = Ctype(item.FindControl("ddlNextQuestion"),
DropDownList)
should be in the foreach loop.
3. I will have a look at how to get the repeater control in master page.
Thanks.
0
 
LVL 3

Author Comment

by:Andy Green
Comment Utility
1. repAnswers is the nested repeater inside repQuestion
2. yes is is inside the for each, I changed it to VB here is what I have
For Each item As RepeaterItem In repAnswers.Items
 Dim lblAnsId As Label = DirectCast(item.FindControl("lblhAnswer_ID"), Label)
 Response.Write(lblAnsId.Text)
Next Intellisense underline repAnswers saying its not declared, Its because its in a master page I think
0
 
LVL 3

Author Comment

by:Andy Green
Comment Utility
You helped me yesterday, this code works in a master page

txtAnswer = CType(Page.Master.FindControl("ctl00$cph_Main$txtAnswer" & a), TextBox)
Andy
0
 
LVL 3

Author Comment

by:Andy Green
Comment Utility
Hi All
This is urgent now, can anyone help.
I've got this far this will give me a handle on the outer Repeater, but I cant get any further,

Dim key As Control = CType(Page.Master.FindControl("cph_Main$repQuestions"), Repeater)
I still needdto get the values of lblhAnswer_ID and ddlNextQuestion from the answers (nested) repeater.
To reiterate
MasterPage
Content placeholder
Question Repeater Contol
Answer Repeater Control
lblhAnswer_ID and ddlNextQuestion  I need these values
Andy
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
How may I help?
0
 
LVL 3

Author Comment

by:Andy Green
Comment Utility

Hi  this is a copy of a prevoius post , This is what I'm trying to do:


I've got this far this will give me a handle on the outer Repeater, but I cant get any further,

Dim key As Control = CType(Page.Master.FindControl("cph_Main$repQuestions"), Repeater)
I still needdto get the values of lblhAnswer_ID and ddlNextQuestion from the answers (nested) repeater.

To reiterate
MasterPage
Content placeholder
Question Repeater Contol
Answer Repeater Control
lblhAnswer_ID and ddlNextQuestion  I need these values

Andy
0
 
LVL 27

Expert Comment

by:nmarun
Comment Utility
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
If you view the rendered page, how does the control's unique ID get assigned (View Source from browser)?
0
 
LVL 3

Author Comment

by:Andy Green
Comment Utility
This is the id of one of the dropdowns
id="ctl00_cph_Main_repQuestions_ctl01_repAnswers_ctl01_ddlNextQuestion"
Andy
0
 
LVL 3

Author Comment

by:Andy Green
Comment Utility

Dim form As Control = Master.FindControl("frmMaster")  - Finds OK
Dim content1 As Control = form.FindControl("cph_Main")  - Finds OK
Dim rep1 As Repeater = content1.FindControl("repQuestions")  - Finds OK
 
These return nothing so it cant find these on the page

Dim rep2 As Repeater = rep1.FindControl("repAnswers")  
Dim ddl As DropDownList = DirectCast(rep2.FindControl("ddlNextQuestion"), DropDownList)
Andy
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
I can see the HTML for "repAnswers" in the question body, but I can't see any reason why you can't find it.  Another approach is to use a recursive search approach.

Recursive Page.FindControl
http://www.codinghorror.com/blog/archives/000307.html
0
 
LVL 3

Author Comment

by:Andy Green
Comment Utility
OK Nearly there, I can get the dropdown list value with

Dim b As DropDownList = DirectCast(sender, DropDownList)
Dim value As String = b.SelectedValue
All I need now is the value of a label in the same repeater row.
id="ctl00_cph_Main_repQuestions_ctl01_repAnswers_ctl02_lblhAnswer_ID"
I need the id / value so I know which id the assign the new vaklue to.
Andy
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
In order to find controls in the Repeater, I would suggest getting the Repeater item, and then call FindControl from that.

0
 
LVL 3

Author Comment

by:Andy Green
Comment Utility
I've got what I want now, but it's not very elagent.
I'm assinging the css class to the answer id value,

asp:DropDownList ID="ddlNextQuestion" runat="server" DataSourceID="sdsNextQuestion" DataValueField="Question_ID" DataTextField="DisplayOrder" AutoPostBack="true" OnSelectedIndexChanged="UpdateNextQuestion" CssClass='<%#DataBinder.Eval(Container.DataItem, "Answer_ID")%>'></asp:DropDownList></td>
then on postback I'm getting the dropdown value as previous post and the ID like this

Dim ctl As DropDownList = DirectCast(sender, DropDownList)
Dim id As String = ctl.CssClass
Is there a better way that using the CSSClass?
 
Andy
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
Andy,

Where is this code being called?
0
 
LVL 3

Author Comment

by:Andy Green
Comment Utility
CssClass='<%#DataBinder.Eval(Container.DataItem, "Answer_ID")%>' this is in my aspx page and happens on databind, the dropdown has an onselected event change (
OnSelectedIndexChanged="UpdateNextQuestion") this extracts the values in my code behind (the control auto post backs)
This is my sub.

Protected Sub UpdateNextQuestion(ByVal sender As Object, ByVal e As System.EventArgs)

Dim x As DropDownList = DirectCast(sender, DropDownList)
Dim NextQuestion As String = x.SelectedValue

Dim y As DropDownList = DirectCast(sender, DropDownList)
Dim id As String = y.CssClass
 
 
End Sub
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
Andy,

I don't understand what you want from me.  What is that code doing and/or not doing that you need a "better" approach?
0
 
LVL 3

Author Comment

by:Andy Green
Comment Utility
Passing the row ID back through the cssclass property.
 
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
Yes, but what is "catching" the passed row ID?
0
 
LVL 3

Author Comment

by:Andy Green
Comment Utility
That is my problem, I dont know how to get the row id from the repeater, I originally had a label control, but couldn't get a handle on it, hence my first post.
I've since got the id by assigning it to the css property of the dropdown list and reading it back through my onselect code.
It doesn't look right thats all, there must be a way to get my Answer id from either the row id (I know there are no rows in a repeater)  or from another control like say a label which I had.
I've got it working now, I was just after a more elegant way.
Andy
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
I didn't feel like reading through all those comments, and I didn't totally understand your original question, so I started asking questions to draw out answers.

Where do you need the row ID from the DropDownList?  Do you need to pass is as a parameter for an SqlDataSource?
0
 
LVL 3

Author Comment

by:Andy Green
Comment Utility
OK
I have 2 repeater controls the outer one is for questions, and the inner one for answers, and that fills the page. A requirement for the app is that if a user answers a particular question thay can branch off to other questions.
To do this each answer has a dropdown list containing the values of the other questions, when a user selects a value from the dropdown list I need to pass the value and the answerid, (these 2 values are passed to a data class to update the database)  when the page renders I'm placing the answer id into the css property of the dropdown so I can get its value on post back, but this method doesn't seem very good, and I was asking if there is another way.
It's all working now and the users seem happy, but I have this nagging feeling that I could have been a bit slicker in passing the id back.
Andy
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
Comment Utility
Andy,

I still don't completely understand your requirement, but a simple approach would be to store the answer ID into a ViewState variable, since using the CSS property for storage is clearly a violation in principal.  What I believe that you have described is a perfect example for using the view state for small storage requirements (not a DataSet with 10000 rows for example).
0
 
LVL 3

Author Comment

by:Andy Green
Comment Utility
Viewstate not an option I dont think, I dont want to store a value between postpacks, I want the answer ID from one of the lines the repeater puts out when a user makes a selection from a dropdown. If this were a gridview with an embedded dropdown I could get the id from the gridview row.
I wouldn't take any more time over this, its working, so i have time to investigate further.
Thanks
Andy
0
 
LVL 3

Author Closing Comment

by:Andy Green
Comment Utility
Thanks again I appreciate your time
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now