Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting code from my .aspx page to code behind in the C# .cs page

Learning how to code in ASP.NET via Visual Studio comes with its pros and cons. The pros are that everything is so easy to do via drag and drop, but the con is that I don't know much about the code behind and all of this "data binding". I have this code in my .aspx page, but I'm not sure how to "convert" it to the code behind. For example, I am right now attempting to implement an application that is in essence a searchable (that has a drop down list to serve as a filter on which column to search on) Gridview with paging that has read and write capabilities. Here is the code I have thus far in my .aspx page:

<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>

And here is what I have in the code behind:

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 = "";
    }
}

Very barren. The issue I'm running into right now is that the paging "resets" itself after I search. For example, if I search up something that can't be displayed in one page, it'll obviously page it. If I click on any/all pages that isn't Page 1, the gridview resets back to its original select statement and shows what would be page 2 of THAT. I did some research and I see that it is an issue with the way the data is bound (feel free to correct me if that isn't the issue here). As such, how can I remedy the problem I'm having right now, and will it require me to convert my .aspx page to code behind in C#?

Thanks for any help.

EDIT: I made a big change with my code behind:

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();

    }
}

I'm now getting these type of errors:

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

Anyone know what's up?

like image 649
Leon Avatar asked Feb 04 '26 00:02

Leon


1 Answers

To fix the paging issue you need to rebind the data on every page index change. The issue with your markup is although you have AllowPaging set to true you never implemented the on page index changing event thus the data gets "lost" when the page index changes. Here is example markup and code behind. Please let me know if you need further clarification.

MarkUp:

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

Code Behind:

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();
}
like image 122
John Paul Avatar answered Feb 06 '26 13:02

John Paul