2015-06-22 76 views
1

我在Visual Studio中有一個連接的SQL Server數據庫,並在網格中顯示其內容。我創建了一個下拉菜單,將列名稱設置爲可選選項,並使用文本字段過濾特定內容,例如,DropDown =「Start」 - Textfield = 14.03.2015 =每個包含「14.03.2015」 「 - 並將其顯示在網格中。取決於所選下拉ID的文本框中的水印

爲了讓使用更加直觀,我想在例如下拉菜單中選擇需要日期作爲輸入的文本框時,顯示「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語句中。但我只是缺乏關於如何做到這一點的知識。 事情是這樣的:

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

HTML5允許<input>標籤的placeholder屬性。假設你在你的網站上啓用了jQuery,你可以做的是在客戶端更改時觸發類似於以下內容:DropDownList1

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

嗨,Adam。謝謝您的回答。我假設我把這段代碼放在我的c#if語句中? .aspx中的對應內容是什麼樣的?我的文本框現在看起來像這樣:我想每次更改佔位符dynamicaly更改下拉列表的值。 – Andre

+0

啊。我曾使用過jQuery。這是純粹的客戶端解決方案。抱歉。 :\ 從後端看來,您需要做的是使自定義控件從具有「佔位符」屬性的文本框控件繼承,然後在刷新輸入時進行更新。 以下內容可能會有用:[HTML5佔位符支持](http://www.kashyapas.com/2013/02/03/html5-placeholder-support-for-asp-net-webform-textbox/)。這篇文章已經有幾年了,現在有些截圖現在已經無法恢復,但它會突出顯示我的想法。 –