2011-11-03 85 views
1

我正在使用一個簡單的Asp.Net按鈕,並試圖隱藏它在頁面加載事件,我想在做一些客戶端腳本後顯示回來。Asp.net按鈕的可見性使用javascript

我試過了這種方式document.getElementById('<%=Button1.ClientID %>').style.visibility = "visible";,它沒有顯示我。

那麼如何啓用它?

的Page_Load:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
Button1.Visible = False 
End Sub 

這是我的腳本:

<script type="text/javascript"> 
    // Convert divs to queue widgets when the DOM is ready 
    $(function() { 
     $("#uploader").plupload({ 
      // General settings 
      runtimes: 'gears,flash,silverlight,browserplus,html5', 
      url: 'Final.aspx', 
      max_file_size: '10mb', 
      max_file_count: 25, 
      chunk_size: '1mb', 
      unique_names: true, 

     // Resize images on clientside if we can 
     //     resize: { width: 320, height: 240, quality: 90 }, 

     // Specify what files to browse for 
     filters: [ 
     { title: "Image files", extensions: "jpg,gif,png" }, 
     { title: "Zip files", extensions: "zip" } 
    ], 

     // Flash settings 
     flash_swf_url: 'js/plupload.flash.swf', 

     // Silverlight settings 
     silverlight_xap_url: 'js/plupload.silverlight.xap' 
    }); 


    // Client side form validation 
    $('form').submit(function (e) { 
     var uploader = $('#uploader').plupload('getUploader'); 

     // Files in queue upload them first 
     if (uploader.files.length > 0) { 
      // When all files are uploaded submit form 
      uploader.bind('StateChanged', function() { 
       if (uploader.files.length === (uploader.total.uploaded + uploader.total.failed)) { 
        $('form')[0].submit(); 
       } 
      }); 

      uploader.start(); 
     } 
     else 
      alert('You must at least upload one file.'); 

     return false; 
    }); 
    var uploader = $('#uploader').plupload('getUploader'); 
    uploader.bind('FileUploaded', function (up, file, res) { 
     $('#showfilelist').append("<div id=" + file.id + " class='thumb'><a href='uploads/" + document.getElementById("currentDirectory").value + "/" + file.name + "' target='_blank' rel='gallery'><img src='uploads/" + document.getElementById("currentDirectory").value + "/" + file.name + "' width='50' height='50'/></a></div>"); 
     $('#Maintabs').tabs('enable', 1); 
     document.getElementById('<%=Button1.ClientID %>').style.visibility = "visible"; 
    }); 


}); 

回答

9

如果您設置控件Visible屬性false(服務器端),控制不會呈現給客戶,所以沒有什麼可以改變風格的。

如果你想隱藏它的服務器上,但它仍然呈現到客戶端,設置visibility CSS屬性(通過Style屬性),或者指定元素的CSS類,將其隱藏(通過CssClass屬性) 。

+0

感謝您指出我出去以正確的方式,你已經解決了我的問題! – coder

+0

+1,打我吧。 – wsanville

1

如果元素總是會在頁面加載時被隱藏,然後我只想設置一個默認的類或樣式,設置display: none;然後切換與JavaScript的

所以在HTML你按鈕

<asp:button runate="server" id="Button1" CssClass="displayNone"></asp:button> 

<script> 
    $("#Button1").removeClass(displayNone"); 
</script> 

<style> 
    .displayNone { display: none; } 
</style> 
+0

感謝您的幫助! – coder

2

爲什麼不能使用display屬性呢?

document.getElementById('<%=Button1.ClientID %>').style.display= ''; 
document.getElementById('<%=Button1.ClientID %>').style.display= 'none'; 

這樣,您不會觸及服務器屬性,而是觸摸客戶端屬性。你可能需要稍微調整一下上面的代碼。

7

感謝您的建議。我將其知名度如下

<script type="text/javascript"> 
$(function() { 
document.getElementById('<%=Button1.ClientID %>').style.visibility = "hidden"; 
}); 
<script> 
+0

不錯。這也會起作用 – Eric

+0

謝謝埃裏克和我忘了這個小點,並努力爲它! – coder