我有一个逻辑错误,我一直在努力修复。这是我的情况:我正在研究.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();
不幸的是,由于长模板定义和网站Body Limit,我无法粘贴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后点击编辑按钮时,它总是跳回到第一条记录并打开它进行编辑。我希望任何可以指向正确方向的东西。
答案 0 :(得分:0)
我设法解决了我的问题。似乎问题源于FormView的SqlDataSource的Select命令,它覆盖了我在SelectedIndexChanged的代码中定义的SelectCommand。我通过在设计时没有为formview定义一个SqlDataSource来解决这个问题,而是在我需要的时候在后面的代码上创建一个并绑定它。这是,我控制何时绑定以及我正在绑定的查询或命令。另一方面,我必须为插入,更新和删除设置参数,但是如果我定义了SqlDataSource,我会简单地定义它的Select Command,Insert Command,Update Command和Delete Command并完成它。