按钮没有数据绑定到正确的模型

时间:2014-07-08 17:02:28

标签: c# ado.net

我遇到的问题是我创建的按钮应该数据绑定到gridview。它应该按姓氏搜索作者,但是当我点击按钮时没有任何反应。任何人都可以帮我解决我的代码吗?

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="BookForm.aspx.cs" Inherits="BookWebApp.BookForm" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        Enter Author&#39;s Last Name:
        <asp:TextBox ID="lastNameBox" runat="server"></asp:TextBox>
        <asp:Button ID="applyButton" runat="server" OnClick="applyButton_Click" Text="Apply" />

    </div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ISBN" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <Columns>
                <asp:BoundField DataField="ISBN" HeaderText="ISBN" ReadOnly="True" SortExpression="ISBN" />
                <asp:BoundField DataField="Title1" HeaderText="Title1" SortExpression="Title1" />
                <asp:BoundField DataField="EditionNumber" HeaderText="EditionNumber" SortExpression="EditionNumber" />
                <asp:BoundField DataField="Copyright" HeaderText="Copyright" SortExpression="Copyright" />
            </Columns>
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>
        <asp:EntityDataSource ID="bookEntity" runat="server" ConnectionString="name=BooksEntities" DefaultContainerName="BooksEntities" EnableFlattening="False" EntitySetName="Titles">
        </asp:EntityDataSource>
    </form>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

首先,您应该将Page_Load更改为

if(!Page.IsPostBack)
{
    // load Authors table ordered by LastName then FirstName
    dbcontext.Authors
       .OrderBy(author => author.LastName)
       .ThenBy(author => author.FirstName)
       .Load();

    // specify DataSource for authorBindingSource
    authorBind.DataSource = dbcontext.Authors.Local;
}

单击该按钮时,您不想重新加载所有数据。它还可能导致一些意想不到的行为。

然后,如果您在DataBind上致电GridView,它会有帮助吗?诚然,我没有看到GridView被分配到BindingSource的位置,但我相信它正在发挥作用。

答案 1 :(得分:0)

查看按钮是asp按钮还是简单的html按钮。并检查它是否有runat =随附的服务器标签。