Treeview node going to same postbackurl previously clicked by other adjacent node - asp.net

I have a treeview asp.net control. And I have the following SelectedNodeChanged Event ofr Treeview1.
Private Sub TreeView1_SelectedNodeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.SelectedNodeChanged
Try
Dim type As Boolean = False
Dim nodetext As String = TreeView1.SelectedNode.Value
Dim depth As Integer = TreeView1.SelectedNode.Depth
If nodetext = "Root" Then
btnCreateComp.Visible = True
btnCreateComp.Text = "Create Company"
btnCreateUser.Visible = False
Else
type = getNodeType(nodetext)
'if it is true its USER
'if false it is COMPANY
If type = True Then
btnCreateComp.Visible = False
btnCreateUser.Visible = True
btnCreateUser.Text = "Edit User"
btnCreateUser.PostBackUrl = "~/Account/User.aspx"
Else
btnCreateComp.Visible = True
btnCreateComp.Text = "Edit Company"
btnCreateUser.Visible = True
btnCreateUser.Text = "Create User"
btnCreateUser.PostBackUrl = "~/Account/User.aspx"
End If
End If
Catch ex As Exception
End Try
End Sub
When I click on a node, and if it is not "root", it makes 2 buttons visible, if I click the button btnCreateUser , it goes to a page "user.aspx", which is postbackurl for btnCreateUser.
This is fine.
But when the next time a click a node other than "Root", it directly goes to the postbackurl page "User.aspx", even though I have not clicked on the Button btnCreateUser.
And this happens only once after the first time I have clicked "btnCreateUser".
Can anyone please help?

Related

Need a 'RaiseEvent' statment to raise an event

I have a page on the website that display a client details in text boxes.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not page.IsPostBack Then
Dim dh As New Datahandler
Dim CI As ClientInfomation = dh.GETClientInfomationWithClientID(Session("ClientID"))
txtClientID.Text = Session("ClientID")
txtCompany.Text = CI.Company
txtEmail.Text = CI.Email
txtPassword.text = CI.Password
dtSubcriptionEndDate.Text = CI.SubscriptionEndDate
txtContactName.Text = CI.ContactName
txtTelephoneNum.Text = CI.Telephone.ToString()
End If
The above code works,the code gets the client data from the database in the GETClientInfomationWithClientID(Session("ClientID")) and loads the data into a class, and from the class I load the data into the text boxes.
Here is where my problem starts. A user will now change his details in the text boxes and click the 'Save changes' button invoking the butSaveChanges_Click Event.
Protected Sub butSaveChanges_Click(sender As Object, e As EventArgs) Handles butSaveChanges.Click
Dim dh As New Datahandler
Dim CI As New ClientInfomation With {.ClientID = txtClientID.Text,
.Company = txtCompany.Text,
.ContactName = txtContactName.Text,
.Email = txtEmail.Text,
.Password = txtPassword.Text,
.SubscriptionEndDate = dtSubcriptionEndDate.Text,
.Telephone = txtTelephoneNum.Text}
If dh.SaveUserProfileChanegs(CI) = True Then
ClientScript.RegisterStartupScript(Me.[GetType](), "alert", "alert('Changes has been saved')", True)
Else
ClientScript.RegisterStartupScript(Me.[GetType](), "alert", "alert('Changes could not be saved')", True)
End If
End Sub
I just wanted to load the changed text values into the class, and load the class in the SaveUserProfileChanegs(CI) function that updates the new values in the database.
When the butSaveChanges_Click event is invoked I get a "(1) : error BC32022: 'Public Event Click As EventHandler' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event."

dropdownlist does not retain values after postback if it is inside formview and is populated at runtime

