By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,178 Members | 2,144 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,178 IT Pros & Developers. It's quick & easy.

Put value from DataList on user control to parent page?

P: n/a
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!
Nov 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Mary Kerrigan" <mk*******@ktoys.com> wrote in message
news:65**************************@posting.google.c om...
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.
--
John
Nov 17 '05 #2

P: n/a
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. ???

"John Saunders" <john.saunders at surfcontrol.com> wrote in message news:<Of**************@TK2MSFTNGP10.phx.gbl>...
"Mary Kerrigan" <mk*******@ktoys.com> wrote in message
news:65**************************@posting.google.c om...
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.

Nov 17 '05 #3

P: n/a
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


mk*******@ktoys.com (Mary Kerrigan) wrote in message news:<65**************************@posting.google. com>...
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. ???

"John Saunders" <john.saunders at surfcontrol.com> wrote in message news:<Of**************@TK2MSFTNGP10.phx.gbl>...
"Mary Kerrigan" <mk*******@ktoys.com> wrote in message
news:65**************************@posting.google.c om...
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.

Nov 17 '05 #4

P: n/a
"Didn't work" how?

--
John

"Mary Kerrigan" <mk*******@ktoys.com> wrote in message
news:65**************************@posting.google.c om...
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


mk*******@ktoys.com (Mary Kerrigan) wrote in message

news:<65**************************@posting.google. com>...
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. ???

"John Saunders" <john.saunders at surfcontrol.com> wrote in message news:<Of**************@TK2MSFTNGP10.phx.gbl>...
"Mary Kerrigan" <mk*******@ktoys.com> wrote in message
news:65**************************@posting.google.c om...
> 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.

Nov 17 '05 #5

P: n/a
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...

"John Saunders" <john.saunders at surfcontrol.com> wrote in message news:<O7**************@TK2MSFTNGP12.phx.gbl>...
"Didn't work" how?

--
John

"Mary Kerrigan" <mk*******@ktoys.com> wrote in message
news:65**************************@posting.google.c om...
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


mk*******@ktoys.com (Mary Kerrigan) wrote in message

news:<65**************************@posting.google. com>...
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. ???

"John Saunders" <john.saunders at surfcontrol.com> wrote in message news:<Of**************@TK2MSFTNGP10.phx.gbl>... > "Mary Kerrigan" <mk*******@ktoys.com> wrote in message
> news:65**************************@posting.google.c om...
> > 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.

Nov 17 '05 #6

P: n/a
I have never found an answer to this problem... can anyone please help?
mk*******@ktoys.com (Mary Kerrigan) wrote in message news:<65**************************@posting.google. com>...
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...

"John Saunders" <john.saunders at surfcontrol.com> wrote in message news:<O7**************@TK2MSFTNGP12.phx.gbl>...
"Didn't work" how?

--
John

"Mary Kerrigan" <mk*******@ktoys.com> wrote in message
news:65**************************@posting.google.c om...
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


mk*******@ktoys.com (Mary Kerrigan) wrote in message news:<65**************************@posting.google. com>... > 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. ???
>
> "John Saunders" <john.saunders at surfcontrol.com> wrote in message news:<Of**************@TK2MSFTNGP10.phx.gbl>... > > "Mary Kerrigan" <mk*******@ktoys.com> wrote in message
> > news:65**************************@posting.google.c om...
> > > 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.

Nov 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.