在按钮上筛选GridView在Dropdownlist和Textboxes的基础上单击
本文关键字:Textboxes 基础上 单击 Dropdownlist 按钮 筛选 GridView | 更新日期: 2023-09-27 18:01:04
我需要的是。我想根据我想要的数据过滤并显示GridView
中的记录。
例如:-我有一个dropdownlist
和Textbox
,我将在其中选择要筛选的数据,然后单击按钮,相关数据将显示在网格视图中。
请注意:-Dropdownlist
、Textboxes
和Button
都在网格视图外。
我尝试了Here中的代码,但实际上并不是我想要的。请查看下面的代码。
ASPX
<div>
<table>
<tr>
<td>
<asp:Label ID="lblProject" runat="server" Text="Project Name">Project Name</asp:Label>
</td>
<td>
<asp:DropDownList ID="ddlProjectName" runat="server" AutoPostBack="true">
</asp:DropDownList>
</td>
<td>
<asp:Label ID="lblFrmDate" runat="server" Text="From Date">From Date</asp:Label>
</td>
<td>
<input id="TxtIndate" runat="server" maxlength="20" type="text" style="text-align: right;
width: 75px" readonly="readonly" />
<cc3:Calendar ID="Calendar2" runat="server" CultureName="en-GB" DatePickerImagePath="Images/icon2.gif"
DatePickerMode="true" TextBoxId="TxtIndate">
</cc3:Calendar>
</td>
<td>
<asp:Label ID="lblToDate" runat="server" Text="To Date">To Date</asp:Label>
</td>
<td>
<input id="txtOutDate" runat="server" maxlength="20" type="text" style="text-align: right;
width: 75px" readonly="readonly" />
<cc3:Calendar ID="Calendar1" runat="server" CultureName="en-GB" DatePickerImagePath="Images/icon2.gif"
DatePickerMode="true" TextBoxId="txtOutDate">
</cc3:Calendar>
</td>
<td>
<asp:Button ID="btnSubmit" runat="server" Width="100" Text="Search" />
</td>
</tr>
</table>
</div>
<br />
<br />
<div>
<asp:GridView ID="grdData" runat="server" AutoGenerateColumns="false">
</asp:GridView>
</div>
另请参阅CS代码:-
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
fillProject();
}
grdData.Visible = false;
}
private void fillProject()
{
// dt.Clear();
ObjPriDaAdp = new OracleDataAdapter("select distinct Project_id, PROPERTY_NAME Project_name from xxcus.XXACL_PN_PROJBUILD_V order by PROPERTY_NAME", ObjPriCon);
ObjPriDaAdp.Fill(dt);
ddlProjectName.DataTextField = "Project_name";
ddlProjectName.DataValueField = "Project_id";
ddlProjectName.DataSource = dt;
ddlProjectName.DataBind();
ddlProjectName.Items.Insert(0, new ListItem("--- Select ---", "0"));
}
请建议在这种情况下如何处理。也许我不能从这些开始:-
此外,我想在网格视图中显示Date
、ProjectName
、VehicleNO
、TimeIn
、TimeOut
列
IN CS页面
protected void btnSubmit_Click(object sender, EventArgs e)
{
string query = "SELECT T1.Project_Id,T2.Project_name,T1.Vehicle_No,T1.Creation_date,T1.Time_In,T1.Time_Out FROM XXCUS.XX_SUPINV T1 INNER JOIN XXACL_PN_PROJBUILD_V T2 ON T1.Project_Id = T2.Project_id WHERE Project_Id = '"+ ddlProjectName.SelectedValue +"' AND Creation_date BETWEEN Convert(datetime,'"+lblFrmDate.Text+"') AND Convert(datetime,'"+lblToDate.Text+"')"
SqlDataAdapter da = new SqlDataAdapter(query, connection);
DataTable dt = new DataTable();
da.Fill(dt);
grdData.DataSource = dt;
grdData.DataBind();
}
ASPX页面中的AND
<asp:GridView ID="grdData" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
CellPadding="3" GridLines="Vertical">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:BoundField HeaderText="PROPERTY NAME" DataField="PROPERTY_NAME" SortExpression="PROPERTY_NAME">
<HeaderStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField HeaderText="Vehicle No" DataField="Vehicle_No" SortExpression="Vehicle_No">
<HeaderStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField HeaderText="Creation date" DataField="Creation_date" SortExpression="Creation_date">
<HeaderStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField HeaderText="Time In" DataField="Time_In" SortExpression="Time_In">
<HeaderStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField HeaderText="Time Out" DataField="Time_Out" SortExpression="Time_Out">
<HeaderStyle HorizontalAlign="Center" />
</asp:BoundField>
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
在按钮"btnSubmit"上单击事件,保持文本框和下拉列表的值,然后将该值传递给sql查询,如:
protected void btnSubmit_Click(object sender, EventArgs e)
{
//connection
string query = "select * from tablename where Projectname like "+ddlProjectName.SelectedValue+"%";// this is an sample query change as per your requirement
SqlDataAdapter da = new SqlDataAdapter(query,connection);
DataTable dt = new DataTable();
da.Fill(dt);
grdData.DataSource =dt;
grdData.DataBind();
}
这里有一个链接可能会有更多帮助:http://www.c-sharpcorner.com/UploadFile/0c1bb2/searching-records-from-database-and-display-in-gridview-usin/