Put value from DataList on user control to parent page?

  • Thread starter Thread starter Mary Kerrigan
  • Start date Start date
M

Mary Kerrigan

I have a user control (menu) with a data list:

<asp:DataList id="MyList" runat="server">
<ItemTemplate>
<asp:hyperlink
cssclass="MenuUnselected"
id="myLink1"
Text='<%# Container.DataItem("Subject") %>'
NavigateUrl='<%# "../productlist.aspx?SubjectID=" &
Container.DataItem("SubjectID") & "&selection=" & Container.ItemIndex
%>'
runat="server" />
</ItemTemplate>
<SelectedItemTemplate>
<asp:hyperlink
cssclass="MenuSelected"
id="myLink2"
Text='<%# Container.DataItem("Subject") %>'
NavigateUrl='<%# "../productlist.aspx?SubjectID=" &
Container.DataItem("SubjectID") & "&selection=" & Container.ItemIndex
%>'
runat="server" />
</SelectedItemTemplate>
</asp:DataList>

I want to take the value of the Text attribute (the data item:
"Subject") from the selected item and put it in a label on the parent
page. I have spent a day and a half trying this, so I decided to ask
if anyone out there can help me. Thanks!
 
Mary Kerrigan said:
I have a user control (menu) with a data list:

<asp:DataList id="MyList" runat="server">
<ItemTemplate>
<asp:hyperlink
cssclass="MenuUnselected"
id="myLink1"
Text='<%# Container.DataItem("Subject") %>'
NavigateUrl='<%# "../productlist.aspx?SubjectID=" &
Container.DataItem("SubjectID") & "&selection=" & Container.ItemIndex
%>'
runat="server" />
</ItemTemplate>
<SelectedItemTemplate>
<asp:hyperlink
cssclass="MenuSelected"
id="myLink2"
Text='<%# Container.DataItem("Subject") %>'
NavigateUrl='<%# "../productlist.aspx?SubjectID=" &
Container.DataItem("SubjectID") & "&selection=" & Container.ItemIndex
%>'
runat="server" />
</SelectedItemTemplate>
</asp:DataList>

I want to take the value of the Text attribute (the data item:
"Subject") from the selected item and put it in a label on the parent
page. I have spent a day and a half trying this, so I decided to ask
if anyone out there can help me. Thanks!

With ASP.NET, it's best to think a bit object-oriented.

So, why should your user control know anything about the page it happens to
be loaded into? In particular, why should it know that there's a label on
that page? Answer: it shouldn't.

On the other hand, the user control does know about the Text attribute of
the selected item. You could define a public property in the user control
which would expose that Text attribute so that any containing page (or other
control) can see the value of the Text attribute.

If the surrounding page needs to know when the selected index has changed
(so that it knows to fetch the Text property) then you can define a public
event in your user control and raise it when the user control receives the
SelectedIndexChanged event.
 
OK - I know how to assign a value to the public property, but what I
can't figure out is the syntax to retrieve the Text attribute's value
for the selected item. ???
 
Here's one thing I have tried which didn't work:

Private Sub MyList_SelectedIndexChanged(ByVal sender As Object, ByVal
e As System.EventArgs) Handles MyList.SelectedIndexChanged

Dim myListItem As DataListItem = CType(sender,
DataList).SelectedItem
Dim myHyperlink As HyperLink = myListItem.FindControl("Hyperlink2")
Dim myValue As String = myHyperlink.Text
lblTest.Text = myValue

End Sub
 
"Didn't work" how?

--
John

Mary Kerrigan said:
Here's one thing I have tried which didn't work:

Private Sub MyList_SelectedIndexChanged(ByVal sender As Object, ByVal
e As System.EventArgs) Handles MyList.SelectedIndexChanged

Dim myListItem As DataListItem = CType(sender,
DataList).SelectedItem
Dim myHyperlink As HyperLink = myListItem.FindControl("Hyperlink2")
Dim myValue As String = myHyperlink.Text
lblTest.Text = myValue

End Sub




(e-mail address removed) (Mary Kerrigan) wrote in message
 
Nothing happened - the label was blank instead of showing the value of
the Text attribute of Hyperlink2.

But now I think that's because this is in a user control. The
SelectedIndexChanged does not appear to fire at all. I think it's
because the user control does not postback???

It looks like I will have to go back to the database to get the value
I need when the page reloads... I didn't want to have to do this, you
would think I could get the value from the control, geez, it's right
there on the screen...
 
Back
Top