VB.net textbox return hit(enter) event - asp.net

How to catch return hit (enter) on TextBox?
The following does not work. I am keep getting error "KeyPress event can't be found".
Private Sub TextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox.KeyPress
If TextBox.Text Is Nothing Then Return
If TextBox.Text.Length = 6 And Asc(e.KeyChar) = 13 Then
' Do something here
End If
End Sub

try deleting the text box and re-entering the code
If TextBox.Text Is Nothing Then Return
If TextBox.Text.Length = 6 And Asc(e.KeyChar) = 13 Then
' Do something here
End If
as that should work - you most likely have something messed up with the textbox name or keypress declaration code.
I just checked that code in a new text box and it worked fine.

The following code does work:
Private Sub TextBox_KeyPress(ByVal KeyAscii As Integer)
If TextBox.Text Is Nothing Then Return
If TextBox.Text.Length = 6 And KeyAscii = 13 Then
' Do something here
End If
End Sub

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Convert.ToChar(13) Then
If TextBox1.Text.Trim <> Nothing Then
'Do Something here
End If
End If

Related

VB.NET Webform DetailsView - Loop Through Updating field names

I have this code below in VB.NET and it works fine, which is looping through updating input elements clean it with HtmlEncode
Protected Sub DetailsView1_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdateEventArgs) Handles data_DetailsView1.ItemUpdating
For i As Integer = 0 To e.NewValues.Count - 1
If e.NewValues(i) IsNot Nothing Then
e.NewValues(i) = Server.HtmlEncode(e.NewValues(i).ToString())
End If
Next
End Sub
The above code loop through every inputs in the DetailsView. I want to skip two inputs (textboxes) name "Desc1" and "Desc2". I couldn't figure out how to get the input name from the element. Please help me correct the below code:
Protected Sub DetailsView1_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdateEventArgs) Handles data_DetailsView1.ItemUpdating
For i As Integer = 0 To e.NewValues.Count - 1
If e.NewValues(i) IsNot Nothing Then
If e.Name <> "Desc1" OR e.Name <> "Desc2" Then 'this is not working, but just the pseudo code.
e.NewValues(i) = Server.HtmlEncode(e.NewValues(i).ToString())
End If
End If
Next
End Sub
Thanks in advance,

a method to prevent duplicate entry in multiline textbox

So I try to write a a code to prevent duplicated entry from register at all. So I've tried this but it doesn't work like how I want it to.
Private Sub NoDuplicate1()
For Each line As String In Me.TxtResult4.Text.Split(vbLf)
If line = TxtResult4.Text Then
LblMsg.Text = ""
TxtResult4.Text = TxtResult4.Text.Remove(TxtResult4.Text.LastIndexOf(Environment.NewLine))
End If
Next
End Sub
I've also try this code and put in postback and it work:
If Not IsPostBack Then
TxtResult3.Text = String.Join(Environment.NewLine,
TxtResult3.Text.Split({Environment.NewLine}, StringSplitOptions.None).Distinct())
TxtResult4.Text = String.Join(Environment.NewLine,
TxtResult4.Text.Split({Environment.NewLine}, StringSplitOptions.None).Distinct())
End If
but the problem that I have with the code is that the duplicate data only deleted when the page refresh. when what I want to do is to block/prevent the duplicate data from being enter at all . Is there any suggestion on how I can modified my code ?
This should remove any duplicate lines in the TextBox.
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim splitter() As String = {Environment.NewLine}
Dim lines() As String = TextBox1.Text.Split(splitter, StringSplitOptions.RemoveEmptyEntries)
Dim dupFree = lines.Distinct.ToArray
TextBox1.Text = Join(dupFree, Environment.NewLine)
End Sub

How to match the answer in vb.net and asp.net?

I random the 3 question to display on asp.net page. I want to match the security question with correct answer in database. But after I enter the correct answer in textbox and click the "Next" button, the answer cannot match the current question displayed on screen. But if I click the "Next" button, the next question displayed is matched with answer I enter on textbox in previous question. I think this is because the answer is match the question and answer after refresh the page. Please help. Here is my code. thanks
vb.net code
Dim SecurityQuestion As New DBDataContext
Dim randomNumber As Integer
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim SecurityQuestion As New DBDataContext
Dim rowCount As Integer = (From t In SecurityQuestion.tblSecurityQuestions Select t).Count + 1
End Sub
Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
ValidateAnswer()
End Sub
Private Sub LoadSecurityQuestion()
Dim SecurityQuestion As New DBDataContext
Dim mySecurityQuestion = (From c In SecurityQuestion.tblSecurityQuestions Where c.PkID = randomNumber Select c)
Dim rowCount As Integer = (From t In SecurityQuestion.tblSecurityQuestions Select t).Count + 1
randomNumber = New Random().Next(1, rowCount)
With gvQuestion
.DataSource = mySecurityQuestion
.DataBind()
End With
End Sub
Private Sub ValidateAnswer()
Dim SecurityQuestion As New DBDataContext
Dim validate = (From r In SecurityQuestion.tblRegistrations From s In SecurityQuestion.tblSecurityQuestions _
Where s.PkID = r.Q01 And r.A01 = txtSecurityAns.Text And r.UserID = ad And s.PkID = randomNumber _
Select r)
If validate.Count > 0 Then
Msg3.Text = "Correct Answer"
Else
Msg3.Text = "Invalid Answer"
End If
End Sub
At the time I posted this answer, it looked like a small bit of your code might have been missing. As an example, I couldn't see where you were actually calling the method LoadSecurityQuestion, but it sounds like you may be acidentally re-calling the method LoadSecurityQuestion even after validation succeeds.
I think you can fix this problem by wrapping your call to LoadSecurityQuestion with an "if" qualifier. As an example, lets suppose that you were calling LoadSecurityQuestion in your Load Event handler. If you did that, your new Load event handler might read something like this:
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
...
If IsPostBack Then
' If you wrap all of your Me.LoadSecurityQuestion calls
' with "If" statements like this one, then it should keep
' the security question from changing when the user
' enters the correct answer
If Msg3.Text <> "Correct Answer" Then
Me.LoadSecurityQuestion()
End If
End If
...
End Sub

