How do you stop the Next button in a WizardControl? - asp.net

I am using a WizardControl in .NET 2.0. On the first Step (which is set to StepType="Start") when the next button is clicked, I run server-side validation code. But, no matter what I do it keeps going to the next step. Here is my code:
Protected Sub Wizard1_NextButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles Wizard1.NextButtonClick
Dim oUser As New BE.User
Select Case Wizard1.ActiveStepIndex
Case 0
If Membership.FindUsersByName(UserName.Text).Count = 0 Then
oUser.UserName = UserName.Text
oUser.Password = Password.Text
oUser.Email = Email.Text
Wizard1.ActiveStepIndex = 1
Else
Wizard1.ActiveStepIndex = 0
ErrorMessage.Text = "user name already in use"
End If
Case 1
Case 2
End Select
End Sub

You can write e.Cancel=true
if you are working in any wizard event. Here "e" is an alias for WizardNavigationEventArgs

The Wizard control's NextButtonClick event has a
"WizardNavigationEventArgs" parameter that contains a "Cancel" property
help to cancel the current next navigation operation.
courtesy of
Steven Cheng Microsoft Online Support

As others have mentioned, you can use the Cancel property of the WizardNavigationEventArgs. Here's your code updated to reflect that:
Protected Sub Wizard1_NextButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles Wizard1.NextButtonClick
Dim oUser As New BE.User
Select Case Wizard1.ActiveStepIndex
Case 0
If Membership.FindUsersByName(UserName.Text).Count = 0 Then
oUser.UserName = UserName.Text
oUser.Password = Password.Text
oUser.Email = Email.Text
Wizard1.ActiveStepIndex = 1
Else
Wizard1.ActiveStepIndex = 0
ErrorMessage.Text = "user name already in use"
' Set the Cancel property to True here
e.Cancel = True
End If
Case 1
Case 2
End Select
End Sub

Related

On Button click change Button value text

I have response form in asp.net. I want on send button click the button text should change from send to please wait & once it is send it should back to it's default value text 'send'. can any one help?
code
Protected Sub submit_client_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles submit_client.Click
Try
Dim pattern As String
pattern = "^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*#([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$"
If Regex.IsMatch(TextBox3.Text, pattern) Then
Label2.Text = ""
Else
Label2.Text = "Not a valid Email address "
End If
Dim emails As New List(Of String)()
generate.Visible = True
clear.Visible = True
SendHTMLMail()
'For Each item As ListViewItem In lvCustomers.Items
' Dim ck As CheckBox = DirectCast(item.FindControl("CheckBox1"), CheckBox)
' If ck.Checked Then
Catch ex As Exception
Response.Write(ex)
End Try
End Sub
try like this:
MyButton.Text="Sending"
SendHTMLMail()
Thread.Sleep(3000) // add this & remove SendHTMLMail() for testing
MyButton.Text="Send"

how to get the text value of dynamically created textbox

I want to get the value of my textbox that I created dynamically when I click a button
I need to do this cause the value of my textbox is used for retrieve data from database
how could I achieved this thing??
the flow is Button click - creating textbox - filling textbox with value - Button Click - Get Text of textbox
here is my code to make the textbox
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For i As Integer = 0 To 4
textbox = New TextBox With {.ID = "TextBox" & i}
plchld.Controls.Add(textbox)
Next
End Sub
I have tried something like this but the code didn't work
Protected Sub OkButton_Click(sender As Object, e As EventArgs) Handles OkButton.Click
Dim a(5) As String
For i As Integer = 0 To 4
a(i) = CType(plchld.FindControl("Textbox" & i), TextBox).Text
Next
End Sub
thanks in advance for any help
edit for the answer
I've found the way to solve this. I use request.form to get the value of my textbox.
Thanks for anyone that participating
Regards,
Julian
This is how I have done in my asp.net application.
Creating dynamic control
TextBox txtDate = new TextBox();
txtDate.EnableViewState = true;
txtDate.ID = "PreixValue" + 1;
txtDate.Text = "07 Feb 2014"
pnl.controls.add(txtdate);
To retrieve the value from that textbox
DateTime datefrom = DateTime.Now ;
for (int cnt = 0; cnt < Request.Form.Count; cnt++)
{
if (Request.Form.AllKeys[cnt].Contains("Prefixvalue"))
{
int ParamStartPoint = Request.Form.AllKeys[cnt].IndexOf("Prefix") + 4;
int ParamNameLength = Request.Form.AllKeys[cnt].Length - ParamStartPoint;
string[] ControlName = Request.Form.AllKeys[cnt].Substring(ParamStartPoint, ParamNameLength).Split('$');
if (ControlName[0] == "Date From")
{
datefrom = DateTime.Parse(Request.Form[cnt]);
//datefrom has value now
}
}
}
This is how I have done in my web application, but there may be other ways achieve this.
basically when you create Dynamic control in webform this will be available through Request.Form.
hope this helps you.
Protected Sub OkButton_Click(sender As Object, e As EventArgs) Handles OkButton.Click
Dim a(5) As String
For i As Integer = 0 To 4
Dim anotherObj As TextBox = Me.Controls.Item("Textbox" & i)
a(i) =anotherObj.Text
Next
The issue is that dynamic controls are lost on a postback so when the OkButton click event is handled, there is nothing inside your plchld control. You must recreate your controls with the same ID on postback if you want to retrieve the text in the textboxes.
Using your code, all you need to do is on postback determine if the textboxes were created and if so, recreate them.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' Determine if the text boxes were created and if so, recreate them.
If CBool(ViewState("TextBoxesCreated")) Then
CreateTextBoxes()
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
CreateTextBoxes()
ViewState("TextBoxesCreated") = True
End Sub
Private Sub CreateTextBoxes()
For i As Integer = 0 To 4
plchld.Controls.Add(New TextBox With {.ID = "TextBox" & i})
Next
End Sub
Protected Sub OkButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles OkButton.Click
Dim a(4) As String
For i As Integer = 0 To 4
a(i) = CType(plchld.FindControl("Textbox" & i), TextBox).Text
Next
End Sub
I don't know the full extent of what you are doing but I would suggest not creating them dynamically if you don't need to. Just show or hide the textboxes instead.
Reference: http://www.codeproject.com/Articles/3684/Retaining-State-for-Dynamically-Created-Controls-i

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

