Solved

How to get Control values from repeater

Posted on 2009-04-03
30
451 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 15
  • 9
  • 4
  • +1
30 Comments
 
LVL 8

Expert Comment

by:Faheem Shaikh
ID: 24058674
0
 
LVL 8

Expert Comment

by:Faheem Shaikh
ID: 24058685
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
ID: 24058808
For VB.net:
Dim ddl as DropDownList = Ctype(item.FindControl("ddlNextQuestion"),
DropDownList)
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 3

Author Comment

by:Andy Green
ID: 24058925
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
ID: 24059026
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
ID: 24059052
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
ID: 24059060
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
ID: 24075242
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
ID: 24080510
How may I help?
0
 
LVL 3

Author Comment

by:Andy Green
ID: 24080962

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
ID: 24080976
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24083486
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
ID: 24085982
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
ID: 24086121

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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24086159
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
ID: 24086189
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
ID: 24086249
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
ID: 24086290
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
ID: 24086306
Andy,

Where is this code being called?
0
 
LVL 3

Author Comment

by:Andy Green
ID: 24086370
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
ID: 24086395
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
ID: 24086437
Passing the row ID back through the cssclass property.
 
0
 
LVL 96

Expert Comment

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

Author Comment

by:Andy Green
ID: 24086503
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
ID: 24089330
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
ID: 24094544
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
ID: 24097314
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
ID: 24097456
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
ID: 31566195
Thanks again I appreciate your time
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

737 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