我拥有的ASPX是一个部分,其上有一个母版页,我想用新文本替换文本框。
我有一个从ASCX中的数据库创建的列表框。我在default.aspx页面中有一个文本框,如果所选索引已更改,我想更改测试。我一直得到delcare类的错误,defualt.aspx.vb的类定义如下定义。
Partial Class _Default
Inherits System.Web.UI.Page
位于default.aspx.vb上的代码
Public Sub test(ByVal val As String)
lbl1LoginPage.Text = val
End Sub
VB ascx代码获取所选索引的值
Protected Sub ListBox3_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ListBox3.SelectedIndexChanged
Dim test As String = ListBox3.Text
Dim page As _Default = DirectCast(page, _Default)
page.test(test)
End Sub
答案 0 :(得分:1)
你可以在aspx页面中创建一个属性,公开文本框控件说" TextBoxControl"然后你可以在你的下拉列表处理程序中访问它,如下所示:
Protected Sub ListBox3_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ListBox3.SelectedIndexChanged
Dim test As String = ListBox3.Text
Dim page As _Default = DirectCast(Me.Page, _Default)
page.TextBoxControl.Text = "Some Text"
End Sub
(我对vb.net并不熟悉,所以在某些地方语法可能有问题)
答案 1 :(得分:1)
我的建议是使用气泡事件:
Protected Sub ListBox3_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ListBox3.SelectedIndexChanged
Dim test As String = ListBox3.Text
// this line is in C#. I don't know how it is in VB
RaiseBubbleEvent( this, new CommandEventArgs( "ListBoxText", test ) );
End Sub
这一切都在C#!!这是你的aspx页面中的方法:
protected override bool OnBubbleEvent( object source, EventArgs args )
{
// you can check in addition whether the source is of type of your user control
if ( args is CommandEventArgs )
{
lbl1LoginPage.Text = ((CommandEventArgs)args ).CommandArgument.ToString();
return true;
}
return base.OnBubbleEvent( source, args );
}
答案 2 :(得分:0)
UserControl 不应调用父页面。这不是一个好的设计。
相反,您希望冒泡从 UserControl 到父页面。
以下是示例 -
<asp:ListBox runat="server" ID="ListBox3"
OnSelectedIndexChanged="ListBox3_SelectedIndexChanged"
AutoPostBack="True">
<asp:ListItem>Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
</asp:ListBox>
Public Partial Class Child
Inherits System.Web.UI.UserControl
Public Event ListBox3SelectedIndexChanged As EventHandler
Protected Sub ListBox3_SelectedIndexChanged(sender As Object, e As EventArgs)
RaiseEvent ListBox3SelectedIndexChanged(sender, e)
End Sub
End Class
<%@ Register Src="~/Child.ascx" TagName="Child" TagPrefix="uc1" %>
...
<uc1:Child ID="Child1" runat="server"
OnListBox3SelectedIndexChanged="Child1_ListBox3SelectedIndexChanged" />
Protected Sub Child1_ListBox3SelectedIndexChanged(sender As Object,
e As EventArgs)
Dim listBox3 = TryCast(sender, ListBox)
If listBox3 IsNot Nothing Then
Dim selectedText As String = listBox3.SelectedItem.Text
End If
End Sub