我遇到了問題,無論用戶在HtmlSelect控件'drpTeam'中選擇了什麼值,列表中的第一個值(57)是發送到數據庫。這隻發生在我們的兩個測試環境之一中。它不會發生在我的本地開發機器中。服務器端的第一個值<select>總是提交給db無論用戶選擇
我已通過IE的開發工具檢查了選擇控件的客戶端值,它顯示了正確的選定值。但是,當點擊提交按鈕時,它總是提交57(選擇控制列表中的第一個條目)。我用小提琴確認IE發送正確的值,例如。 61.我使用SQL Profiler來檢查SQL Server從實體框架接收的值是多少,因此問題不在數據庫中。我無法通過aspx.cs代碼,因爲這是在測試環境中發生的。不管是否drpConfirmationTeam_SelectedIndexChanged(0)
被稱爲有史以來出現的問題,所以
<select id="drpTeam" runat="server" onchange="drpConfirmationTeam_SelectedIndexChanged(0);"></select>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="drpTeam" CssClass="style1" ErrorMessage="(*)"></asp:RequiredFieldValidator>
:不知何故,不管什麼樣的價值IE發送到Web服務器,Web服務器始終以57
drpTeam取代它被定義爲問題不在於這個功能。
由ASPX頁面drpTeam生成的HTML看起來像這樣:
<select name="ctl00$ContentPlaceHolder1$drpTeam" id="ctl00_ContentPlaceHolder1_drpTeam" Validators="[object HTMLSpanElement]">
<option value="57">
TeamA
</option>
<option value="58">
TeamB
</option>
<option value="59">
TeamC
</option>
...
</select>
代碼提交按鈕點擊事件是以下:
protected void btnSubmit_Click(object sender, EventArgs e)
{
using (var db = new EFDBContext())
{
User user = null;
int userID = Convert.ToInt32(hdnUserID.Value);
int profileChangeRequestID = Convert.ToInt32(hdnPCRID.Value);
int actionerUserID = db.Users.Single(u => u.NTID == User.Identity.Name).UserID;
user = (hdnUserID.Value == "0") ? new User() : db.Users.Single(u => u.UserID == userID);
user.Name = txtName.Text;
user.NTID = txtNTID.Text;
user.RoleID = Convert.ToInt32(drpRole.SelectedValue);
//Error happens in below line:
user.TeamID = Convert.ToInt32(drpTeam.Value); // Always submits 57 (first <option>) for drpTeam.Value
//Error happens in above line^^^^^^
if (drpGroup.SelectedIndex != 0 && drpGroup.SelectedIndex != -1)
user.GroupID = Convert.ToInt32(drpGroup.SelectedValue);
else
user.GroupID = null;
user.Transit = txtTransit.Text;
user.Location = "";
user.FaxNumber = txtFax.Text;
user.Email = txtEmail.Text;
user.PhoneNumber = txtPhoneNumber.Text;
user.Title = txtTitle.Text;
user.ReceiveEmails = chkReceiveEmails.Checked;
user.ReportsToUserID = (drpReportsTo.SelectedIndex != 0) ? (int?)Int32.Parse(drpReportsTo.SelectedValue) : null;
user.isActive = true;
if (hdnUserID.Value == "0")
db.Users.Add(user);
ProfileChangeRequest profileChangeRequest =
db.ProfileChangeRequests.Single(pcr => pcr.ProfileChangeRequestID == profileChangeRequestID);
db.ProfileChangeRequests.Remove(profileChangeRequest);
db.SaveChanges();
bindGrid();
divUserFormConfirm.Style.Add("display", "none");
divConfirmationSuccess.Style.Add("display", "inline");
}
}
我也這麼認爲。但顯然,SelectedValue只適用於類型。由於此DDL定義爲帶有runat =「server」標籤的