将代码从我的.aspx页面转换为C#.cs页面中的代码

时间:2015-11-18 16:37:14

标签: c# asp.net gridview webforms

学习如何通过Visual Studio在ASP.NET中进行编码具有优缺点。优点是通过拖放操作一切都很容易,但是我不知道背后的代码和所有这些“数据绑定”。我在.aspx页面中有这个代码,但我不确定如何将其“转换”为后面的代码。例如,我现在正在尝试实现一个本质上是可搜索的应用程序(具有下拉列表作为要搜索的列的过滤器)Gridview具有具有读写功能的分页。这是我目前在.aspx页面中的代码:

<head runat="server">
    <title></title>
    <style>
        .hiddencol 
        { 
            display: none; 
        }
     </style>
</head>
<body>
<form id="form1" runat="server">
<div>
    <asp:TextBox ID="SearchBox" runat="server"></asp:TextBox>
    <asp:DropDownList ID="SearchParameterList" runat="server">
        <asp:ListItem Selected="True" Value="Project_Name">Project Name</asp:ListItem>
        <asp:ListItem Value="Product">Product</asp:ListItem>
        <asp:ListItem Value="Description">Description</asp:ListItem>
    </asp:DropDownList>
    <asp:Button ID="SearchButton" runat="server" Text="Search" OnClick="SearchButton_Click" />
    <asp:Button ID="ClearButton" runat="server" OnClick="ClearButton_Click" Text="Clear" />
    <br />
    <br />

    <asp:GridView ID="ProjectTable" runat="server" Font-Names="Verdana,Arial" Font-Size="12px" AutoGenerateColumns="False" AutoGenerateEditButton="True" DataSourceID="PopulateProjectTable" AllowPaging="True" OnPageIndexChanging="ProjectTable_PageIndexChanging>
        <AlternatingRowStyle BackColor="#BFE4FF" />
        <PagerStyle BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" />
        <HeaderStyle Height="30px" BackColor="#6DC2FF" Font-Size="12px" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" />
        <RowStyle Height="20px" Font-Size="12px" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" />
        <Columns>
            <asp:BoundField DataField="ProjID" HeaderText="ProjID" ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol" SortExpression="ProjID" />
            <asp:BoundField DataField="Project_Name" HeaderText="Project Name Value" ReadOnly="True" SortExpression="Project_Name" />
            <asp:BoundField DataField="Product" HeaderText="Product" SortExpression="Product" ReadOnly="True" />
            <asp:BoundField DataField="Product_Edit" HeaderText="Product (alternate)" SortExpression="Product_Edit" />
            <asp:BoundField DataField="Description" HeaderText="Description" ReadOnly="True" SortExpression="Description" />
            <asp:BoundField DataField="Description_Edit" HeaderText="Description (alternate)" SortExpression="Description_Edit" />
            <asp:BoundField DataField="Comment" HeaderText="Comment Submission/Approval" SortExpression="Comment" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="PopulateProjectTable" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ODSConnectionString %>" 
        SelectCommand="SELECT [Project_Name], [Product], [Product_Edit], [Description], [Description_Edit], [Comment], [ProjID] FROM [Pipeline_Detail]" 
        UpdateCommand="UPDATE [Pipeline_Detail] SET Product_Edit = @Product_Edit, Description_Edit= @Description_Edit, Comment = @Comment WHERE ProjID = @ProjID">
        <UpdateParameters>
            <asp:Parameter Name="ProjID"/>
            <asp:Parameter Name="Product_Edit" Type="String"/>
            <asp:Parameter Name="Description_Edit" Type="String"/>        
            <asp:Parameter Name="Comment" Type="String"/>
        </UpdateParameters>
    </asp:SqlDataSource>

</div>
</form>

以下是代码背后的内容:

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void SearchButton_Click(object sender, EventArgs e)
    {
        PopulateProjectTable.FilterExpression = SearchParameterList.SelectedValue.ToString() + " LIKE " + "'" + "%{0}%" + "'";
    }

    protected void ClearButton_Click(object sender, EventArgs e)
    {
        PopulateProjectTable.FilterExpression = "Project_Name LIKE '%%'";
        SearchBox.Text = "";
    }
}

