0

I have a web user control book.ascx and a formview:

<formview runat="server" id="fv">
<ItemTemplate>
<asp:Label runat="server" id="bookID" Text='<%# Eval ("bookId") %>' />
</ItemTemplate>
</FormView>

This formview is databind dynamically. Now i have a Content page Default.aspx:

<%@ Register src="Book.ascx" tagname="Book" tagprefix="uc1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<uc1:Book id="book1" runat="server"/>
<asp:Label runat="server" id="lblBookId" />
</asp:Content>

I want to get the value of the label from web user control to a default.aspx page. Whats the best method to solve this issue. Thank You.

Michael Mior
  • 28,107
  • 9
  • 89
  • 113
sacrament
  • 63
  • 1
  • 2
  • 8

3 Answers3

2

From the code behind in the Default.aspx.cs:

protected void fv_OnDataBound(object sender, EventArgs e) 
{
    Label fvLabel = (Label)fv.FindControl("bookID");
    lblBookId.Text = fvLabel.Text;
}
jon3laze
  • 3,188
  • 6
  • 36
  • 69
  • Thank you Jon, but the formview it is binded in the control i am not binding in the default.aspx. – sacrament Oct 01 '10 at 17:52
  • It should work the same way as trying to reference MasterPage controls from a content page. Try using just these two lines of code in the Default.aspx.cs and see if that works for you. Label fvLabel = (Label)fv.FindControl("bookID"); lblBookId.Text = fvLabel.Text; – jon3laze Oct 01 '10 at 18:02
  • I tried but still the same problem Object reference not set to an instance of an object. – sacrament Oct 01 '10 at 18:08
  • Ok guys i figured out how: here it is: – sacrament Oct 01 '10 at 18:23
  • 1
    ContentPlaceHolder c = Page.Master.FindControl("MainContentPlaceHolder") as ContentPlaceHolder; UserControl usc = c.FindControl("book1") as UserControl; FormView fv = usc.FindControl("fv") as FormView; Label lbl = fv.FindControl("bookID") as Label; – sacrament Oct 01 '10 at 18:24
  • Thanks though for the advices – sacrament Oct 01 '10 at 18:25
  • @sacrament answer your own question and then accept it...you'll get a neat 'self learner' badge. or...just go down an accept my answer :) – jim Oct 01 '10 at 18:39
0

you wanted to get it on the client side via javascript?

getElementById('<%=lblBookId.ClientID%>')

I would also recommend getting firebug for firefox and then you can take a look at the generated html of the webpage. You'll also be able to step though and debug your javascript.

if trying to find this on the server side try this.

ContentPlaceHolder ph = Page.Master.FindControl("ContentPlaceHolder1");   
UserControl Uc = ph.Controls(0);
FormView fv = up.FindControl("fv");
Label label = fv.FindControl("lblBookId");
label.Text = "Hi there"; 

if this doesn't work, you can get the idea. keep drilling down until you find what you're looking for.

jim
  • 26,598
  • 13
  • 51
  • 66
0

I guess the way to achieve what you want is to let the book-control fire an event after it knows what the value is.

You now need to gain access from the page to the value inside the control. That can be achieved by exposing the value via a property or you can create your own EventArgs and throw an Event.

public class StringEventArgs:EventArgs
{
  public String Value {get; private set;}  
  public StringEventArgs(String val){ this.Value = val; }
}
citronas
  • 19,035
  • 27
  • 96
  • 164
  • I like your solution, but I think that sacrament is slightly more junior than your answer is geared to. Maybe walk though the entire process of setting up the event and then subscribing to it and I'd upvote you...hell, I'd tell my friends to upvote you, I'd have an Upvote citronas party Saturday night. :) – jim Oct 01 '10 at 18:41
  • I'm a little short of time right now but maybe I'll edit this answer this night to get an upvote and the votes from your party ;) – citronas Oct 01 '10 at 18:56
  • I'm going on a beer & chips run, I'll check back later. – jim Oct 01 '10 at 19:00