Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Enter Key Problem

Status
Not open for further replies.

sila

Technical User
Aug 8, 2003
76
GB
Hi

I have an asp.net page which has a datagrid with edit buttons. When clicked the edit buttons feed the field details into some textboxes on the same page where they can be edited, alternatively, a new record can be added by clicking a separate 'add new' button. If I click the 'add new' button and start to enter some text into the field then hit the enter button (by mistake) the page reloads and inserts all of the data from the first row in the datagrid???

Does anyone have any idea why this might be?

Thanks

Sila.
 
Sounds like it is doing an autopostback and rebinding the datagrid. Could you give us some code to better identify the problem?
 
Here is the item command code which I think is what you need to see?(excuse the session variable nonsense!)
thanks
---------------------

Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.ItemCommand
Dim EditCandidate As Integer
EditCandidate = Me.DataGrid1.DataKeys(e.Item.ItemIndex)

Dim dtrCandidate As System.Data.SqlClient.SqlDataReader

Me.SqlCommand8.Parameters("@CandidateID").Value = EditCandidate
Me.SqlConnection1.Open()
dtrCandidate = Me.SqlCommand8.ExecuteReader
If dtrCandidate.Read Then
'Put existing database record fields into session variables
Session("Surname") = dtrCandidate("Surname")
Session("Initials") = dtrCandidate("Initials")
Session("Title") = dtrCandidate("Title")
Session("Forename2") = dtrCandidate("ForeName1")
Session("ForeName2") = dtrCandidate("ForeName2")
Session("ForeName3") = dtrCandidate("ForeName3")
Session("Address1") = dtrCandidate("Address1")
Session("Address2") = dtrCandidate("Address2")
Session("Address3") = dtrCandidate("Address3")
Session("Address4") = dtrCandidate("Address4")
Session("Postcode") = dtrCandidate("Postcode")
Session("ForeName1") = dtrCandidate("ForeName1")
Session("CandidateID") = dtrCandidate("CandidateID")
Session("PartyNameID") = dtrCandidate("PartyNameID")
Me.SqlConnection1.Close()

'session variables are then put into variables
Surname = Session("Surname")
ForeName1 = Session("ForeName1")
ForeName2 = Session("ForeName2")
ForeName3 = Session("ForeName3")
ForeName3 = Session("ForeName3")
Address1 = Session("Address1")
Address2 = Session("Address2")
Address3 = Session("Address3")
Address4 = Session("Address4")
Postcode = Session("Postcode")
PartyNameID = Session("PartyNameID")
TypeID = Session("TypeID")
ElecFullDate = Session("FullDate")
DivisionID = Session("DivisionID")
DivName = Session("DivName")
ElecID = Session("ElecID")
DivElecID = Session("DivElecID")

AddNewCandidate()

'the variables are fed back into text boxes for editing
Me.txtCanSurname.Text = Surname
Me.txtCanForename1.Text = ForeName1
Me.txtCanForename2.Text = ForeName2
Me.txtCanForename3.Text = ForeName3
Me.txtCanAddress1.Text = Address1
Me.txtCanAddress2.Text = Address2
Me.txtCanAddress3.Text = Address3
Me.txtCanAddress4.Text = Address4
Me.txtCanPostcode.Text = Postcode
Me.ddlCanSpecificName.SelectedValue = PartyNameID

Session("Mode") = "Edit"
Me.lblTitleAddCandidate.Text = "Add/Edit details for " & ForeName1 & " " & Surname & " standing at " & DivName & " "

End If

End Sub
 
Well, I think it may be where or when you bind the datagrid that is problem. Could you print out your page load as well as any other sub that refers to binding the datagrid?

Also use [ code ] and [ /code ] with no spaces around your code so we can read it better. Thanks!
 
Case "B" below calls ShowCandidates() but I have no other refs to binding the datagrid.

thanks

-----------------------------
Code:
 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here

        If Not Page.IsPostBack Then
            Me.lblTitle.Text = "The County Election System"
            'get Session
            Select Case Session("SetUpPage")
                Case "A" ' an existing election
                    SetUpPageA()
                Case "B" ' a new election
                    SetUpPageB()
   
            End Select
        End If

        If Session("Mode") = "New" Or Session("Mode") = "Edit" Then
            KeepTextBoxVals()
        Else : RemoveBoxes()
        End If

    End Sub
-----------------------------------------------------

Private Function ShowCandidates()

        'INSERT CANDIDATES
        Dim dtrStoredCandidates As System.Data.SqlClient.SqlDataReader
        Dim intCountCandidates As Integer

        'feed named variables into 'CandidateStored' stored procedure
        ElecFullDate = Session("ElecFullDate")
        DivisionID = Session("DivisionID")
        DivName = Session("DivName")
        ElecID = Session("ElecID")
        Session("Electorate") = Me.txtElectorate.Text


        Me.SqlCommand2.Parameters("@ElecID").Value() = Session("ElecID")
        Me.SqlCommand2.Parameters("@DivElecID").Value() = Session("DivElecID")

        'pull in all candidate standing for the division (if any entered)
        Me.SqlConnection1.Close()
        Me.SqlConnection1.Open()
        Me.SqlCommand2.ExecuteNonQuery()
        dtrStoredCandidates = Me.SqlCommand2.ExecuteReader
        Me.DataGrid1.DataSource = dtrStoredCandidates
        Me.DataGrid1.DataKeyField = "CandidateID"
        Me.DataGrid1.DataBind()
        Me.SqlConnection1.Close()
        Me.DataGrid1.Visible = True
    End Function
 
Basically what you are saying is that when you hit Add Row, a row is created and you can add text to it. However, if you hit enter while adding data to this new row, the page refreshes but the new row has data from the old Dataset's first row?

Is this correct?
 
Yes, but the new row is in text boxes. Explanation below....

On the page I have a datagrid with edit buttons for each row, a separate 'Add New' button and some text boxes. If I press a datagrid edit button the values of he datagrid row are taken and inserted into the textboxes on the page and may be edited (i.e. not inline editing). The problem is when I press the 'Add New' button, again using the textboxes, I start to enter a new record, then if at thi spouint I accidently press enter the new record I have started to put in the text boxes disappears and the first record of the datagrid on the page is put into the text boxes instead.
 
Hmm. Is there a row of the first record without the textboxes? Or are there 2 records that are identical, other than one is in textboxes?

I would recommend debugging, stepping through it, and see if you can see when events are fired.

I think when you add a new record while you are editing a record, you need to kick off the save before adding the new record. Maybe have a popup that says, 'Are you done editing this record?', if you are, go to the next record.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top