非常贫瘠。我现在遇到的问题是我搜索后的分页“重置”了。例如,如果我搜索一个无法在一个页面中显示的内容,它显然会将其分页。如果我点击不是第1页的任何/所有页面,gridview会重置回原来的select语句,并显示该页面的第2页。我做了一些研究,我发现这是数据绑定方式的问题(如果这不是问题,请随时纠正我)。因此,我如何解决我现在遇到的问题,是否需要将我的.aspx页面转换为C#后面的代码?

感谢您的帮助。

编辑:我的代码背后发生了很大变化:

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void SearchButton_Click(object sender, EventArgs e)
    {
        ProjectTable.PageIndex = 0;
        ProjectTable.DataSourceID = null;
        PopulateProjectTable.SelectCommand = "SELECT [Project_Name], [Product], [Product_Edit], [Description], [Description_Edit], [Comment], [ProjID] FROM [Pipeline_Detail] WHERE " + SearchParameterList.SelectedValue.ToString() + " LIKE '%" + SearchBox.Text + "%'";
        ProjectTable.DataSource = PopulateProjectTable;
        ProjectTable.DataBind();
    }

    protected void ClearButton_Click(object sender, EventArgs e)
    {
        ProjectTable.PageIndex = 0;
        ProjectTable.DataSourceID = null;
        PopulateProjectTable.SelectCommand = "SELECT [Project_Name], [Product], [Product_Edit], [Description], [Description_Edit], [Comment], [ProjID] FROM [Pipeline_Detail] WHERE Project_Name LIKE '%%'";
        ProjectTable.DataSource = PopulateProjectTable;
        ProjectTable.DataBind();
        SearchBox.Text = "";
    }

    protected void ProjectTable_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        ProjectTable.PageIndex = e.NewPageIndex;
        ProjectTable.DataSourceID = null;
        PopulateProjectTable.SelectCommand = "SELECT [Project_Name], [Product], [Product_Edit], [Description], [Description_Edit], [Comment], [ProjID] FROM [Pipeline_Detail] WHERE " + SearchParameterList.SelectedValue.ToString() + " LIKE '%" + SearchBox.Text + "%'" ;
        ProjectTable.DataSource = PopulateProjectTable; 
        ProjectTable.DataBind();

    }
}

我现在遇到这些类型的错误:

The GridView 'ProjectTable' fired event RowEditing which wasn't handled.
The GridView 'ProjectTable' fired event RowCancelingEdit which wasn't handled.

任何人都知道发生了什么事?

2 个答案:

答案 0 :(得分:3)

要修复分页问题,​​您需要在每次页面索引更改时重新绑定数据。标记的问题是,虽然您将AllowPaging设置为true,但您从未实现过页面索引更改事件,因此当页面索引更改时数据会“丢失”。这是示例标记和代码隐藏。如果您需要进一步澄清,请与我们联系。

标记:

<asp:GridView ID="ProjectTable" runat="server" Font-Names="Verdana,Arial" Font-Size="12px" AutoGenerateColumns="False" AutoGenerateEditButton="True" DataSourceID="PopulateProjectTable" AllowPaging="True" OnPageIndexChaning="ProjectTable_PageIndexChanging">

代码背后:

protected void ProjectTable_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    //Set the page index
    this.ProjectTable_PageIndexChanging.PageIndex = e.NewPageIndex;

    //Rebind the data
    this.ProjectTable_PageIndexChanging.DataSource = PopulateProjectTable;
    this.ProjectTable_PageIndexChanging.DataBind();
}

答案 1 :(得分:0)

在Page_Load事件中设置断点。

单击第2页时,需要将数据重新绑定到网格。它通常涉及这样的事情:

if(Page.IsPostBack) { 
  ProjectTable.DataSource = [some collection which populates your grid]
  ProjectTable.DataBind();
}