0

我建立了一个 ASP.net 项目,但我是新手......

我遇到了这个问题:

有一个 textbox1 和按钮名称 = 过滤器和网格视图

有太表的数据库(标记,课程)

当我在 textbox1 中写一个句子并按过滤器网格视图连接到数据库并根据 textbox1 内容获取数据时,我需要

默认情况下,所有东西都没有过滤器

我做到了,但网格视图没有出现,没有错误

<table >
<tr>
<td colspan= "2" bgcolor="#4B6C9E"  >
<font color = white> Choose The Course :</font>
</td>
</tr>
<tr>
   <td> <asp:Label ID="Label1" runat="server" Text="Course :" ></asp:Label>
   </td>
    <td><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td colspan = "2" align = "center">
    <asp:Button ID="Button1" runat="server" Text="Filter" />
    </td>
    </tr>
</table>

    <br />
<br />
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="SqlDataSource1">
        <Columns>
            <asp:BoundField DataField="StdID" HeaderText="StdID" SortExpression="StdID" />
            <asp:BoundField DataField="Mark" HeaderText="Mark" SortExpression="Mark" />
            <asp:BoundField DataField="CourseID" HeaderText="CourseID" 
                SortExpression="CourseID" />
            <asp:BoundField DataField="StudentName" HeaderText="StudentName" 
                SortExpression="StudentName" />
            <asp:BoundField DataField="CourseName" HeaderText="CourseName" 
                SortExpression="CourseName" />
            <asp:BoundField DataField="Year" HeaderText="Year" SortExpression="Year" />
            <asp:BoundField DataField="Semester" HeaderText="Semester" 
                SortExpression="Semester" />
            <asp:BoundField DataField="Grade" HeaderText="Grade" SortExpression="Grade" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        SelectCommand="SELECT Marks.StdID, Marks.Mark, Marks.CourseID, Marks.StudentName, Course.CourseName, Course.Year, Course.Semester, Course.Grade FROM Marks INNER JOIN Course ON Marks.CourseID = Course.ID WHERE (Course.CourseName LIKE '@name')">
        <SelectParameters>
                 <asp:ControlParameter Name="name" ControlID="TextBox1" PropertyName="Text" DefaultValue="%" Type="String"  />
        </SelectParameters>
    </asp:SqlDataSource>

注意:当我在数据库中执行查询时,它工作得很好

请尽快提供帮助

4

1 回答 1

1

如果没有来自数据源的行,Gridiview 不会显示任何内容。这个子句是错误的“WHERE(Course.CourseName LIKE '@name'”,你不需要把@name放在字符串里面,数据库会搜索一个名为'@name'的课程,直接把参数放在SQL里面。这边走:

 SelectCommand="SELECT Marks.StdID, Marks.Mark, Marks.CourseID, Marks.StudentName, Course.CourseName, Course.Year, Course.Semester, Course.Grade FROM Marks INNER JOIN Course ON Marks.CourseID = Course.ID WHERE (Course.CourseName LIKE @name)"
于 2013-06-16T21:16:17.247 回答