Add event onserverclick when a load usercontrol - asp.net

So I searched but could not solve my problem.
please help.The user control is loaded. But onserverclick event does not work.
WebFormTest.aspx
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="WebFormTest.aspx.vb" Inherits="MS2.WebFormTest" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%=MS2.CLoadUC.RenderUserControl("/Site/testUC.ascx")%>
</div>
</form>
</body>
</html>
testUC.ascx
<%# Control Language="vb" AutoEventWireup="false" CodeBehind="~/testUC.ascx.vb" Inherits="MS2.testUC" %>
<script runat="server">
Protected Sub Button1_Click(sender As Object, e As EventArgs) 'Handles Button1.ServerClick
Dim a = "test"
End Sub
</script>
<button ID="Button1" runat="server" onserverclick="Button1_Click">Button</button>
CLoadUC.RenderUserControl("/Site/testUC.ascx")
Imports System.IO
Imports System.Reflection
Public Class CLoadUC
Public Shared Function RenderUserControl(path As String) As String
Dim pageHolder As New Page
Dim viewControl As UserControl = DirectCast(pageHolder.LoadControl(path), UserControl)
Dim viewControlType As System.Type = viewControl.GetType()
'Return viewControl
pageHolder.Controls.Add(viewControl)
Dim output As New StringWriter()
HttpContext.Current.Server.Execute(pageHolder, output, False)
Return output.ToString()
End Function
End Class

Related

Handles clause requires a WithEvents Callback

Error in CallbackUpdateSchema.Callback
BC30506 Visual Basic AND ASP.net Handles clause requires a WithEvents variable defined in the containing type or one of its base types. Callback
Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering
Imports DevExpress.Xpo.DB
Public Class UpdateSchema
Inherits System.Web.UI.Page
Dim uow As UnitOfWork
Private Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init
uow = XpoHelper.GetNewUnitOfWork
End Sub
Protected Sub CallbackUpdateSchema_Callback(source As Object, e As DevExpress.Web.CallbackEventArgs) Handles CallbackUpdateSchema.Callback
uow.UpdateSchema()
uow.CreateObjectTypeRecords()
End Sub
End Class
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="UpdateSchema.aspx.vb" %>
<%# Register assembly="DevExpress.Xpo.v18.2, Version=18.2.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Xpo" tagprefix="dx" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<dx:ASPxButton ID="ASPxButtonUpdateSchema" runat="server" AutoPostBack="False" Text="Update Schema">
<ClientSideEvents Click="function(s, e) {CallbackUpdateSchema.PerformCallback();}" />
</dx:ASPxButton>
<dx:ASPxCallback ID="CallbackUpdateSchema" runat="server" ClientInstanceName="CallbackUpdateSchema">
</dx:ASPxCallback>
</div>
</form>
</body>
</html>
Use the overridable method OnInit
https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.page.oninit?view=netframework-4.8#System_Web_UI_Page_OnInit_System_EventArgs_
Events are meant for users. For sub-classing, you should use overridable methods instead.
Protected Overrides Sub OnInit(e As EventArgs)
MyBase.OnInit()
uow = XpoHelper.GetNewUnitOfWork
End Sub

ASP.NET Public Variable at Code Behind is not accesible in .aspx

This is a simple example of what is happening here.
Default.aspx
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="default.aspx.vb" Inherits="base._default1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<% Response.Write(sName) %>
</div>
</form>
</body>
</html>
and the code behind default.aspx.vb
Public Class _default1
Inherits System.Web.UI.Page
Public sName As String = "Jimmy"
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
End Class
the error is
BC30451 'sName' is not declared. It may be inaccessible due to its protection level. base C:\Users\Jimmy\Documents\Visual Studio 2015\Projects\base\base\teste\default.aspx 12
Where is the problem ?
The biggest issue is your class _default1 is not marked as partial class.
It should rather be
Public partial Class _default1
Inherits System.Web.UI.Page
One more point is that your page directive has the property AutoEventWireup="false" and with that the way you have your page events mapped right now will not work. So set it to true rather AutoEventWireup="true"
Your #Page directive is total weird as can be seen below.
<%# Page AutoEventWireup="false" CodeBehind="default.aspx.vb" Inherits="base._default1" %>
To summarize; below are the issues you should take care off
mark the class partial
set AutoEventWireup="true"
change CodeBehind property to CodeBehind="default1.aspx.vb"
Change your Inherits property to Inherits="your_namespace._default1"

vbCode is autocomplete is not working

