文本框中的水印取决于所选的下拉ID

本文关键字:ID 文本 取决于 | 更新日期: 2023-09-27 17:57:27

我在Visual Studio中连接了一个SQL Server数据库,并在网格中显示其内容。我创建了一个下拉菜单,其中列名是可选选项,还有一个文本字段用于筛选特定内容,例如,dropdown="Start"-Textfield=14.03.2015=为每个包含"14.03.2015"的条目筛选列"Start",并将其显示在网格中。

为了使用法更加直观,例如,当从下拉列表中选择一个需要在文本框中输入日期的选项时,我希望显示类似"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> &nbsp;
    <asp:TextBox ID="TextBox1" runat="server" Width="315px"></asp:TextBox> &nbsp;

    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Search" Width="100px"/> &nbsp;
    <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); 
        }

文本框中的水印取决于所选的下拉ID

HTML5允许<input>标记的placeholder属性。假设您的网站上启用了jQuery,那么您可以在客户端更改DropDownList1:时触发以下操作

$("#TextBox1").attr("placeholder", $("#DropDownList1").find("option:selected").html());