Put value from DataList on user control to parent page?

Discussion in 'ASP .Net' started by Mary Kerrigan, Oct 30, 2003.

  1. 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, Oct 30, 2003
    #1
    1. Advertising

  2. "Mary Kerrigan" <> wrote in message
    news:...
    > 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
     
    John Saunders, Oct 30, 2003
    #2
    1. Advertising

  3. 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:<OfK$>...
    > "Mary Kerrigan" <> wrote in message
    > news:...
    > > 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.
     
    Mary Kerrigan, Oct 31, 2003
    #3
  4. 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




    (Mary Kerrigan) wrote in message news:<>...
    > 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:<OfK$>...
    > > "Mary Kerrigan" <> wrote in message
    > > news:...
    > > > 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.
     
    Mary Kerrigan, Oct 31, 2003
    #4
  5. "Didn't work" how?

    --
    John

    "Mary Kerrigan" <> wrote in message
    news:...
    > 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
    >
    >
    >
    >
    > (Mary Kerrigan) wrote in message

    news:<>...
    > > 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:<OfK$>...
    > > > "Mary Kerrigan" <> wrote in message
    > > > news:...
    > > > > 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 Saunders, Oct 31, 2003
    #5
  6. 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#E0F#>...
    > "Didn't work" how?
    >
    > --
    > John
    >
    > "Mary Kerrigan" <> wrote in message
    > news:...
    > > 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
    > >
    > >
    > >
    > >
    > > (Mary Kerrigan) wrote in message

    > news:<>...
    > > > 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:<OfK$>...
    > > > > "Mary Kerrigan" <> wrote in message
    > > > > news:...
    > > > > > 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.
     
    Mary Kerrigan, Nov 1, 2003
    #6
  7. I have never found an answer to this problem... can anyone please help?


    (Mary Kerrigan) wrote in message news:<>...
    > 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#E0F#>...
    > > "Didn't work" how?
    > >
    > > --
    > > John
    > >
    > > "Mary Kerrigan" <> wrote in message
    > > news:...
    > > > 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
    > > >
    > > >
    > > >
    > > >
    > > > (Mary Kerrigan) wrote in message

    > news:<>...
    > > > > 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:<OfK$>...
    > > > > > "Mary Kerrigan" <> wrote in message
    > > > > > news:...
    > > > > > > 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.
     
    Mary Kerrigan, Nov 25, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nevyn Twyll
    Replies:
    8
    Views:
    1,939
  2. Replies:
    0
    Views:
    4,464
  3. Alan Silver
    Replies:
    3
    Views:
    446
    Alan Silver
    Aug 9, 2005
  4. schapopa
    Replies:
    1
    Views:
    580
    schapopa
    Jan 12, 2006
  5. Replies:
    3
    Views:
    8,804
    Eliyahu Goldin
    Sep 17, 2006
Loading...

Share This Page