Click event is not firing of dynamically added link button in grid view

I am writing the following code on the rowdatabound of the grid view and i am not getting the click event of link button
Protected Sub CoolGRDSourcedDetails_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles CoolGRDSourcedDetails.RowDataBound
Dim iLoop As Integer
Dim lbtnCountDetails As New LinkButton
e.Row.Cells.RemoveAt(1)
If e.Row.RowType = DataControlRowType.Header Then
For iLoop = 1 To (dscolumns / 2) - 1
e.Row.Cells(iLoop).Attributes.Add("colspan", "2")
If iLoop = 1 Then
e.Row.Cells(iLoop).Text = "Self"
Else
e.Row.Cells(iLoop).Text = "Child" & iLoop - 2
End If
Next
e.Row.Cells(iLoop).Text = "Total"
ElseIf e.Row.RowType = DataControlRowType.DataRow Then
For iLoop = 1 To dscolumns - 2
If iLoop Mod 2 <> 0 Then
e.Row.Cells(iLoop + 1).Text = Format(IIf(CInt(e.Row.Cells(iLoop).Text) <> 0, (CInt(e.Row.Cells(iLoop).Text) / value) * 100, 0), "0.00") & "%"
If CInt(e.Row.Cells(iLoop).Text) <> 0 Then
e.Row.Cells(iLoop).Controls.Add(lbtnCountDetails)
lbtnCountDetails.Text = e.Row.Cells(iLoop).Text
lbtnCountDetails.CommandArgument = "strstatus"
lbtnCountDetails.Attributes.Add("OnClick", "lbtnCountDetails_Click")
End If
End If
Next
End If
End Sub
'Click event is here
Protected Sub lbtnCountDetails_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim strStatus As String = CType(sender, LinkButton).CommandArgument
End Sub
Go to the below line in your code and make changes after that: -
ElseIf e.Row.RowType = DataControlRowType.DataRow Then
Actually you forgot to add an event handler to the event of LinkButton1, so you are not able to get the click event of the LinkButton.
The changes you have to make : -
If CInt(e.Row.Cells(iLoop).Text) <> 0 Then
LinkButton1.Text = e.Row.Cells(iLoop).Text
AddHandler LinkButton1.Click, AddressOf Me.LinkButton1_Click
LinkButton1.CommandArgument = e.Row.Cells(0).Text
e.Row.Cells(iLoop).Controls.Add(LinkButton1)
End If
Try it.
You can't add the event binding with an Attributes.Add. You can use the RowCommand event from the gridview. This link has a good example of using RowCommand.
Same question here.
Give a command name to your link button, like this
CommandName="Show"
and in the code behind, handle it as,
protected void gridviewReport_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Show")
{
// this will return the row index
int index = Convert.ToInt32(e.CommandArgument);
// your code goes here
}
}
Try this line:
Dim WithEvents lbtnCountDetails As New LinkButton

How to sort a gridview once a radio button is selected

I'm trying to sort records in the gridview right after a radio button is selected. My approach is with the dataview, but because the dataset variable doesn't survive a round trip to the server, I don't know how to make this happen. please help!
Public Sub GetCustomers()
db.RunProcedure("usp_customers_get_all")
db.doSort(radList.SelectedValue)
gvCustomers.DataSource = db.MyView
End Sub
Protected Sub radList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radList.SelectedIndexChanged
If radList.SelectedValue = 0 Then
db.doSort(0)
gvCustomers.DataSource = db.MyView
End If
If radList.SelectedValue = 1 Then
db.doSort(1)
gvCustomers.DataSource = db.MyView
End If
End Sub
Public Sub doSort(ByVal strIn As Integer)
If strIn = 0 Then
MyView.Sort = "lastname, firstname"
Else
MyView.Sort = "username"
End If
End Sub
Public Sub RunProcedure(ByVal strName As String)
Dim objConnection As New SqlConnection(mstrConnection)
Dim mdbDataAdapter As New SqlDataAdapter(strName, objConnection)
Try
mdbDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure
Me.mDataset.Clear()
mdbDataAdapter.Fill(mDataset, "tblCustomers")
MyView.Table = mDataset.Tables("tblCustomers")
Catch ex As Exception
Throw New Exception("stored procedure is " & strName.ToString & " error is " & ex.Message)
End Try
End Sub
You could store the dataset in one of the following places and then when the post back happens just load it again from there. I have done many of these on a corporate intranet.
Session Variable
ViewState
QueryString
Cache
I cant really provide more help as you didn't specify if this is done in Ajax or if you do a full postback etc. If you provide more info I would love to help you.

Resources