Reputation: 177
The ASPX that I have is a partial that has a a master page on it and I would like to replace a textbox with new text.
I have a listbox that is created from the data base in the ASCX. I have a text box in the default.aspx page which I would like to change the test if the selected index has changed. I keep getting the error to delcare class, the class definiton for the defualt.aspx.vb is got a definition is below.
Partial Class _Default
Inherits System.Web.UI.Page
Code that sits on default.aspx.vb
Public Sub test(ByVal val As String)
lbl1LoginPage.Text = val
End Sub
VB ascx code to get the value of the selected index
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
Upvotes: 0
Views: 1347
Reputation: 62260
UserControl should not call Parent page. It is not a good design.
Instead, you want to bubble up the event from UserControl to the Parent page.
Here is the example -
<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
Upvotes: 0
Reputation: 4569
My suggestion is to use a bubble event:
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
this is all in C#!! here is your method in your aspx-page:
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 );
}
Upvotes: 1
Reputation: 782
You can create a Property in aspx page exposing the text box control say "TextBoxControl" then you can access it in you dropdownlist handler as shown below :
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
(I am not well versed with vb.net so syntax may be wrong at some places)
Upvotes: 1