I can a textbox and a "Go" button, so user will enter the employid then click GO button to retrieve data. However, it doesn't work at the first click, but will work at the 2nd click. I set a breakpoint on Sub btnGO_Click(), and it will only active on the 2nd click.....
Can someone tell me why?Possible you could have two button click event handlers.
Set autoeventwireup to false.
Try using a delegate.
eg --------------
Public Sub New()
AddHandler NameOfYourButton.Click, AddressOf Me.ButtonClick
End Sub
Public Sub ButtonClick(Sender as object,E as EventArgs)
'Implementation in here
End Sub
---------------
Let me know if it works.
I have a textbox, txtEmpID, next to GO button. When user entered a employee id, and click GO, then all data will be retrieved. User can click Submit button to submit the application.
I just found out if I turned off the autopost back on the txtEmpID, the GO button will work fine at the first click. But I want to turn autopost back =true on the txtEmpID so when user clicked GO and retrieved data, then changed the EmployeeID, the submit button will be disabled until the user clicked GO button to retrieve data.
So what should I do now?
Go button code is like this :
Private Sub btnGO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGO.Click
Dim ds As DataSet
Dim dr As SqlDataReader
If Not conGPDb.State = ConnectionState.Open Then
conGPDb.Open()
End If
Dim cmdEmployeeInfo As New SqlCommand
Dim dtrEmployeeInfo As SqlDataReader
With cmdEmployeeInfo
.Connection = conGPDb
.CommandText = "SELECT M.EMPLOYID,M.LASTNAME,M.FRSTNAME FROM UPR00100 M where M.Employid=@.EmpID"
.Parameters.Add("@.EmpId", txtEmpID.Text)
End With
Dim adpt As SqlDataAdapter = New SqlDataAdapter(cmdEmployeeInfo)
ds = New DataSet
adpt.Fill(ds, "UPR00100")
dr = cmdEmployeeInfo.ExecuteReader()
dr.Read()
txtLName.Text = dr("LASTNAME")
txtFName.Text = dr("FRSTNAME")
txtMName.Text = dr("MIDLNAME")
conGPDb.Close()
conEZDb.Open()
End Sub
You can also get problems like this if you are creating the controls dynamically. Couold you post a little of your code.
But first have a look to see if midi25 is right. I remember I was copying and pasting and forgot to change the Handles part of a function. I ended up having two functions that handled the datagrid itemcommand lol. Took me ages to find out what was wrong.
The problem as you rightly say is because you have AutoPostBack set to true for your EmployeeId text box. When you change the value in that text box and then click the Go button what is actually happening is that the autopostback is kicking in and is raising the TextChanged server side event for your textbox. The Click event for the button will not fire as the Text_Changed event gets in there first as it were. This is why the first click appears to do nothing.
Maybe its better to create a new form. And slighty re-work what you are tyring to achieve.
Place a textbox on the form and name it eg : tbEmployeeId
Place a button on the form and name it eg: btSubmit
In the code behind you should see an autowired event handler for the btSubmit click event. eg:
Public Sub btSubmitClick(ByVal Sender as object,ByVal E as EventArgs) Handles btSubmit.Click
'Place a call to your GetEmployee Function here...
GetEmployee()
End Sub
Private Sub GetEmployee()
'Declarations
'!!Dim conn and command here
'!!Dim datareader here
'Implement conn and command functionality
'Invoke command.executereader to populate the datareader
Assign your other text boxes with the values from the reader.
eg tbFName.text = dr(0)
tbLName.text = dr(1)
End Sub
Sorry hanvt got time to give you a better reply as am hard at work in study for my next mcp. But the jist is there.
Once they entered a value and click the button you could redirect them to another page that would display the results in a repeater or datagird etc.
Hope someone here can give you the correct syntax maybe.
Another quick and dirty reply is to use a bit of client side script to disable the Submit button when the value in the employee field changes. Try putting the following in your Page_Load event
txtEmpId.Attributes.Add("onChange", "document.getElementById('" + btnSubmit.ClientID + "').disabled = true;");
Where txtEmpId id your Employee TextBox and btnSubmit is your submit button control.
Thanks to all, especially to Clarky.
I put the txtEmpId.Attributes.Add("onChange", "document.getElementById('" + btnSubmit.ClientID + "').disabled = true") in my Page_Load even and it works perfectly.
If I have time, I should rework a little bit on my program. But now I will use this shortcut.
0 comments:
Post a Comment