0

我有一个我一直在努力解决的逻辑错误。这是我的情况:我正在使用 .Net 4.0。你有一个绑定到 SQLDataSource 的 FormView 控件。此外,我还有一个 DropDownList 用作过滤 FormView 记录的选项。DropDownList 也绑定到 SQLDataSource。我的问题是,当 FormView 没有通过 DropDownList 过滤时,它的行为符合预期,但是一旦我从 DropDownList 中选择了一个过滤器,它的行为就会以一种奇怪的方式出现。我的代码将遵循此描述。每当单击编辑按钮时,FormView 总是跳回第一条记录并打开它进行编辑,无论我选择了哪条记录。我可能做错了一些小事,希望您能提供帮助。

澄清一下:我有一个 DropDownList 如下:

    <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>

DropDownList的数据源如下:

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

在 DropDownList 的 SelectedIndexChanged 事件处理程序中,我重新定义了 FormView 的 SelectCommad 并重新绑定它,如下所示:

    stdCode = Convert.ToInt32(cboSearchApplicantName.SelectedValue);

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

不幸的是,由于长模板定义和网站正文限制,我无法粘贴 formview 控件的定义。但是,FormViews SqlDataSource 定义如下。

    <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>  

为了重申问题所在,当我通过选择我希望它显示的记录重新填充 FormView 后单击编辑按钮时,它总是跳回到第一条记录并打开它进行编辑。我将不胜感激任何可以为我指明正确方向的事情。

4

1 回答 1

0

我设法解决了我的问题。似乎问题源于 FormView 的 SqlDataSource 的 Select Command 覆盖了我在 SelectedIndexChanged 的​​代码中定义的 SelectCommand。我通过在设计时不为 formview 定义 SqlDataSource 而是在后面的代码上创建一个并在需要时绑定到它来解决这个问题。这是,我可以控制何时绑定以及绑定到什么查询或更确切地说是命令。这样做的另一面是,我必须为插入、更新和删除设置参数做繁重的工作,而如果我定义了 SqlDataSource,我会简单地定义它的选择命令、插入命令、更新命令和删除命令并完成它。

于 2013-06-04T21:21:37.150 回答