Link to home
Start Free TrialLog in
Avatar of Andy Green
Andy GreenFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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
Avatar of Faheem Shaikh
Faheem Shaikh
Flag of India image

foreach (RepeaterItem item in repAnswers.Items)
{
Label lblAnsId = (Label)item.FindControl("lblhAnswer_ID");
Response.Write(lblAnsId.Text);
}
For VB.net:
Dim ddl as DropDownList = Ctype(item.FindControl("ddlNextQuestion"),
DropDownList)
Avatar of Andy Green

ASKER

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
 
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.
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
You helped me yesterday, this code works in a master page

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

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

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

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
Andy,

Where is this code being called?
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
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?
Passing the row ID back through the cssclass property.
 
Yes, but what is "catching" the passed row ID?
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
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?
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
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Thanks again I appreciate your time