2013-02-27 25 views
0
<asp:CheckBox ID="cbIsSignificantRegistration" runat="server" 
       AutoPostBack="True" /> 

    <asp:TextBox ID="txtSignificantOtherName" ReadOnly='<%# cbIsSignificantRegistration.Checked %>' runat="server" class="tblTxt" MaxLength="100"></asp:TextBox> 

    <asp:Button ID="btnSignificantOtherSearch" runat="server" Enabled='<%# cbIsSignificantRegistration.Checked %>' OnClick="Button1_Click" Text="Search" /> 

參考上面的代碼我想啓用一個文本框和按鈕,如果複選框被用戶選中,反之亦然。而且我想在不觸發複選框CheckChanged事件的情況下做同樣的事情。我們可以使用「Eval」來動態地啓用或禁用ASP.NET中的控件

是否有可能在ASP.NET中使用Eval,就像我在上面的代碼中使用它。

注意:我已經使用了上面的代碼,它不適用於我的情況。

回答

1

ASP.NET是服務器端,所以這隻會在頁面加載時設置啓用狀態。之後的任何用戶交互都不會被觸發。

最好的解決方案是使用Javascript觸發事件來啓用/禁用文本框。

使用jQuery:

$(function(){ 
    $(".chk").change(function(){ 
     $(".txt").attr("disabled", !$(this).is(":checked")); 
    }); 
}); 

<asp:CheckBox ID="cbIsSignificantRegistration" runat="server" 
       AutoPostBack="True" CssClass="chk" /> 

<asp:TextBox ID="txtSignificantOtherName" runat="server" class="tblTxt txt" MaxLength="100"></asp:TextBox> 

現場演示:http://jsfiddle.net/VnCpP/

+0

明白了..謝謝 – 2013-02-27 10:29:59

相關問題