I need help figuring out why some controls that are inside a formview and are filled at runtime lose their contents after postback.
I have a dropdownlist (ddl_1) that I populate based on entries from another dropdownlist (ddl_2) in the same formview. All seem to work fine util a postback event occurs after which, the newly populated dropdownlist (ddl_1) is empty.
The EnableViewState for both these ddls is set to true. ddl_2 is databound but ddl_1 is not. In Page_Load in IsPostBack clause, ddl_2 is databound and then I call the function that populates ddl_1.
If I move ddl_1 outside of the formview, it retains its entries after postback just fine.
Another similar issue is with a TextBox inside the formview. When in insert mode, the contents of the TextBox disappear after postback. This does not happen in insert mode though.
What is specific to formview that causes this?
Thanks much.
Here is the Page_load code.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
If NavHelper.User.UserName = "" Then
Dim UserIP As String
Dim UserLogin As String
Dim UserEmail As String
UserIP = HttpContext.Current.Request.UserHostAddress
UserLogin = HttpContext.Current.Session("Username")
UserEmail = HttpContext.Current.Session("Email")
GetUserInfo()
CurrentRFQ = Nothing
If NavHelper.RFQ.ID = -1 Then
formview_RFQ.ChangeMode(FormViewMode.Insert)
tabpanelCustomerParts.Visible = False
tabpanelDocuments.Visible = False
tabpanelReviews.Visible = False
tabpanelRFQReviewHistory.Visible = False
listview_CustomerParts.Dispose()
Else
formview_RFQ.ChangeMode(FormViewMode.Edit)
listview_ReviewContracts_Initial.EditIndex = 0
SessionHelper.CurrentObject = TAA.Library.RFQ.GetObject(NavHelper.RFQ.ID)
mRFQ = DirectCast(SessionHelper.CurrentObject, TAA.Library.RFQ)
Dim UserdeptTotal As Long
UserdeptTotal = HttpContext.Current.Session("DepartmentTotal")
If formview_RFQ.FindControl("ddlCompanyBuyerNVList") IsNot Nothing Then
Dim ddl As DropDownList = DirectCast(formview_RFQ.FindControl("ddlCompanyBuyerNVList"), DropDownList)
FillCompanyNameDropDownList(ddl)
End If
tabpanelCustomerParts.Visible = True
tabpanelDocuments.Visible = True
tabpanelReviews.Visible = True
tabpanelRFQReviewHistory.Visible = True
If NavHelper.RFQ.Copy = True Then
SetModifyCopy()
End If
End If
Else 'IsPostBack
datasource_BuyerNVList.Dispose()
datasource_BuyerNVList.DataBind()
Dim ddl As DropDownList
If (formview_RFQ.CurrentMode = FormViewMode.Insert) Then
ddl = DirectCast(formview_RFQ.FindControl("ddlCompanyBuyerNVListInsert"), DropDownList)
ElseIf formview_RFQ.FindControl("ddlCompanyBuyerNVList") IsNot Nothing Then
ddl = DirectCast(formview_RFQ.FindControl("ddlCompanyBuyerNVList"), DropDownList)
End If
FillCompanyNameDropDownList(ddl)
End If
End If
End Sub
While binding the dropdown use the IsPostback option.
if (!IsPostback)
{
BindDropdown1();
BindDropdown2();
}
This will retain your state. If you are not using ispostback every time the dropdown will be binded when ever you refresh the page. So use ispostback for binding dropdown for the first time.

Button control not firing

The problem im having is a little complicated to explain, so please bear with me. I have 2 button controls. In the page load, I wanted to know what button created a postback on the page load. Through research I have found this snippet below and it works as expected. So here is my scenario of events that occur when click on the button.
1. Click the button does a postback
2. Runs the function below and tell me the id of the button
3. Runs the clicked event handlers for that button
Protected Sub btnCalc_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCalc.Click
' Do this
End Sub
The problem comes in when I click the second button, it does steps 1 and 2 but NEVER DOES 3. Through testing, I have that it only does 1, 2, and 3 on the first button clicked. I have no clue why this is happening?
Function GetPostBackControlName() As String
Dim control As Control = Nothing
Dim ctrlname As String = Page.Request.Params("__EVENTTARGET")
If ctrlname <> Nothing AndAlso ctrlname <> [String].Empty Then
control = Page.FindControl(ctrlname)
Else
Dim ctrlStr As String = [String].Empty
Dim c As Control = Nothing
For Each ctl As String In Page.Request.Form
c = Page.FindControl(ctl)
If TypeOf c Is System.Web.UI.WebControls.Button Then
control = c
Exit For
End If
Next
End If
Try
Return control.ID.ToString
Catch
Return ""
End Try
End Function

Field Validation in Repeater Control

I have a question related to asp.net and vb.net. I have a repeater control that I bind some data to and allow users to update/change fields within certain text boxes. I added a validation control to trigger when the user doesn't enter a valid date or the text "TBD". On submit, I want to go through and highlight each field where its corresponding validator is not valid. This is my current code, but I am lost as to how to find the text box control.
Sub ValidateDateField(ByVal sender As Object, _
ByVal args As ServerValidateEventArgs)
'validate against three conditions - date, "TBD", and "N/A"
Dim dtValue = args.Value
If dtValue.ToUpper = "TBD" Or dtValue.ToUpper = "N/A" Then
args.IsValid = True
ElseIf IsDate(dtValue) Then
args.IsValid = True
Else
args.IsValid = False
Dim cont As WebControl = DirectCast(Page.FindControl(args.ToString), WebControl)
cont.BackColor = Drawing.Color.White
util.Client_Alert("Please Update Highlighted Fields")
End If
End Sub
I am completely lost as to how to get cont = textbox1row1 of my repeater control. Please advise. All examples I have seen so far directly state the control as in text1.BackColor =
So I figured out the answer. Sorry for the question. Don't know why I didn't think about it sooner. I found the naming container of the my custom val and then found the control based on its name in the repeater. in vb.net it was something like this:
Sub ValidateDateField(ByVal sender As Object, _
ByVal args As ServerValidateEventArgs)
'args holds value. validate against three conditions - date, "TBD", and "N/A"
Dim dtValue = args.Value
Dim cont As CustomValidator = sender
Dim myNC As Control = cont.NamingContainer
Dim strControl As String
strControl = cont.ControlToValidate
Dim txtbox As TextBox = _
DirectCast(myNC.FindControl(cont.ControlToValidate), TextBox)
If dtValue.ToUpper = "TBD" Or dtValue.ToUpper = "N/A" Or IsDate(dtValue) Then
args.IsValid = True
txtbox.BackColor = Drawing.Color.Empty
Else
args.IsValid = False
txtbox.BackColor = Drawing.Color.White
End If
End Sub

How do you stop the Next button in a WizardControl?

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

Resources