【问题标题】:OnRowCommand not firingOnRowCommand 未触发
【发布时间】: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

标签: c# asp.net sql webforms


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多