Andy Green
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"></HeaderTe mplate>
<ItemTemplate>
<tr>
<td class="answer" width="33%"><asp:label ID="lblhAnswer_ID" runat="Server" Text='<%#DataBinder.Eval(C ontainer.D ataItem, "Answer_ID")%>' Visible="true" ></asp:label><%#DataBinder .Eval(Cont ainer.Data Item, "Answer")%></td>
<td class="nextquestion" width="33%"><%#DataBinder. Eval(Conta iner.DataI tem,"NextQ uestion_ID ")%></td>
<td class="nextquestion" width="33%"><asp:DropDownL ist ID="ddlNextQuestion" runat="server" DataSourceID="sdsNextQuest ion" DataValueField="Question_I D" DataTextField="DisplayOrde r" AutoPostBack="true" OnSelectedIndexChanged="Up dateNextQu estion"></ asp:DropDo wnList></t d>
</tr>
</ItemTemplate>
<FooterTemplate></table></ FooterTemp late>
</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.FindCont rol("lblhA nswer_ID") , Label).Text
Dim NextQuestion = CType(Page.Master.FindCont rol("ddlNe xtQuestion "), DropDownList).SelectedValu e
And takin account of the master page tried this.
Dim key As String = CType(Page.Master.FindCont rol("ctl00 $cph_Main$ lblhAnswer _ID"), Label).Text
Dim NextQuestion = CType(Page.Master.FindCont rol("ctl00 $cph_Main$ ddlNextQue stion"), DropDownList).SelectedValu e
<asp:Repeater ID="repAnswers" runat="server" >
<HeaderTemplate><table width="100%" cellpadding="0" cellspacing="0"></HeaderTe
<ItemTemplate>
<tr>
<td class="answer" width="33%"><asp:label ID="lblhAnswer_ID" runat="Server" Text='<%#DataBinder.Eval(C
<td class="nextquestion" width="33%"><%#DataBinder.
<td class="nextquestion" width="33%"><asp:DropDownL
</tr>
</ItemTemplate>
<FooterTemplate></table></
</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.FindCont
Dim NextQuestion = CType(Page.Master.FindCont
And takin account of the master page tried this.
Dim key As String = CType(Page.Master.FindCont
Dim NextQuestion = CType(Page.Master.FindCont
foreach (RepeaterItem item in repAnswers.Items)
{
Label lblAnsId = (Label)item.FindControl("l blhAnswer_ ID");
Response.Write(lblAnsId.Te xt);
}
{
Label lblAnsId = (Label)item.FindControl("l
Response.Write(lblAnsId.Te
}
For VB.net:
Dim ddl as DropDownList = Ctype(item.FindControl("dd lNextQuest ion"),
DropDownList)
Dim ddl as DropDownList = Ctype(item.FindControl("dd
DropDownList)
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
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("dd lNextQuest ion"),
DropDownList)
should be in the foreach loop.
3. I will have a look at how to get the repeater control in master page.
Thanks.
2. In my third post:
Dim ddl as DropDownList = Ctype(item.FindControl("dd
DropDownList)
should be in the foreach loop.
3. I will have a look at how to get the repeater control in master page.
Thanks.
ASKER
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.FindContro l("lblhAns wer_ID"), Label)
Response.Write(lblAnsId.Te xt)
Next Intellisense underline repAnswers saying its not declared, Its because its in a master page I think
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.FindContro
Response.Write(lblAnsId.Te
Next Intellisense underline repAnswers saying its not declared, Its because its in a master page I think
ASKER
You helped me yesterday, this code works in a master page
txtAnswer = CType(Page.Master.FindCont rol("ctl00 $cph_Main$ txtAnswer" & a), TextBox)
Andy
txtAnswer = CType(Page.Master.FindCont
Andy
ASKER
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.FindCont rol("cph_M ain$repQue stions"), 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
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.FindCont
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
How may I help?
ASKER
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.FindCont
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)?
ASKER
This is the id of one of the dropdowns
id="ctl00_cph_Main_repQues tions_ctl0 1_repAnswe rs_ctl01_d dlNextQues tion"
Andy
id="ctl00_cph_Main_repQues
Andy
ASKER
Dim form As Control = Master.FindControl("frmMas
Dim content1 As Control = form.FindControl("cph_Main
Dim rep1 As Repeater = content1.FindControl("repQ
These return nothing so it cant find these on the page
Dim rep2 As Repeater = rep1.FindControl("repAnswe
Dim ddl As DropDownList = DirectCast(rep2.FindContro
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
Recursive Page.FindControl
http://www.codinghorror.com/blog/archives/000307.html
ASKER
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_repQues tions_ctl0 1_repAnswe rs_ctl02_l blhAnswer_ ID"
I need the id / value so I know which id the assign the new vaklue to.
Andy
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_repQues
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.
ASKER
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="sdsNextQuest ion" DataValueField="Question_I D" DataTextField="DisplayOrde r" AutoPostBack="true" OnSelectedIndexChanged="Up dateNextQu estion" CssClass='<%#DataBinder.Ev al(Contain er.DataIte m, "Answer_ID")%>'></asp:Drop DownList>< /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
I'm assinging the css class to the answer id value,
asp:DropDownList ID="ddlNextQuestion" runat="server" DataSourceID="sdsNextQuest
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?
Where is this code being called?
ASKER
CssClass='<%#DataBinder.Ev al(Contain er.DataIte m, "Answer_ID")%>' this is in my aspx page and happens on databind, the dropdown has an onselected event change (
OnSelectedIndexChanged="Up dateNextQu estion") 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
OnSelectedIndexChanged="Up
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?
I don't understand what you want from me. What is that code doing and/or not doing that you need a "better" approach?
ASKER
Passing the row ID back through the cssclass property.
Yes, but what is "catching" the passed row ID?
ASKER
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'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?
Where do you need the row ID from the DropDownList? Do you need to pass is as a parameter for an SqlDataSource?
ASKER
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
I wouldn't take any more time over this, its working, so i have time to investigate further.
Thanks
Andy
ASKER
Thanks again I appreciate your time
http://bytes.com/groups/ne