Domanda

I have a logical error I have been struggling to fix. Here is my situation: I am working on .Net 4.0. U have a FormView control that's bound to an SQLDataSource. In addition I also have a DropDownList that I use as an option to filter the FormView 's records. The DropDownList is also bound to a SQLDataSource. My problem is that when the FormView is not filtered through the DropDownList it behaves as expected but soon as I select a filter from the DropDownList its behaves in a weird way. My code will follow this description. Whenever the Edit button is clicked the FormView always jumps back to the first record and open it for editing regardless of which record I have selected. There is probably something small that I am doing wrong and would appreciate your help.

To clarify: I have a DropDownList as follows:

    <asp:ComboBox ID="cboSearchApplicantName" runat="server" 
                    AutoCompleteMode="Suggest" DataSourceID="SearchApplicantDataSource" 
                    DataTextField="Name" DataValueField="ApplicantCode" DropDownStyle="DropDownList" 
                    MaxLength="0" style="display: inline;" AutoPostBack="True" 
                    onselectedindexchanged="cboSearchApplicantName_SelectedIndexChanged" AppendDataBoundItems="True">
                    <asp:ListItem Selected="True" Value="">(none)</asp:ListItem>
                </asp:ComboBox>

The DropDownList's datasource is as follows:

    <asp:SqlDataSource ID="SearchApplicantDataSource" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:Grads_tablesSQLContext %>" 
                    SelectCommand="Select ApplicantCode,UGStudentID,LastName + ' ' + FirstName as Name from Applicants">
                </asp:SqlDataSource>

In the DropDownList's SelectedIndexChanged event handler, I re-define the FormView's SelectCommad and rebind it as follows:

    stdCode = Convert.ToInt32(cboSearchApplicantName.SelectedValue);

        ApplicantSqlDataSource.SelectCommand = "Select * FROM Applicants WHERE ApplicantCode =" + stdCode;
        ApplicantFormView.DataBind();

Unfortunately I cannot paste the definition of the formview control because of the long template definitions and the sites Body Limit. However, the FormViews SqlDataSource is defined below.

    <asp:SqlDataSource ID="ApplicantSqlDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:Grads_tablesSQLContext %>" 

        SelectCommand="SELECT * FROM [Applicants]" 

        UpdateCommand="UPDATE Applicants SET LastName=@LastName, FirstName=@FirstName, Sex=@Sex, DateOfBirth=@DateOfBirth, ApplicationCitizenshipStatus=@ApplicationCitizenshipStatus, 
                        InternationalCitizenship=@InternationalCitizenship, 
                        HomeUnit=@HomeUnit,ProgramType=@ProgramType,Program=@Program,EntrySemester=@EntrySemester, 
                        RegistrationClassification=@RegistrationClassification, Address1=@Address1, 
                        Address2=@Address2,City=@City, 
                        Province=@Province,PostCode=@PostCode, 
                        Country=@Country,PhonePermanent=@PhonePermanent,PhoneOther=@PhoneOther, 
                        Email=@Email 
                       WHERE ApplicantCode=@ApplicantCode" 
        InsertCommand="INSERT INTO Applicants(UGStudentID, LastName, FirstName, Sex, DateOfBirth, ApplicationCitizenshipStatus, InternationalCitizenship, 
                       HomeUnit, ProgramType, Program, EntrySemester, RegistrationClassification, Address1,  Address2, City, 
                        Province, PostCode, Country, PhonePermanent, PhoneOther, Email) 
                        VALUES (@UGStudentID, @LastName, @FirstName, @Sex, @DateOfBirth,@ApplicationCitizenshipStatus ,@InternationalCitizenship, 
                        @HomeUnit, @ProgramType, @Program, @EntrySemester, @RegistrationClassification, 
                        @Address1, @Address2, @City,  @Province, @PostCode, 
                        @Country, @PhonePermanent, @PhoneOther, @Email); 
                     SELECT @ApplicantCode = SCOPE_IDENTITY()" 

        DeleteCommand="DELETE Applicants WHERE ApplicantCode=@ApplicantCode" 
    oninserting="ApplicantSqlDataSource_Inserting" 
    onupdating="ApplicantSqlDataSource_Updating"> 

            <InsertParameters> 
        <asp:Parameter Name="ApplicantCode" Direction="Output" Type="Int32" DefaultValue="0" /> 
      </InsertParameters> 
        </asp:SqlDataSource>  

To reiterate what the problem is, when I click on the edit button after having repopulated the FormView by selecting a record I'd want it to display, it always jumps back to the first record and open it for editing. I would appreciate anything that can point me to the right direction.

È stato utile?

Soluzione

I have managed to solve my issue. It seems the issue stemmed from the Select Command of the FormView's SqlDataSource overriding the SelectCommand I had defined in my code in the SelectedIndexChanged. I solved this by not defining an SqlDataSource for the formview at design time but rather creating one on the code behind and binding to it when I needed to. This was, I have control of when to bind and to what query or rather command I'm binding to. The flip side of this is that I have to do the tiring work setting up the parameters for insert,update, and delete whereas had I had an SqlDataSource defined I would have simply defined its Select Command, Insert Command, Update Command, and Delete Command and be done with it.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top