2011-11-23 44 views
0

我无法获取OnRowCommand开火。什么看起来不对?这是为了显示来自sql server的数据库,每个行都有链接。它是建立在C#使用MVC和visual studio 2010我不知道什么可能是错误的程序,我已经在此页面和site.master页面上启用ViewStateMode,并试图编辑它的方式太长,有什么看起来不对?OnRowCommand未开火

顺便说一下,这是我们第一次使用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> 
+1

到目前为止,它看起来并不像你对我实际上是使用* * MVC,你似乎陷入了webforms模式。 –

+1

你可能想阅读MVC,因为(就像Anthony Pegram所说的那样),它看起来像是在尝试使用webforms而不是MVC。查看http://www.asp.net/mvc/tutorials –

回答

1

ASP.NET MVC使用视图只呈现HTML - 所以在这种情况下,如果你使用ASP.NET网页或用户控制,然后查看它只会渲染输出使用HTML。

由于POST请求将被ASP.NET MVC控制器拦截,包含控件事件的整个回发模型将不起作用。 ASP.NET页面没有机会处理请求(并随后引发回发事件)。

而且,你不似乎明白了ASP.NET MVC模型,你要开始阅读更多关于它 - 见http://msdn.microsoft.com/en-us/magazine/cc337884.aspx