如何使用母版页从 Web 用户控件获取标签值到内容页

发布于 2024-09-25 10:08:15 字数 663 浏览 1 评论 0原文

我有一个网络用户控件 book.ascx 和一个表单视图:

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

这个表单视图是动态数据绑定的。 现在我有一个内容页面 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>

我想从 Web 用户控件获取标签的值到 default.aspx 页面。 解决这个问题的最佳方法是什么。 谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝海似她心 2024-10-02 10:08:15

来自 Default.aspx.cs 后面的代码:

protected void fv_OnDataBound(object sender, EventArgs e) 
{
    Label fvLabel = (Label)fv.FindControl("bookID");
    lblBookId.Text = fvLabel.Text;
}

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;
}
臻嫒无言 2024-10-02 10:08:15

我想实现你想要的方法是让书籍控件在知道值是什么后触发一个事件。

您现在需要从页面访问控件内的值。这可以通过通过属性公开值来实现,或者您可以创建自己的 EventArgs 并引发事件。

public class StringEventArgs:EventArgs
{
  public String Value {get; private set;}  
  public StringEventArgs(String val){ this.Value = val; }
}

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; }
}
鲸落 2024-10-02 10:08:15

你想通过 javascript 在客户端获取它吗?

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

我还建议您获取 firefox 的 firebug,然后您可以查看生成的网页 html。您还可以单步执行并调试您的 JavaScript。

如果试图在服务器端找到它,请尝试这个。

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

如果这不起作用,你可以明白这个想法。继续深入下去,直到找到您要找的东西。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文