遇到问题: 希望点击页面中下拉列表,触发onChange事件,想在onChange事件中用到服务器端方法(根据选中的值对页面中的控件进行操作,需要调用底层的一些方法)

解决办法:

页面中加入js 脚本:

<script type="text/javascript">
    function fnRole_OnChange(role)
    {
        if(role.length > 0)
        {
            $("#<%= hidRole.ClientID %>").val(role); //把下拉列表选中值存到服务器端组件隐藏字段中,以备服务器端调用
            <%= Page.ClientScript.GetPostBackEventReference(Page,"role") %>;
        }
    }

</script>

 在后台代码中加入:

第一步: 该页需要继承接口IPostBackEventHandler

如: public partial class PopInsertUser : System.Web.UI.Page, IPostBackEventHandler

 

 

protected void Page_Load(object sender, EventArgs e)
    {

        ddlRole.Attributes["OnChange"] = "fnRole_OnChange(this.value);";

     ...........

  }


 第二步: 需要实现接口IPostBackEventHandler中的方法RaisePostBackEvent

 public void RaisePostBackEvent(string eventArgument)
    {
        if ((eventArgument == "role") && (hidRole.Value.Length > 0))
        {
            if (this.isHideGroup(hidRole.Value)) //在isHideGroup方法中调用底层的方法
            {
                ListItem _nullGroup = new ListItem();
                _nullGroup.Text = "未设置";
                _nullGroup.Value = "";
                ddlGroup.Items.Insert(0, _nullGroup);
                this.ddlGroup.SelectedValue = "";
                this.ddlGroup.Enabled = false;
            }
            else
            {
                ddlGroup.Items.RemoveAt(0);
                this.ddlGroup.Enabled = true;                
            }
        }
    }

相关文章:

  • 2021-11-25
  • 2021-06-16
  • 2021-09-24
  • 2021-12-02
  • 2021-09-14
  • 2021-11-23
猜你喜欢
  • 2022-12-23
  • 2022-02-06
  • 2022-02-14
  • 2022-01-07
  • 2022-03-04
  • 2022-12-23
相关资源
相似解决方案