• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 467
  • Last Modified:

How to get Control values from repeater

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
Andy Green
Asked:
Andy Green
  • 15
  • 9
  • 4
  • +1
1 Solution
 
Faheem ShaikhCommented:
foreach (RepeaterItem item in repAnswers.Items)
{
Label lblAnsId = (Label)item.FindControl("lblhAnswer_ID");
Response.Write(lblAnsId.Text);
}
0
 
Faheem ShaikhCommented:
For VB.net:
Dim ddl as DropDownList = Ctype(item.FindControl("ddlNextQuestion"),
DropDownList)
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
Andy GreenAuthor Commented:
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
 
Faheem ShaikhCommented:
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
 
Andy GreenAuthor Commented:
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
 
Andy GreenAuthor Commented:
You helped me yesterday, this code works in a master page

txtAnswer = CType(Page.Master.FindControl("ctl00$cph_Main$txtAnswer" & a), TextBox)
Andy
0
 
Andy GreenAuthor Commented:
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
 
Bob LearnedCommented:
How may I help?
0
 
Andy GreenAuthor Commented:

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
 
Bob LearnedCommented:
If you view the rendered page, how does the control's unique ID get assigned (View Source from browser)?
0
 
Andy GreenAuthor Commented:
This is the id of one of the dropdowns
id="ctl00_cph_Main_repQuestions_ctl01_repAnswers_ctl01_ddlNextQuestion"
Andy
0
 
Andy GreenAuthor Commented:

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
 
Bob LearnedCommented:
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
 
Andy GreenAuthor Commented:
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
 
Bob LearnedCommented:
In order to find controls in the Repeater, I would suggest getting the Repeater item, and then call FindControl from that.

0
 
Andy GreenAuthor Commented:
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
 
Bob LearnedCommented:
Andy,

Where is this code being called?
0
 
Andy GreenAuthor Commented:
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
 
Bob LearnedCommented:
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
 
Andy GreenAuthor Commented:
Passing the row ID back through the cssclass property.
 
0
 
Bob LearnedCommented:
Yes, but what is "catching" the passed row ID?
0
 
Andy GreenAuthor Commented:
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
 
Bob LearnedCommented:
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
 
Andy GreenAuthor Commented:
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
 
Bob LearnedCommented:
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
 
Andy GreenAuthor Commented:
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
 
Andy GreenAuthor Commented:
Thanks again I appreciate your time
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 15
  • 9
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now