我在Visual Studio中有一个连接的SQL Server数据库,并在网格中显示其内容。我创建了一个下拉菜单,其中列名称为可选选项,文本字段用于过滤特定内容,例如,DropDown =“Start” - Textfield = 14.03.2015 =筛选列“开始”,表示包含“14.03.2015”的每个条目“ - 并将其显示在网格中。
为了使用更直观,我想显示像“dd.mm.yyyy”这样的文本,例如选择下拉列表中的选项,需要在我的文本框中输入日期。
网格看起来像这样:http://abload.de/img/untitled123yqkyn.png
您可以在下面找到我的网格代码:
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>Organizer</asp:ListItem>
<asp:ListItem>Room</asp:ListItem>
<asp:ListItem>Creation Time</asp:ListItem>
<asp:ListItem>Start</asp:ListItem>
<asp:ListItem>End</asp:ListItem>
<asp:ListItem>Last Modified</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="TextBox1" runat="server" Width="315px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Search" Width="100px"/>
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Reset Search" Width="100px"/>
<br/>
<br/>
<asp:GridView ID="GridView1" runat="server" GridLines="Both" BorderColor="White" BorderStyle="Solid" CellPadding="4" ForeColor="#333333" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource1" AllowSorting="True" pagesize="1000" AllowPaging="True" HorizontalAlign="Center" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<AlternatingRowStyle BackColor="White"/>
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" ItemStyle-HorizontalAlign="Center" ItemStyle-BorderColor="White"/>
<asp:BoundField DataField="Organizer" HeaderText="Organizer" SortExpression="Organizer" ConvertEmptyStringToNull="False" HtmlEncode="False" HtmlEncodeFormatString="False" InsertVisible="False" ItemStyle-BorderColor="White"/>
<asp:BoundField DataField="Room" HeaderText="Room" SortExpression="Room" ItemStyle-BorderColor="White"/>
<asp:BoundField DataField="DateTimeCreated" HeaderText="Creation Time" SortExpression="DateTimeCreated" ItemStyle-BorderColor="White"/>
<asp:BoundField DataField="Start" HeaderText="Start" SortExpression="Start" ItemStyle-BorderColor="White"/>
<asp:BoundField DataField="End" HeaderText="End" SortExpression="End" ItemStyle-BorderColor="White"/>
<asp:BoundField DataField="LastModifiedTime" HeaderText="Last Modified" SortExpression="LastModifiedTime" ItemStyle-BorderColor="White"/>
<asp:CheckBoxField DataField="Cancelled" HeaderText="Cancelled" SortExpression="Cancelled" ItemStyle-HorizontalAlign="Center" ItemStyle-BorderColor="White"/>
</Columns>
<EditRowStyle BackColor="#2461BF"/>
<FooterStyle BackColor="#E1000F" Font-Bold="True" ForeColor="White"/>
<HeaderStyle BackColor="#E1000F" Font-Bold="True" ForeColor="White" Font-Underline="false"/>
<PagerStyle BackColor="#E1000F" ForeColor="White" HorizontalAlign="Center"/>
<RowStyle BackColor="#F9F9F9"/>
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333"/>
</asp:GridView>
<asp:SqlDataSource ID="xyz" runat="server" ConnectionString="<%$ ConnectionStrings:xyz %>" SelectCommand="SELECT * FROM [xyz]"></asp:SqlDataSource>
</center>
我用来过滤网格的C#代码:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string FilterExpression = string.Empty;
if (DropDownList1.SelectedValue.ToString().Equals("Start"))
{
FilterExpression = string.Format("Start >= '{0} 0:00:00' AND Start <= '{0} 23:59:59'", TextBox1.Text);
}
else if (DropDownList1.SelectedValue.ToString().Equals("End"))
{
FilterExpression = string.Format("End >= '{0} 0:00:00' AND End <= '{0} 23:59:59'", TextBox1.Text);
}
else if (DropDownList1.SelectedValue.ToString().Equals("Creation Time"))
{
FilterExpression = string.Format("DateTimeCreated >= '{0} 0:00:00' AND DateTimeCreated <= '{0} 23:59:59'", TextBox1.Text);
}
else if (DropDownList1.SelectedValue.ToString().Equals("Last Modified"))
{
FilterExpression = string.Format("LastModifiedTime >= '{0} 0:00:00' AND LastModifiedTime <= '{0} 23:59:59'", TextBox1.Text);
}
else
{
FilterExpression = string.Concat(DropDownList1.SelectedValue, " Like '%{0}%'");
}
SqlDataSource1.FilterParameters.Clear();
SqlDataSource1.FilterParameters.Add(new ControlParameter(DropDownList1.SelectedValue, "TextBox1", "Text"));
SqlDataSource1.FilterExpression = FilterExpression;
}
protected void Button2_Click(object sender, EventArgs e)
{
TextBox1.Text = string.Empty;
SqlDataSource1.FilterParameters.Clear();
}
protected void ImageButton_Click(object sender, EventArgs e)
{
TextBox1.Text = string.Empty;
SqlDataSource1.FilterParameters.Clear();
}
我想将它附加到if / else if Statments。但我只是缺乏如何做到这一点的知识。 像这样:
if (DropDownList1.SelectedValue.ToString().Equals("Start"))
{
Displaywatermark ("dd.mm.yyyy");
FilterExpression = string.Format("Start >= '{0} 0:00:00' AND Start <= '{0} 23:59:59'", TextBox1.Text);
}
答案 0 :(得分:0)
您可以使用MaskedEdit Validator
中的AjaxControlToolkit
。它为您的文本框设置了预定义的格式,并阻止用户输入除此之外的任何内容。我在我的一个项目中使用了这个控件,它就像一个魅力。控件的行为在运行时在javascript中呈现,它可以提供流畅的用户体验,而无需回发到服务器。
更多详情 - http://www.ajaxcontroltoolkit.com/MaskedEdit/MaskedEdit.aspx