RiseEvent from usercontrol or communicate between usercontrols - asp.net

I have problem with bubbleup event from my dynamic loaded ascx control (katalogbooklist.ascx) to it´s parent control (ViewAJBarnboksKatalog.ascx)
i need to fire/run sub uppdateraAndraModuler in the parent control when the event addMultiVotes_command fires in the chiled control.
is there any one who knows or have an idea on how to do this?
/
Andreas
(the code lives in a dotnetnuke cms modules, if that's any help)
Partial Class ViewAJBarnboksKatalog '<--------Partial codebehind file for ViewAJBarnboksKatalog.ascx
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
.. code for adding for loading katalogbooklist.ascx dynamic...
Me.Controls.Add(..code.. add katalogbooklist.ascx ..) '
End Sub
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim t As Execkoden = New Execkoden()
AddHandler t.onAjeventtest, AddressOf Uppdateramod
End Sub
Public Sub Uppdateramod(sender As Object, e As ajeventArgs)
uppdateraAndraModuler()
End Sub
Public Sub uppdateraAndraModuler()
'..do some code
End Sub
End Class
Partial Class katalogenBookList '<--------Partial codebehind file for katalogbooklist.ascx
Protected Sub addMultiVotes_command(ByVal sender As Object, ByVal e As System.EventArgs)
'..more code...
Dim te As New Execkoden ' <----- i want to use the constructor to raise the event in class Execkoden can´t raiseevent directly it wont´t fire
'... more code...
End sub
End Class
Public Class ajeventArgs : Inherits EventArgs
Public Sub New()
End Sub
End Class
Public Delegate Sub Uppdatera(sender As Object, e As ajeventArgs)
Public Class Execkoden
Public Event onAjeventtest As Uppdatera
Public Sub New()
RaiseEvent onAjeventtest(Me, New ajeventArgs)
End Sub
End Class

Create an event handler in the child control, like this:
public event EventHandler DeleteButtonClick;
When a button is clicked in the child control, do this:
protected void DeleteClick(object sender, EventArgs e)
{
if (this.DeleteButtonClick != null)
this.DeleteButtonClick(sender, e);
}
And in your parent control, in the markup:
<UC:SomeUserControl ID="UserControl1" runat="server" OnDeleteButtonClick="UserControl1_DeleteClick" ...>
And in the code behind of the parent control:
protected void UserControl1_DeleteClick(object sender, EventArgs e)
{
//do something
}

I would suggest using the IMC or Inter Module Communication feature that's built in to DotNetNuke.

Related

vb.net CefSharp.Windows document.getElementById

We used webbrowser control to login website in VB.NET, now we are going to us CefSharp.
We have made a simple test form. Clicking on the button sets the "toegangscode". It works, but when I go to next field wachtwoord (password) and type 1 first character the "toegangscode" field will be made empty. What I am doing wrong ?
Public Class Form1
Public WithEvents browser As ChromiumWebBrowser
Public Sub New()
' This call is required by the designer.
InitializeComponent()
Dim settings As New CefSharp.WinForms.CefSettings()
CefSharp.Cef.Initialize(settings)
browser = New CefSharp.WinForms.ChromiumWebBrowser("https://diensten.kvk.nl/inloggen.html") With {
.Dock = DockStyle.Fill}
Panel1.Controls.Add(browser)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim jsScript1 = <js><![CDATA[document.getElementById("username").focus();]]></js>.Value
browser.ExecuteScriptAsync(jsScript1)
Dim jsScript2 As String = <js><![CDATA[document.getElementById("username").value = "xxxxxx";]]></js>.Value
browser.ExecuteScriptAsync(jsScript2)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim bCanExecuteJavascriptInMainFrame As Boolean = browser.CanExecuteJavascriptInMainFrame
Dim bIsBrowserInitialized As Boolean = browser.IsBrowserInitialized
If bIsBrowserInitialized Then
browser.ShowDevTools()
End If
End Sub
End Class

How to fix the error "Object reference not set to an instance of an object"?

In this program (VB, ASP.NET 2010) I create three fields: accno, name and balance, and the following buttons: create, destroy, set and get.
But while clicking on set or get method it gives the following exception: object reference not set to an instance of an object
Default.aspx.vb
Partial Class _Default
Inherits System.Web.UI.Page
Dim obj As account 'declaring the obj of class account
Protected Sub btn_create_Click(sender As Object, e As System.EventArgs) Handles btn_create.Click
obj = New account 'initializing the object obj on class accounts
End Sub
Protected Sub btn_set_Click(sender As Object, e As System.EventArgs) Handles btn_set.Click
'sending the values from textboxes to accounts class through method setdata
Try
obj.setdata(CInt(txt_accno.Text), (txt_name.Text), CInt(txt_bal.Text))
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Protected Sub btn_get_Click(sender As Object, e As System.EventArgs) Handles btn_get.Click
'calling the method getdata to view the output
Try
obj.getdata()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Protected Sub btn_destroy_Click(sender As Object, e As System.EventArgs) Handles btn_destroy.Click
'calling the constructor
obj = Nothing
End Sub
End Class
Account.vb
Imports Microsoft.VisualBasic
Public Class account
Private accno As Integer
Private acc_name As String
Private bal As Integer
'constructor
Public Sub New()
MsgBox("object created")
End Sub
'public method to populate above three private variable
Public Sub setdata(ByVal a As Integer, ByVal b As String, ByVal c As Integer)
Me.accno = a
Me.acc_name = b
Me.bal = c
End Sub
Public Sub getdata()
MsgBox(Me.accno.ToString + vbNewLine + Me.acc_name + vbNewLine + Me.bal.ToString)
End Sub
'destructor
Protected Overrides Sub finalize()
MsgBox("object destroyed")
End Sub
End Class
You have use New Keyword for Initializing Objects...
Dim obj As NEW account
Default.aspx.vb Partial Class _Default Inherits System.Web.UI.Page
'declaring the obj of class account
Dim obj As NEW account
Protected Sub btn_create_Click(sender As Object, e As System.EventArgs) Handles btn_create.Click
'initializing the object obj on class accounts
obj = New account
End Sub
Protected Sub btn_set_Click(sender As Object, e As System.EventArgs) Handles btn_set.Click
'sending the values from textboxes to accounts class through method setdata
Try
obj.setdata(CInt(txt_accno.Text), (txt_name.Text), CInt(txt_bal.Text))
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Protected Sub btn_get_Click(sender As Object, e As System.EventArgs) Handles btn_get.Click
'calling the method getdata to view the output
Try
obj.getdata()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Protected Sub btn_destroy_Click(sender As Object, e As System.EventArgs) Handles btn_destroy.Click
'calling the constructor
obj = Nothing
End Sub
End Class
Please mark it as answer if it's working.
use below code. You need to initialize in page load method and need to check is postback property.
Default.aspx.vb Partial Class _Default Inherits System.Web.UI.Page
'declaring the obj of class account
Dim obj As account
Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs)
if not ispostback then
obj = New account
end
End Sub
Protected Sub btn_create_Click(sender As Object, e As System.EventArgs) Handles btn_create.Click
'initializing the object obj on class accounts
obj = New account
End Sub
Protected Sub btn_set_Click(sender As Object, e As System.EventArgs) Handles btn_set.Click
'sending the values from textboxes to accounts class through method setdata
Try
obj.setdata(CInt(txt_accno.Text), (txt_name.Text), CInt(txt_bal.Text))
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Protected Sub btn_get_Click(sender As Object, e As System.EventArgs) Handles btn_get.Click
'calling the method getdata to view the output
Try
obj.getdata()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Protected Sub btn_destroy_Click(sender As Object, e As System.EventArgs) Handles btn_destroy.Click
'calling the constructor
obj = Nothing
End Sub
End Class

