2015-08-15 139 views
1

我想隱藏txtsearchRadioButtonListvalue = OrderDate隱藏txtsearch,如果任何其他值選擇顯示隱藏文本框基於單選按鈕列表值

顯示txtsearch我想它下面的代碼沒有工作

if (!IsPostBack) 
{ 
    if (lblfield.SelectedValue == "OrderDate") 
    { 
     txtsearch.Visible = false; 
    } 
    else 
    { 
     txtsearch.Visible = true; 
    } 
} 

<asp:RadioButtonList ID="lblfield" runat="server" CellPadding="10" CellSpacing="10" RepeatDirection="Horizontal" AutoPostBack="true" Width="460px"> 
     <asp:ListItem Selected="True" Value="Cust_Name">Customer</asp:ListItem> 
     <asp:ListItem Value="OrderDate">Order Date</asp:ListItem> 
     <asp:ListItem Value="TotalAmount">Total Amount</asp:ListItem> 
     <asp:ListItem Value="InvoiceStatus">Invoice Status</asp:ListItem> 
</asp:RadioButtonList> 


<asp:TextBox ID="txtsearch" runat="server" class="form-control" placeholder="Search"></asp:TextBox> 
+0

將RadioButtonList設置爲「OrderDate」的事件是什麼? –

回答

0

您不需要使用if (!IsPostBack)。只要刪除if (!IsPostBack),然後代碼工作正常。

protected void Page_Load(object sender, EventArgs e) 
{ 

    if (lblfield.SelectedValue == "OrderDate") 
    { 
      txtsearch.Visible = false; 
    } 

    else 
    { 
      txtsearch.Visible = true; 
    } 
} 

或者只是在一條線:

txtsearch.Visible = lblfield.SelectedValue == "OrderDate" ? false : true; 
0

這是更好地爲您RadioButtonSelectedIndexChanged創建活動。然後你可以在頁面加載時調用它,並且你可以對它進行更多的控制。

protected void Page_Load(object sender, EventArgs e) 
{ 
    lblfield_SelectedIndexChanged(sender, e); 
} 

protected void lblfield_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    if (lblfield.SelectedValue == "OrderDate") 
     txtsearch.Visible = false; 
    else 
     txtsearch.Visible = true; 
} 
0

我認爲最好不要使用服務器調用JQuery,因爲這太昂貴了。

相關問題