所有人。
我正在使用C#和ASP.NET为公司开发Web应用程序。
我有Default.aspx页面和一个名为wucUdc.ascx的用户控件,当用户按下Default.aspx页面中的按钮时,该控件就会加载到Default.aspx页面中。
在用户控件中,有一个ASP.NET GridView,其中填充了存储过程的结果。
我想为GridView标题中的每一列(动态)添加一个TextBox,以便在用户每次在其中写入内容时用作列过滤器。
我将在下面向您展示代码。
Default.aspx
<%@ Page EnableViewState="true" Language="C#" CodeBehind="Default.aspx.cs" Inherits="DynamicStoreWeb._Default" %>
<%@ Reference Control="~/wucUdc.ascx"%>
<link href="Default.css" rel="stylesheet" type="text/css" />
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>DynamicStore Web App</title>
</head>
<body>
<form id="form" runat="server">
<div id="primary-menu" class="primary-menu">
<asp:ImageButton id="btnUDC" runat="server"
AlternateText="UDC"
ImageUrl="Resources/shipping-and-delivery.png"
Height="80"
Width="80"
BorderColor="#000000"
BorderStyle="Solid"
BorderWidth="2"
CssClass="button"
OnClick="btnUDC_OnClick"
/>
<asp:ImageButton id="btnCelle" runat="server"
AlternateText="Celle"
ImageUrl="Resources/stock.png"
Height="80"
Width="80"
BorderColor="#000000"
BorderStyle="Solid"
BorderWidth="2"
CssClass="button"
OnClick="btnCelle_OnClick"
/>
<asp:ImageButton id="btnSpedizioni" runat="server"
AlternateText="Spedizioni"
ImageUrl="Resources/truck.png"
Height="80"
Width="80"
BorderColor="#000000"
BorderStyle="Solid"
BorderWidth="2"
CssClass="button"
OnClick="btnSpedizioni_OnClick"
/>
</div>
<section>
<nav>
<ul style="font-family: Tahoma;">
<li><a href="#">Example</a></li>
<li><a href="#">Example</a></li>
<li><a href="#">Example</a></li>
<li><a href="#">Example</a></li>
<li><a href="#">Example</a></li>
<li><a href="#">Example</a></li>
<li><a href="#">Example</a></li>
<li><a href="#">Example</a></li>
<li><a href="#">Example</a></li>
<li><a href="#">Example</a></li>
<li><a href="#">Example</a></li>
<li><a href="#">Example</a></li>
<li><a href="#">Example</a></li>
<li><a href="#">Example</a></li>
<li><a href="#">Example</a></li>
<li><a href="#">Example</a></li>
<li><a href="#">Example</a></li>
<li><a href="#">Example</a></li>
<li><a href="#">Example</a></li>
<li><a href="#">Example</a></li>
<li><a href="#">Example</a></li>
<li><a href="#">Example</a></li>
<li><a href="#">Example</a></li>
<li><a href="#">Example</a></li>
<li><a href="#">Example</a></li>
</ul>
</nav>
<article runat="server" enableviewstate="true">
<asp:PlaceHolder ID="PlaceHolder" runat="server" EnableViewState="true"></asp:PlaceHolder>
</article>
</section>
<footer>
<p style="font-family: Tahoma;">DynamicStore Web App</p>
</footer>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace DynamicStoreWeb
{
public partial class _Default : System.Web.UI.Page
{
HttpCookie cookie = new HttpCookie("Scelta");
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["Scelta"] != null)
{
HttpCookie cookie = Request.Cookies["Scelta"];
cookie.Expires = DateTime.Now.AddDays(-1);
UserControl uc = (UserControl)Page.LoadControl(cookie["command"]);
PlaceHolder.Controls.Add(uc);
}
}
protected void btnUDC_OnClick(object sender, EventArgs e)
{
UserControl uc = (UserControl)Page.LoadControl("~/wucUdc.ascx");
PlaceHolder.Controls.Add(uc);
cookie["command"] = "~/wucUdc.ascx";
Response.Cookies.Add(cookie);
}
}
}
wucUdc.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="wucUdc.ascx.cs" Inherits="DynamicStoreWeb.wucUdc" %>
<link href="wucUdc.css" rel="stylesheet" type="text/css" />
<div runat="server">
<asp:Label ID="DateFromLbl" runat="server" Text="Dal giorno: " CssClass="label"></asp:Label>
<asp:TextBox ID="DateFrom" runat="server" Width="130px" TextMode="Date" CssClass="dropdown" Font-Names="Tahoma">2000-01-01</asp:TextBox>
<asp:Label ID="DateToLbl" runat="server" Text="Al giorno: " CssClass="label"></asp:Label>
<asp:TextBox ID="DateTo" runat="server" Width="130px" TextMode="Date" CssClass="dropdown" Font-Names="Tahoma"></asp:TextBox>
<asp:Label ID="Ricerca" runat="server" Text="Ricerca: " CssClass="label"></asp:Label>
<asp:TextBox ID="Search" runat="server" Width="95px" TextMode="SingleLine"></asp:TextBox>
<asp:Label ID="Risultati" runat="server" Text="Ultimi risultati: " CssClass="label"></asp:Label>
<asp:CheckBox ID="TopResults" runat="server" AutoPostBack="true" EnableViewState="true" ViewStateMode="Enabled"/>
<asp:Button ID="btnSubmit" runat="server" Text="Visualizza UDC" CssClass="control-button" OnClick="btnSubmit_Click"/>
<div style = "overflow-x:auto; width:100%">
<asp:GridView ID="GridView" runat="server" CssClass="mydatagrid" PagerStyle-CssClass="pager" HeaderStyle-CssClass="header" RowStyle-CssClass="rows" AllowPaging="True" EnableSortingAndPagingCallbacks="True" AutoGenerateEditButton="False" ShowHeaderWhenEmpty="True">
</asp:GridView>
</div>
</div>
wucUdc.ascx.cs
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
namespace DynamicStoreWeb
{
public partial class wucUdc : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
DateTo.Text = DateTime.Today.ToString("yyyy-MM-dd");
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
string conn = "";
conn = ConfigurationManager.ConnectionStrings["Connection"].ToString();
SqlConnection objsqlconn = new SqlConnection(conn);
try
{
objsqlconn.Open();
SqlCommand objcmd = new SqlCommand("DY_FindUdcList", objsqlconn);
objcmd.CommandType = CommandType.StoredProcedure;
SqlParameter RCp = objcmd.Parameters.Add("@RC", SqlDbType.Int);
RCp.Direction = ParameterDirection.ReturnValue;
SqlParameter DateFromp = objcmd.Parameters.Add("@DateFrom", SqlDbType.DateTime);
DateFromp.Value = DateFrom.Text;
SqlParameter DateTop = objcmd.Parameters.Add("@DateTo", SqlDbType.DateTime);
DateTop.Value = DateTo.Text;
SqlParameter UdcCelp = objcmd.Parameters.Add("@UdcCel", SqlDbType.Int);
UdcCelp.Value = -1;
SqlParameter TopResultsp = objcmd.Parameters.Add("@TopResults", SqlDbType.Bit);
if (TopResults.Checked)
{
TopResultsp.Value = 1;
}
else
{
TopResultsp.Value = 0;
}
SqlParameter Searchp = objcmd.Parameters.Add("@Search", SqlDbType.VarChar);
Searchp.Value = Search.Text;
SqlParameter Operp = objcmd.Parameters.Add("@Oper", SqlDbType.VarChar);
Operp.Value = "";
SqlParameter Termp = objcmd.Parameters.Add("@Term", SqlDbType.VarChar);
Termp.Value = "";
SqlParameter Errorep = objcmd.Parameters.Add("@Errore", SqlDbType.VarChar);
Errorep.Value = "";
Errorep.Direction = ParameterDirection.Output;
SqlDataAdapter adapter = new SqlDataAdapter(objcmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
GridView.DataSource = dt;
GridView.DataBind();
objcmd.Dispose();
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
finally
{
objsqlconn.Close();
}
}
}
}