【发布时间】:2011-11-23 04:00:31
【问题描述】:
我无法触发 OnRowCommand。有什么不对劲吗?这是为了显示来自 sql server 的数据库,其中包含来自每一行的链接。它是在 c# 中使用 MVC 和 Visual Studio 2010 构建的
我不知道程序有什么问题,我在这个页面和 site.master 页面上都启用了 ViewStateMode,并且尝试编辑它的时间太长了,看起来有什么问题吗?
顺便说一句,这是我们第一次使用 c# MVC 或 asp.net,所以代码在这方面可能有点草率。
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<Teamsone.Models.Student>" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Registration
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<form id="form1" runat="server">
<h2>Classes offered for Next Semester [Spring 2012]</h2>
<script runat="server">
protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];
// Create a new ListItem object for the contact in the row.
ListItem item = new ListItem();
item.Text = "weeeee!";
ContactsListBox.Items.Add(item);
}
</script>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
OnRowCommand="GridView1_RowCommand" allowpaging="true" DataKeyNames="SectionKey"
DataSourceID="SqlDataSource1" EnableViewState = "true">
<Columns>
<asp:BoundField DataField="FacultyKey" HeaderText="FacultyKey"
SortExpression="FacultyKey" />
<asp:BoundField DataField="SectionKey" HeaderText="SectionKey" ReadOnly="True"
SortExpression="SectionKey" />
<asp:BoundField DataField="CourseKey" HeaderText="CourseKey"
SortExpression="CourseKey" />
<asp:BoundField DataField="SectionDay" HeaderText="SectionDay"
SortExpression="SectionDay" />
<asp:BoundField DataField="SectionTime" HeaderText="SectionTime"
SortExpression="SectionTime" />
<asp:BoundField DataField="SectionSemester" HeaderText="SectionSemester"
SortExpression="SectionSemester" />
<asp:BoundField DataField="SectionYear" HeaderText="SectionYear"
SortExpression="SectionYear" />
<asp:BoundField DataField="ClassroomKey" HeaderText="ClassroomKey"
SortExpression="ClassroomKey" />
<asp:buttonfield buttontype="Link"
commandname="Add"
text="Add"/>
</Columns>
</asp:GridView>
<asp:listbox id="ContactsListBox" runat="server" Height="200px" Width="200px"/>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [NextCourse]"></asp:SqlDataSource>
</form>
</asp:Content>
【问题讨论】:
-
到目前为止,在我看来,您实际上并没有使用 MVC,您似乎陷入了网络表单模式。
-
您可能想阅读 MVC,因为(就像 Anthony Pegram 所说),看起来您正在尝试使用网络表单,而不是 MVC。查看asp.net/mvc/tutorials