0

My master page contains a menu which is dynamically created in <ul><li><li><ul/> format.

When I click on it, I want to pass its ID to codebehind for passing it into base class for its permission. But am getting value from jQuery always null in codebehind - but am getting value in jQuery function.

<script type="text/javascript">

        $(document).ready(function () {
            $('#nav li').click(function () {      
               debugger;
                var vals = $(this).text();  
                document.getElementById('hdnForLabel').value = vals ;               
            });
        }); 
    </script>

This page load am using in Master Page.

protected void Page_Load(object sender, EventArgs e)
    {       

            GetMenus();
            string pageids = hdnForLabel.Value;
            BasePage BasePage = new BasePage();
            BasePage.LoadSettings(pageids );            
    }
4

1 回答 1

0

您是否检查过您呈现的隐藏控件的 ID 是否真的是“hdnForLabel”?它可能类似于“ctl00_hdnForLabel”。您可以使用 jQuery 来检查该值是否已在客户端设置:

<script type="text/javascript">
    $(document).ready(function () {
        $('#nav li').click(function () {      
           debugger;
            var vals = $(this).text();  
            document.getElementById('hdnForLabel').value = vals ;               
            alert( "Value: " + $("#hdnForLabel").val());
        });
    }); 
</script>

请注意,为 hdnForLabel.Value 设置的值仅在页面通过回发发送回服务器时可用

protected void Page_Load(object sender, EventArgs e) {

        GetMenus();
        if (IsPostBack) {
          string pageids = hdnForLabel.Value;
          BasePage BasePage = new BasePage();
          BasePage.LoadSettings(pageids );            
        }
}
于 2013-10-28T19:33:49.077 回答