RadioButtonList、DropDownlist 在客端确认更改

来源:互联网 发布:上海sql培训 编辑:程序博客网 时间:2024/04/28 17:41
RadioButtonList:
<asp:RadioButtonList id="rblMatrixType" style="Z-INDEX: 101; LEFT: 88px; POSITION: absolute; TOP: 104px"
 runat="server" Width="200px" AutoPostBack="True">
 <asp:ListItem Value="A" Selected="True">A</asp:ListItem>
 <asp:ListItem Value="B">B</asp:ListItem>
</asp:RadioButtonList>
<script>
 function attachNewEvent(ctlID)
 {
  var ctl = document.all[ctlID];
  ctl.onclick = "";
  ctl.attachEvent("onclick", comfirmRBLChange);
 }
 
 function comfirmRBLChange()
 {
  if (confirm('Change the type will result in the loss of data. Are you sure you want to proceed?'))
  {
   __doPostBack(event.srcElement.id, '');
  }
  else
  {
   document.all[rblSelectedItem].checked = "checked";
   return false;
  }
 }
</script>
protected override void OnPreRender(EventArgs e)
{
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append("<script>" + System.Environment.NewLine);
        sb.Append(string.Format("       var rbl = /"{0}/";{1}", this.rblMatrixType.ClientID,System.Environment.NewLine));
        
        for (int index = 0; index < this.rblMatrixType.Items.Count; index++)
        {
               sb.Append(string.Format("       attachNewEvent(rbl + /"_{0}/");{1}",index.ToString(),System.Environment.NewLine));
               if (this.rblMatrixType.Items[index].Selected)
               {
                       sb.Append(string.Format("       var rblSelectedItem = /"{0}_{1}/";{2}",this.rblMatrixType.ClientID,index.ToString(),System.Environment.NewLine));
               }
        }
 
        sb.Append("</script>" + System.Environment.NewLine);
 
        if (!this.Page.IsStartupScriptRegistered("RadioButtonListChangeConfirm"))
        {
               this.Page.RegisterStartupScript("RadioButtonListChangeConfirm", sb.ToString());
        }
 
        base.OnPreRender (e);
}
 
 
DropDownList
<asp:DropDownList id="DropDownList1" style="Z-INDEX: 102; LEFT: 280px; POSITION: absolute; TOP: 280px"
 runat="server" Width="88px" AutoPostBack="True">
 <asp:ListItem Selected="True"></asp:ListItem>
 <asp:ListItem Value="A">A</asp:ListItem>
 <asp:ListItem Value="B">B</asp:ListItem>
 <asp:ListItem Value="C">C</asp:ListItem>
</asp:DropDownList>
<script>
 var ddlCtlID = "<%=this.DropDownList1.ClientID%>";
 
 function confirmChangeSelectItem(ctl,message)
 {
  if (ctl[ctl.oldIndex].innerText == "")
  {
   return true;
  }
  if (!confirm(message))
  {
   ctl.selectedIndex = ctl.oldIndex;
   return false;
  }
  else
  {
   return true;
  }
 }
</script>
 
protected override void OnPreRender(EventArgs e)
{
        this.DropDownList1.Attributes.Add("oldIndex", this.DropDownList1.SelectedIndex.ToString());
        this.DropDownList1.Attributes.Add("onchange","if (!confirmChangeSelectItem(this,'Change the size category combination will result in the loss of color prepack data. Are you sure you want to proceed?')) return;");
 
        base.OnPreRender (e);
}
 

 

原创粉丝点击