【发布时间】:2013-09-18 13:12:12
【问题描述】:
我之前以类似的方式问过这个问题,这个问题仍然没有解决。我有一个由本地数据库填充的下拉列表。当用户选择一个选项时,页面刷新并且索引设置回第一个选项。发生这种情况时,SelectedIndexChanged 方法不会触发,在调试时,我发现在页面加载时,!isPostBack 始终为 true。
我为页面、下拉列表和数据库设置了 enableviewstate true。 以及页面的 AutoEventWireUp true。
请帮忙,我已经坚持这个问题大约一个星期了! 我觉得这个问题可能在我的母版页或 web.config 文件中仍然存在,所以我也发布这些问题
索引.aspx
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<BenchmarkApp.Models.Benchmark>>" AutoEventWireup="true" EnableViewState="true"%>
<!DOCTYPE html>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
DropDownList1.SelectedIndexChanged += new EventHandler(DropDownList1_SelectedIndexChanged);
if (!this.IsPostBack)
{
Response.Write("Post Back is False");
bindData();
}
}
protected void bindData()
{
DropDownList1.Items.Clear();
DropDownList1.DataSourceID = "SqlDataSource1";
DropDownList1.DataTextField = "Version";
DropDownList1.DataValueField = "Id";
DropDownList1.DataBind();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Write((sender as DropDownList).SelectedItem.Text);
Label1.Text = DropDownList1.SelectedItem.Text;
}
</script>
<html>
<head runat="server">
<meta name="viewport" content="width=device-width" />
<title>Benchmark</title>
</head>
<body>
<form id="bmform" runat="server">
<p>
<%: Html.ActionLink("Create New", "Create") %>
</p>
<table border="1">
<tr>
<th>
Build version:
</th>
<th>
<%-- Html.DropDownList("Builds", null, new {@onchange = "onChange(this.value);" }) --%>
<%-- Html.DropDownList("BuildID", (SelectList) ViewBag.Builds, "--Select One--") --%>
<%-- Html.DropDownList("BuildDD", (IEnumerable<SelectListItem>)ViewBag.Builds, "--Select One--") --%>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource1" DataTextField="Version"
onselectedindexchanged="DropDownList1_SelectedIndexChanged"
DataValueField="Id">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DAContext %>"
SelectCommand="SELECT [Id], [Version] FROM [Builds]"></asp:SqlDataSource>
</th>
<th>
<asp:Label ID="Label1" runat="server" Text= "--Build Version--"></asp:Label>
</th>
</tr>
</table>
<table border="1">
<tr>
<th>
<%: Html.DisplayNameFor(model => model.Script.Name) %>
</th>
<th>
<%: Html.DisplayNameFor(model => model.Build.Version) %>
</th>
<th>
<%: Html.DisplayNameFor(model => model.MinTime) %>
</th>
<th>
<%: Html.DisplayNameFor(model => model.MeanTime) %>
</th>
<th>
<%: Html.DisplayNameFor(model => model.MaxTime) %>
</th>
<th>
</th>
</tr>
<% foreach (var item in Model)
{ %>
<tr>
<td>
<%: Html.DisplayFor(modelItem => item.Script.Name) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.Build.Version) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.MinTime) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.MeanTime) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.MaxTime) %>
</td>
<td>
<%: Html.ActionLink("Edit", "Edit", new { id=item.BenchmarkID }) %>
|
<%: Html.ActionLink("Details", "Details", new { id=item.BenchmarkID }) %>
|
<%: Html.ActionLink("Delete", "Delete", new { id=item.BenchmarkID }) %>
</td>
</tr>
<% } %>
</table>
</form>
</body>
</html>
大师
<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" AutoEventWireup="true" EnableViewState="true" %>
<!DOCTYPE html>
<html lang="en">
<head runat="server">
<meta charset="utf-8" />
<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
<link href="<%: Url.Content("~/favicon.ico") %>" rel="shortcut icon" type="image/x-icon" />
<meta name="viewport" content="width=device-width" />
<%: Styles.Render("~/Content/css") %>
<%: Scripts.Render("~/bundles/modernizr") %>
</head>
<body>
<form id="master" runat="server">
<header>
<div class="content-wrapper">
<div class="float-left">
<p class="site-title"><%: Html.ActionLink("your logo here", "Index", "Home") %></p>
</div>
<div class="float-right">
<section id="login">
<%: Html.Partial("_LoginPartial") %>
</section>
<nav>
<ul id="menu">
<li><%: Html.ActionLink("Home", "Index", "Home") %></li>
<li><%: Html.ActionLink("About", "About", "Home") %></li>
<li><%: Html.ActionLink("Contact", "Contact", "Home") %></li>
</ul>
</nav>
</div>
</div>
</header>
<div id="body">
<asp:ContentPlaceHolder ID="FeaturedContent" runat="server" />
<section class="content-wrapper main-content clear-fix">
<asp:ContentPlaceHolder ID="MainContent" runat="server" />
</section>
</div>
<footer>
<div class="content-wrapper">
<div class="float-left">
<p>© <%: DateTime.Now.Year %> - My ASP.NET MVC Application</p>
</div>
</div>
</footer>
<%: Scripts.Render("~/bundles/jquery") %>
<asp:ContentPlaceHolder ID="ScriptsSection" runat="server" />
</form>
</body>
</html>
Web.config
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization"/>
<add namespace="System.Web.Routing" />
</namespaces>
</pages>
</system.web.webPages.razor>
<appSettings>
<add key="webpages:Enabled" value="false" />
</appSettings>
<system.web>
<httpHandlers>
<add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>
<!--
Enabling request validation in view pages would cause validation to occur
after the input has already been processed by the controller. By default
MVC performs request validation before a controller processes the input.
To change this behavior apply the ValidateInputAttribute to a
controller or action.
-->
<pages
validateRequest="false"
pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<controls>
<add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
</controls>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
</configuration>
非常感谢您!
【问题讨论】:
-
为什么要混合 MVC 和 WebForms?
-
请不要因为没有得到答案就转发the same question。随意使用该问题上的“编辑”链接,使用您获得的任何新信息对其进行更新。
标签: asp.net asp.net-mvc asp.net-mvc-4 drop-down-menu postback