将代码从我的.aspx页面转换为C#.cs页面中的代码
作者:互联网
学习如何通过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语句,并显示那是THAT的页面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.
有人知道怎么回事吗?
解决方法:
要解决分页问题,您需要在每次页面索引更改时重新绑定数据.标记的问题是,尽管将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();
}
标签:gridview,webforms,asp-net,c 来源: https://codeday.me/bug/20191119/2036012.html