我有一个我一直在努力解决的逻辑错误。这是我的情况:我正在使用 .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 后单击编辑按钮时,它总是跳回到第一条记录并打开它进行编辑。我将不胜感激任何可以为我指明正确方向的事情。