How to get a label value from web user control to a content page using master page

為{幸葍}努か 提交于 2019-12-20 06:27:01

问题


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.


回答1:


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;
}



回答2:


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; }
}



回答3:


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.



来源:https://stackoverflow.com/questions/3841639/how-to-get-a-label-value-from-web-user-control-to-a-content-page-using-master-pa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!