我需要将title绑定到数据源,以便显示的网页标题取决于数据源中的数据。
当我使用Formview和hiddenfield从数据源获取数据代码无法编译时
如何根据数据源中的数据创建可以显示pagetitle的脚本?
<Script runat = "server">
Protected sub Page_load(Byval sender as Object, Byval e As System.eventargs)
Title = Hiddenfield1.value
End sub
</Script>
<asp:FormView ID="FormView2" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server" Value= '<%#eval ("PageTitleConstruct") %>' />
</ItemTemplate>
</asp:FormView>
答案 0 :(得分:1)
试试这个
Sub FormView2_ItemCreated(ByVal sender As Object, ByVal e As EventArgs)
Dim HiddenField1 As HiddenField = CType(FormView2.FindControl("HiddenField1"), HiddenField)
Page.Title = HiddenField1.Value;
End Sub
答案 1 :(得分:0)
在Page_Load函数中。你必须比你更早得到这个值。
Page.Title = value here.
答案 2 :(得分:0)
您应该能够收听ItemCreated
事件,并在该回调中设置标题。
答案 3 :(得分:0)
我已尝试过该脚本,但在使用
时FormView2_ItemCreated(ByVal sender As Object, ByVal e As EventArgs)
Dim HiddenField1 As HiddenField = CType(FormView2.FindControl("HiddenField1"), HiddenField)
Page.Title = HiddenField1.Value;
End Sub
page.title值只有
但如果我将事件更改为
,请更正Private Sub FormView2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView2.Load
Dim hiddenfield1 As HiddenField = CType(FormView2.FindControl("hiddenfield1"), HiddenField)
Page.Title = hiddenfield1.Value
End Sub