Context.Item value is coming as null

On a page we have submit button, on clicking it we are getting error due to m_strPageDefinition has null value. Following is the code of it for more insight. Only sometimes and only in production we are getting value of m_strPageDefinition as null, which is causing problems. Does anyone has idea why m_strPageDefinition is coming null.
Private m_strPageDefinition As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
If (Not Context.Items("MyXmlString") = Nothing) And (Not Context.Items("mFormID") = Nothing) Then
MyXMLString = Context.Items("MyXmlString")
MyHiddenXMLString.Value = MyXMLString
End If
Else
m_strPageDefinition = MyHiddenXMLString.Value
End If
End Sub
Private Property MyXMLString()
Get
Return m_strPageDefinition
End Get
Set(ByVal value)
m_strPageDefinition = value
End Set
End Property
You should consider being more consistent how you address m_strPageDefinition. Why are you accessing is a private variable instead of always using the Property setter? e.g.
Private m_strPageDefinition As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
If (Not Context.Items("MyXmlString") = Nothing) And (Not Context.Items("mFormID") = Nothing) Then
MyXMLString = Context.Items("MyXmlString")
MyHiddenXMLString.Value = MyXMLString
Else
MyXMLString = MyHiddenXMLString.Value
End If
Else
MyXMLString = MyHiddenXMLString.Value
End If
End Sub
Private Property MyXMLString()
Get
Return m_strPageDefinition
End Get
Set(ByVal value)
m_strPageDefinition = value
End Set
End Property
I believe your issue is coming from the missing "Else" I have included here in the Not IsPostBack statement. Forgive me for VB.NET is not a language I code in, so the format/structure might be off a bit.
I think the problem is here:
m_strPageDefinition = MyHiddenXMLString.Value
and more specifically: MyHiddenXMLString.Value is probably null.
I couldn't figure out where you declared MyHiddenXMLString, but how about making it a hidden variable (input type="hidden" .. ) on the aspx side and setting it's value when the page loads the first time.
Then you know the value will always be there and easy to access.
hth

User enters code into TextBox, code gets added to URL (using session)

I'm using ASP.net 4.0 VB :)
I am using a session variable to add a user entered code into the url of each page. I can get the code to show up at the end of the page's URL that my textbox is on, but what do I add to every page to make sure that the session stays at the end of every URL that person visits during their session? This is the code from the page that the user enters their user code.
Protected Sub IBTextBoxButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles IBTextBoxButton.Click
Session("IB") = IBTextBox.Text
Dim IB As String = Session("IB")
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ProductID.Value = Request.QueryString("id")
If Session("IB") Is Nothing Then
tab4.Visible = "False"
Else
tab4.Visible = "True"
End If
End Sub
This is what I have in the page load of one of the other pages. What else do I add to make sure that variable is added to the URL of that page?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim IB As String
IB = Session("IB")
End Sub
string url = Request.Url.ToString();
string newUrl = url + url.Contains("?") ? "&" : "?" + "ib=" + Server.UrlEncode(IBTextBox.Text);
Response.Redirect(newUrl);
return;
The approach I might use would be to create a base page class that all of your pages can inherit. The base page would then inherit the System.Web.UI.Page.
Within your base page class, create a property for IB and also handle the page load event.
In that event, check if the QueryString has the IB parameter in it. If it does, set the property to the value in the parameter.
Private _IB As String
Public Property IB() As String
Get
Return _IB
End Get
Set(ByVal value As String)
_IB = value
End Set
End Property
Public Function GetIB(ByVal url As String) As String
If Not(_IB = String.Empty) Then
If (url.Contains("?")) Then
Return "&IB=" & _IB
Else
Return url & "?IB=" & _IB
End If
Else
Return url
End If
End Function
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not (String.IsNullOrEmpty(Request.QueryString("IB"))) Then
_IB = Request.QueryString("IB")
End If
End Sub
Finally in your markup you would need to place something like the following at the end of all of your links:
next page
I threw this code into the Master Page to make sure that every page knows whether or not the Session is there. Thanks for all the help everyone!
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
If Session("IB") Is Nothing Then
IBText.Visible = True
IBTextBox.Visible = True
IBTextBoxButton.Visible = True
Else
IBText.Visible = False
IBTextBox.Visible = False
IBTextBoxButton.Visible = False
lblIB.Visible = True
lblIB.Text = "Welcome, " + Session("First_Name") + " " + Session("Last_Name")
End If
End Sub

Resources