custom ASP.NET TextBox on textchanged event - asp.net

I want to let TextBox control TextChanged event fire only when there are more than one character in the TextBox.
Thank you.

Public Class ZTextBox
Inherits System.Windows.Forms.TextBox
Public Event ZTextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
'Add your custom paint code here
End Sub
Private Sub ZTextBox_TextChanged1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.TextChanged
If Me.Text.Length > 3 Then
RaiseEvent ZTextChanged(sender, e)
End If
End Sub End Class
and you can use the following in your form
Private Sub ZTextBox1_ZTextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ZTextBox1.ZTextChanged
MsgBox(1)
End Sub

I think you'll need a custom control that inherits the textbox control with a custom event, do your checks at the internal event, and fire your custom event when you see appropriate.

Related

Click event cannot be fired on buttons created dynamically in VB.Net ASP.Net

I'm creating a code in Page_PreRender function to dynamically creates some labels and buttons:
Dim btnExcludeDr As New Button()
btnExcludeDr.ID = "btnExcludeDr"
btnExcludeDr.Text = "Rate Driver"
form1.Controls.Add(btnExcludeDr)
AddHandler btnExcludeDr.Click, AddressOf Me.cmdExcludeDrv_Click
And the event which must be fired for each btnExcludeDr button is:
Protected Sub cmdExcludeDrv_Click(ByVal sender As Object, ByVal e As System.EventArgs)
MsgBox("hello")
End Sub
But the event is not fired. Do you have any solution? thank you !
The best place to create your dynamic controls is in the Page_Init function that the page code-behind class provides.
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
Dim btnExcludeDr As New Button()
btnExcludeDr.ID = "btnExcludeDr"
btnExcludeDr.Text = "Rate Driver"
form1.Controls.Add(btnExcludeDr)
AddHandler btnExcludeDr.Click, AddressOf Me.cmdExcludeDrv_Click
End Sub
Protected Sub cmdExcludeDrv_Click(ByVal sender As Object, ByVal e As System.EventArgs)
MsgBox("hello")
End Sub

creating event control in vb.net. Multiple controls with the same ID '1' were found

Im trying to create buttons on page load and have event controls to those. Im able to create the buttons but the event doesnt seem to be triggered when the button is clicked instead it throws an error stating multiple controls with id found.I think this has something to do with postback and unique ID creation for the buttons. can some one point me as to what to be added along with this?
Sub createbutton()
Dim but As New Button
but.Text = "save"
but.ID = "but"
AddHandler but.Click, AddressOf Button_Click
Me.form1.Controls.Add(but)
End Sub
The event control for this is as below.
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
' Handle your Button clicks here
MsgBox("done")
End Sub
Im getting the error
Multiple controls with the same ID '1' were found
The subroutine createbutton works on page load as follows.
Public Class Default3
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ' to gen page on load ;)
createbutton()
End Sub
Help is appreciated , Thanks :)
i think you are a guy who worked a lot with VB 6.0
Control array feature is available in VB 6.0
in VB.net everything is depend upon control ID.
if you don't have specific requirement like ID should be same then i suggest pls append prefix and incremental ID would resolve your issue.
Let me know if you have some specific requirements
Thanks
Asif
Corrected Code, Instead of ID it's name which allow uniqueness
Public Class Form1
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
' Handle your Button clicks here
MsgBox("done")
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
createbutton()
End Sub
Sub createbutton()
Dim but As New Button
but.Text = "save"
but.Name = "but"
AddHandler but.Click, AddressOf Button_Click
Me.Controls.Add(but)
End Sub
End Class

how to access textbox value from another webpage in asp.net

i want to display text box value to another text box from one webpage to another in a button click.
i know the windows code But don't know the web application.
public qus as form = new question()
qus.txtname=txtname.text
On first page add button click event
Sub Btn_Click(sender As Object, _
e As EventArgs)
Response.Redirect("SecondPage.aspx?id=" + txtname.text, false)
End Sub
on second page set your text on page load.
Private Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack
SecondPageTextBox.Text = Request.QueryString("textValue").ToString()
End If
End Sub
Use the query string to pass the text value to other page in the button click handler, like this:
Protected Sub Button1_Click(sender As Object, e As EventArgs)
Response.Redirect("OtherPage.aspx?textValue='Value from other page'")
End Sub
Now in the Page_Load of the other page, pull the query string value out and assign it to the text box, like this:
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Request.QueryString("textValue") IsNot Nothing Then
YourTextBox.Text = Request.QueryString("textValue").ToString()
End If
End Sub
You have to write code on first page
Protected Sub Button1_Click(sender As Object, e As EventArgs)
Response.Redirect("webpage2.aspx?textValue='value'")
End Sub
On second page
Private Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack
SecondPageTextBox.Text = Request.QueryString("textValue").ToString()
End If
End Sub

Page.Init and Page.PostBack

I know this is a very basic question, but I cannot find the answer. There are lots of web pages that say page.init does not fire on a post back e.g. here: http://www.dotnetfunda.com/interview/exclusive/x3224-what-is-the-difference-between-the-pageinit-and-pageload-events.aspx. Please see the code below:
Public Class _Default
Inherits System.Web.UI.Page
Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
MsgBox("Test Init") 'Line 5
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack = True Then
MsgBox("PostBack") 'line 9
End If
MsgBox("Test Load")
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = "Hello"
End Sub
End Class
The message box on line 5 and line 9 fire every time I click the button. This means that the Init event is fired on a postback. I have obviously forgotten something very basic.
From your reference page:
When you postback to any page, the Page_Init event doesn't fire.
This is totally wrong.
Page_Init is always fired - actually the page cycle is not change at all.

How do I create a base page event that fires after the derived page's load event?

How do I create a new event in a base page class that fires after all derived pages have fired their load events but before any controls fire their load events.
The following code fires the event before the derived page's load event. I want it to fire the event after the derived page's load event but before all control load events:
Base Class:
Public Event FirstLoad(ByVal sender As Object, ByVal e As System.EventArgs)
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
RaiseEvent FirstLoad(sender, e)
End If
End Sub
Derived Class:
Private Sub Page_FirstLoad(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.FirstLoad
'Stuff here happens before controls load but only on first page loads'
End Sub
I got this behavior to work properly by overriding the base page's OnLoad method and within that call the base's OnLoad method as per usual, then raise the event that you wish to fire after all of the loads have occurred.
Base Class:
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
If Not IsPostBack Then
RaiseEvent FirstLoad(sender, e)
End If
End Sub

Resources