I am new to VB.net I want get list of names from my DB with auto complete. I am trying to follow the following example.
But My problem is it not working and I was not getting any error can any one tell me where I am doing wrong.
My home.aspx
<%# Page Title="Home Page" Language="VB" CodeFile="~/home.aspx.vb" AutoEventWireup="true" Inherits="_home"%>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body>
<form id="form1" runat="server">
<asp:ToolkitScriptManager ID="ScripManager1" runat="server"/>
<asp:UpdatePanel ID="autoupdate" runat="server">
<ContentTemplate>
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
<asp:AutoCompleteExtender ID="AutoCompleteExtender2" runat="server"
TargetControlID="txtSearch" ServiceMethod="GetList" MinimumPrefixLength="3"
UseContextKey="True" >
</asp:AutoCompleteExtender>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
This is my home.aspx.vb
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Threading
Imports System.Web.Services
Partial Class _Default
Inherits System.Web.UI.Page
<WebMethod()> _
Public Shared Function GetCompletionList(ByVal prefixText As String, ByVal count As Integer, ByVal contextKey As String) As String()
Try
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("Test").ConnectionString)
con.Open()
Dim cmd As New SqlCommand("select LoginName from users where LoginName like '#Name' +'%' ", con)
cmd.Parameters.AddWithValue("#Name", prefixText)
'Dim da As New SqlDataAdapter(cmd)
'Dim dt As New DataTable()
'da.Fill(dt)
'Dim InviteSearchListresult As New List(Of String)()
'For i As Integer = 1 To dt.Rows.Count
' InviteSearchListresult.Add(dt.Rows(i)(1).ToString())
' Next
Dim result As New List(Of String)()
Dim dr As SqlDataReader = cmd.ExecuteReader()
While dr.Read()
result.Add(dr("LoginName").ToString())
End While
Return (
From m In result
Where m.StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase)
Select m).Take(count).ToArray()
Catch ex As Exception
End Try
End Function
End Class
Please help how to solve this issuse.
Your service method is GetList but your actual method name is GetCompletionList.
Try getting the method names to match up and see if that's the problem. You're trying to call a method that doesn't exist.

Nesting ASP.NET user controls programmatically

I have a user control that programmatically includes a second user control several times, but the end result is that no HTML is generated for the controls at all.
Default.aspx
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="TestApplication._Default" %>
<%# Register TagName="Ten" TagPrefix="me" Src="~/Ten.ascx" %>
<html>
<head runat="server"></head>
<body>
<form id="form1" runat="server">
<me:Ten ID="thisTen" runat="server" />
</form>
</body>
</html>
Ten.ascx
<%# Control Language="vb" AutoEventWireup="false" CodeBehind="Ten.ascx.vb" Inherits="TestApplication.Ten" %>
<asp:Panel ID="List" runat="server"></asp:Panel>
Ten.ascx.vb
Public Class Ten
Inherits System.Web.UI.UserControl
Protected Sub Page_Init() Handles Me.Init
For I As Integer = 0 To 11
List.Controls.Add(New One(I.ToString))
Next
End Sub
End Class
One.ascx
<%# Control Language="vb" AutoEventWireup="false" CodeBehind="One.ascx.vb" Inherits="TestApplication.One" %>
<asp:Button ID="OneButton" Text="Press ME!" runat="server" />
One.ascx.vb
Public Class One
Inherits System.Web.UI.UserControl
Private _number As String
Sub New(ByVal number As String)
_number = number
End Sub
Protected Sub OneButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles OneButton.Click
Dim script As String = "<script type=""text/javascript"">" +
"alert('Button " + _number + "');" +
"</script>"
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "ServerControlScript", script, True)
End Sub
End Class
Edit:
Using load control (Ten.aspx)
Dim p() As String = {I.ToString}
Dim o As One = Me.LoadControl(New One("").GetType, p)
List.Controls.Add(o)
Edit 2:
Dim o As One = Me.LoadControl("~/One.ascx")
o._number = I.ToString
List.Controls.Add(o)
I wouldn't perform this operation in the OnInit event, but this may or may not be related to your issue. Instead, I would load the controls in the OnLoad event.
However, using the new keyword is not typically how user controls are loaded. Sorry, but I only know C# and don't know the exact translation:
In C#:
One one = (One)this.LoadControl("~/Controls/One.ascx");
Does this look right in VB.NET?
Dim one As One = Me.LoadControl("~/Controls/One.ascx")
You'll likely have to delete the constructor and just set the property after you load the control.

calling vb pagemethod from ajax

Hi
I have a simple aspx file with 2 text boxes and an ajax autocomplete extender attached to textbox2
<%# Page Language="VB" AutoEventWireup="false" CodeFile="test4.aspx.vb" Inherits="test4" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<form id="form1" runat="server">
<div id="content">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:TextBox ID="TextBox1" runat="server">
</asp:TextBox><br />
<asp:TextBox ID="TextBox2" runat="server">
</asp:TextBox>
</div>
<asp:AutoCompleteExtender ID="load_textBox2" TargetControlID="TextBox2" ServiceMethod="GetModelName"
UseContextKey="True" runat="server">
</asp:AutoCompleteExtender>
</form>
</body>
</html>
What i am trying to do is to call the pagemethod "GetModelName" form the aspx.vb to fillup the textbox2 with the relevent data
This is the aspx.vb code
Imports System.Web.Services
Partial Class test4
Inherits System.Web.UI.Page
Dim Model_Name_old As String()()
Dim mod_code As String()
Dim mod_name As String()
Dim cod_upper As Integer
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
//calling webservice that retunrs a jagged array
Dim ins As New localhost_insert_model.dbModel
Model_Name_old = ins.get_Model_Name("A")
mod_code = Model_Name_old(0)
mod_name = Model_Name_old(1)
cod_upper = Model_Name_old(0).GetUpperBound(0)
End Sub
<WebMethod()>
Public Function GetModelName() As String()
Return mod_name
End Function
End Class
This not working.. How can i make it work???.
Your function should be shared:
<WebMethod()>
Public Shared Function GetModelName() As String()
Return mod_name
End Function
Check that EnablePageMethods="true" in the script manager tag.

Resources