asp.net server control not saving state properly

I am trying to create an ASP.net server control that uses controls from the AjaxControlToolkit. Essentially, the control is a dropdownlist and a button. When the user clicks the button, a modal dialog (modalPopupExtender) opens. The dialog has a checkboxlist with a list of options to chose from. When the user selects their choice(s) and hits the Ok button, in the dialog, the dialog closes and the selected choices should get added to the dropdownlist. What is happening, though, is that when the user makes their selection and hits OK only the last item in the Checkboxlist gets added. I think it may have something to do with the inherited functions I override. Can anyone tell me what I may be doing wrong? Here is the relevant code:
<DefaultProperty("Text"), ToolboxData("<{0}:myServerControl runat=server>
</{0}:myServerControl >")> _
Public Class myServerControl
Inherits CompositeControl
Implements INamingContainer, IPostBackEventHandler
Private myDropDownListAs New DropDownList
Private addPlant As New Button
Private _updatePanel As UpdatePanel
Private _modalExtenderOne As AjaxControlToolkit.ModalPopupExtender
Private lblWarning As Label
Private cb1 As New CheckBoxList
Private btnSavePlants As New Button
Private btnCancel As New Button
Protected Overrides Function SaveViewState() As Object
Return New Pair(MyBase.SaveViewState(), Nothing)
End Function
Protected Overrides Sub LoadViewState(ByVal savedState As Object)
MyBase.LoadViewState(CType(savedState, Pair).First)
EnsureChildControls()
End Sub
Protected Overrides Sub CreateChildControls()
createDynamicControls()
MyBase.CreateChildControls()
End Sub
Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
MyBase.OnInit(e)
End Sub
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
End Sub
Private Sub createDynamicControls()
Controls.Clear()
AddHandler btnSavePlants.Click, AddressOf saveItems_Click
_updatePanel = New UpdatePanel
Dim myTable As New Table
myTable = buildControl() ' a funciton that returns a built table
Dim myPanel As New Panel
myPanel = buildDialog() 'a function taht returns a build modal dialog
_updatePanel.ContentTemplateContainer.Controls.Add(myTable)
_updatePanel.ContentTemplateContainer.Controls.Add(myPanel)
Me.Controls.Add(_updatePanel)
End Sub
Protected Sub saveItems_Click(ByVal sender As Object, ByVal e As EventArgs)
Me.myDropDownList.Items.Clear()
Try
For Each i As ListItem In Me.cb1.Items
If (i.Selected = True) Then
Dim newLi As New ListItem
With newLi
.Selected = False
.Text = i.Text
.Value = i.Value
End With
Me.myDropDownList.Items.Add(newLi)
Else
End If
Next
Catch ex As Exception
End Try
End Sub
Public Sub bindSelections(ByVal myList As List(Of myObject))
For Each p As myObjectIn myList
Dim newLI As New ListItem
newLI.Value = p.EquipmentPK
newLI.Text = p.EquipmentID
Me.cb1.Items.Add(newLI)
Next
End Sub
Public Sub RaisePostBackEvent(ByVal eventArgument As String) Implements System.Web.UI.IPostBackEventHandler.RaisePostBackEvent
End Sub
End Class

Adding multile row in the girdview

Adding the row one by one in the button click..
(i tried but its overwirting the same row)
Here it is in vb for you. Tested it, seems to work fine. Declare dt as shared
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Shared dt As DataTable
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
dt = New DataTable
dt.Columns.Add("Colum1")
dt.Columns.Add("Colum2")
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
dt.Rows.Add(TextBox1.Text, TextBox2.Text)
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
End Class
A gridview should be bound to a datasource. You'll need to add a record to this datasource and then call myGridView.Bind() to bind to it.
Lots of info on MSDN about GridViews:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.aspx
You can do something like this...
static DataTable dt;
private void Form1_Load(object sender, EventArgs e)
{
dt = new DataTable();
dt.Columns.Add("Colum1");
dt.Columns.Add("Colum2");
}
private void button1_Click(object sender, EventArgs e)
{
dt.Rows.Add(textBox1.Text, textBox2.Text);
dataGridView1.DataSource = dt;
}

Page.Load issue in ASP.NET 2.0

I am trying to aid another programmer with a page called Default.aspx with a code-behind section, and unfortunately I am at a bit of a loss.
Partial Class _Default
Inherits OverheadClass
'A bunch of global variables here'
Private Sub page_load(ByVal sender As Object, ByVal e As System.Eventarts) Handles Me.Load
'Function goes here'
And in the OverheadClass we have
Public Sub Sub_OverheadClass_Load(ByVal sender As Object, ByVal e as System.EventArgs) Handles MyClass.Load
The desired effect is when the OverheadClass is inherited, we want its load to run before the load event on the page runs. There is probably a very simple answer to this that I am missing.
Edit: I forgot to note that we write in VB, and not C# as many of you are used to for ASP.
You should be able to override the OnLoad and call the base class's OnLoad first, then your class, for example:
C# Version
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
// Do some stuff here
}
VB Version
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
' Do some stuff here
End Sub
In VB it would be:
Private Sub page_load(ByVal sender As Object, ByVal e As System.Eventarts) Handles Me.Load
Mybase.Sub_OverheadClass_Load(e)
End Sub
Your default page should inherit OverheadClass
Partial Public Class _Default
Inherits OverheadClass
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Do some page stuff'
End Sub
End Class
And OverheadClass should inherit System.Web.UI.Page
Public Class OverheadClass
Inherits System.Web.UI.Page
Public Sub Sub_OverheadClass_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyClass.Load
'Do some base stuff'
End Sub
End Class
Partial Class OverheadClass
Inherits System.Web.UI.Page
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
End Sub
End Class
Partial Class _Default
Inherits OverheadClass
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
End Sub
End Class

Resources