[Report] issued a revised linkage own drop-down menu (unlimited class, the database, the initial value.)

Because the site needs the Internet to find a lot of needs are not met, so look for a modify their own code, and then packaged into a custom control. I feel relatively satisfied, hoping that someone needs to be available for consultation.

Features controls:
1, theoretically support an unlimited level, but at present I only do four, people need self-expanding, very simple :).
2, is a version of the database, you can easily change the XML version.
3, load faster.
4, set an initial value (many do not have this function oh).
5, you can set whether you must choose.
6. You can specify whether to choose the last one.
7, waiting for you to discover ......

================== control code =====================
a using System;
a using the System.Data;
a using System.Web.UI;
a using System.Web.UI.WebControls;
a using System.ComponentModel;


namespace StsProject.CustomControl
{
 [DefaultProperty("Text"),
 Designer(typeof(CompanyIndustryDesigner)),
 ToolboxData("<{0}:CompanyIndustryer runat=server></{0}:CompanyIndustryer>")]
 public class CompanyIndustryer : System.Web.UI.WebControls.WebControl,INamingContainer
 {
  private bool _ismust = false;
  private bool _musttoend = false;

  [Bindable(true),
  Category("Behavior"),
  DefaultValue(false),
  Description("是否必须填写")]
  public bool IsMust
  {
   get{return _ismust;}
   set{_ismust=value;}
  }

  [The Bindable (to true),
  the Category ( "Behavior"),
  the DefaultValue (to false),
  the Description ( "must select whether the last one")]
  public BOOL MustToEnd
  {
   GET _musttoend {return;}
   SET = {_musttoend value;}
  }


  [Bindable(true),
  Category("Behavior"),
  DefaultValue(""),
  Description("行业编号")]
  public string IndustryValue
  {
   set
   {
    this.EnsureChildControls();
    ((TextBox)Controls[0]).Text = value;
   } 
   get
   {
    this.EnsureChildControls();
    return ((TextBox)Controls[0]).Text;
   }
  }

  protected override void CreateChildControls()
  {
   base.CreateChildControls ();

   TextBox txtIndustryValue = new TextBox();
   txtIndustryValue.ID = "IndustryValue";
   txtIndustryValue.Width = 0;
   this.Controls.Add(txtIndustryValue);

   DropDownList dropIndustry1 = new DropDownList();
   dropIndustry1.ID = "dropIndustry1";
   this.Controls.Add(dropIndustry1);

   DropDownList dropIndustry2 = new DropDownList();
   dropIndustry2.ID = "dropIndustry2";
   this.Controls.Add(dropIndustry2);

   DropDownList dropIndustry3 = new DropDownList();
   dropIndustry3.ID = "dropIndustry3";
   this.Controls.Add(dropIndustry3);

   DropDownList dropIndustry4 = new DropDownList();
   dropIndustry4.ID = "dropIndustry4";
   dropIndustry4.Width = 0;
   this.Controls.Add(dropIndustry4);

   TextBox txtToEnd = new TextBox();
   txtToEnd.ID = "txtIfEnd";
   txtToEnd.Width = 0;
   this.Controls.Add(txtToEnd);
   
   dropIndustry1.Attributes.Add("ChildSelectName", dropIndustry2.ClientID);
   dropIndustry1.Attributes.Add("style","display:none");
   dropIndustry2.Attributes.Add("ChildSelectName", dropIndustry3.ClientID);
   dropIndustry2.Attributes.Add("FatherSelectName", dropIndustry1.ClientID);
   dropIndustry2.Attributes.Add("style","display:none");
   dropIndustry3.Attributes.Add("ChildSelectName", dropIndustry4.ClientID);
   dropIndustry3.Attributes.Add("FatherSelectName", dropIndustry2.ClientID);
   dropIndustry3.Attributes.Add("style","display:none");

   if (_ismust)
   {
    RequiredFieldValidator valrIndustry = new RequiredFieldValidator();
    valrIndustry.ControlToValidate = txtIndustryValue.ID;
    valrIndustry.ErrorMessage = "<<请选择分类";
    valrIndustry.Display = ValidatorDisplay.Dynamic;
    this.Controls.Add(valrIndustry);
   }

   if (_musttoend)
   {
    RegularExpressionValidator valeIndustry = new RegularExpressionValidator();
    valeIndustry.ControlToValidate = txtToEnd.ID;
    valeIndustry.ErrorMessage = "<<必须选择到最后一级";
    valeIndustry.Display = ValidatorDisplay.Dynamic;
    valeIndustry.ValidationExpression = "Yes|DD";
    this.Controls.Add(valeIndustry);
   }

   string strScript = @"<script>
   var m_oXMLDoc = new ActiveXObject(""Microsoft.XMLDOM"");
   var m_sBaseSrc = ""/GetIndustry.aspx?TopID="";

   function BindSelect( strXMLSrc , objSelectName, defaultVal)
   {
    m_oXMLDoc.async = true; 
    m_oXMLDoc.onreadystatechange = Function( ""fnLoadComplete('"" + objSelectName + ""', '"" + defaultVal + ""');"" );   
    m_oXMLDoc.load( strXMLSrc );
   }
    
   function fnLoadComplete(objSelectName, defaultVal)
   {   
    var objSelect = document.all[objSelectName];
    var aryXMLNodes;
    var node;
    if (objSelect == null)
     return;
    try
    {
     var iReadyState = m_oXMLDoc.readyState;
    }
    catch(e)
    {
     return;
    }
      
    if(  iReadyState != 4 ) return;
      
    if( m_oXMLDoc != null && m_oXMLDoc.xml != """" )
    {
     objSelect.length = 0;
     aryXMLNodes = m_oXMLDoc.documentElement.selectNodes(""//IndustryList/Table"");
     objSelect.options[0] = new Option(""==请选择=="","""");
     var m=0;
     for (var i=0; i < aryXMLNodes.length; i++)
     {
      node = aryXMLNodes[i];
      objSelect.options[i+1] = new Option(aryXMLNodes[i].childNodes.item(1).text, aryXMLNodes[i].childNodes.item(0).text);
      m=m+1;
     }
     
     if (m!=0)
     {
      objSelect.style.display='';
     }
     else
     {
      objSelect.style.display='none';
     }

     if (defaultVal != null && defaultVal != """" && objSelect.length > 1)
     {
      SetSelectedValue(objSelect, defaultVal)
     }
     
     if(objSelect.ChildSelectName != null)
     {
      objSelect.onchange = Function( ""BindSelect(m_sBaseSrc+this.options[this.selectedIndex].value, '""+objSelect.ChildSelectName+""', '""+defaultVal+""');SetTheValue(this);"" );  
      objSelect.fireEvent(""onchange"");   
     }
     else
     {
      objSelect.onchange = Function( ""var val = this.options[this.selectedIndex].value;if(val!='')document.all."+ txtIndustryValue.ClientID +@".value=val;"" );   
     }
    }
   }

 

   SetTheValue function (obj)
   {
    // After this a lot of places in the code, mainly fixes a flaw I find code, is to choose the level, and then choose one of the options on the level of the most (- - Please Select) , to take constant values
    var val = obj.value;

    if(val!='')
     document.all."+ txtIndustryValue.ClientID +@".value=val;
    else
    {
     if(obj.FatherSelectName!=null && document.all[obj.FatherSelectName])
     {
      if(document.all[obj.FatherSelectName].value!='')
       document.all."+ txtIndustryValue.ClientID +@".value=document.all[obj.FatherSelectName].value;
      else
      {  
       if(document.all[obj.FatherSelectName].FatherSelectName!=null && document.all[document.all[obj.FatherSelectName].FatherSelectName])
       {
        if(document.all[document.all[obj.FatherSelectName].FatherSelectName].value!='')
         document.all."+ txtIndustryValue.ClientID +@".value=document.all[document.all[obj.FatherSelectName].FatherSelectName].value;
        else
         document.all."+ txtIndustryValue.ClientID +@".value='';
       }
       else
        document.all."+ txtIndustryValue.ClientID +@".value='';
      }
     }
     else
      document.all."+ txtIndustryValue.ClientID +@".value='';
    }


    // here have done quite dead. But I did not find the best way, if there are better ways, do not forget to tell me, oh
    IF (document.all. "+ @ + DropIndustry1.ClientID." Value == '')
     document.all. "+ TxtToEnd.ClientID @ + "value. " DD "=" ";
    . the else IF (the document.all". ". value == '' || (the document.all." dropIndustry2.ClientID + + @ + @ + dropIndustry3.ClientID "value .. == '' && the document.all "+ @ + dropIndustry3.ClientID" length>. 1))
     . the document.all "+ txtToEnd.ClientID . @ +" value = "" No "";
    the else
     the document.all. " txtToEnd.ClientID + + @ "value. " Yes "" = ";
   }


   function InitSelect(defaultVal)
   {
    //document.all."+ txtIndustryValue.ClientID +@".value = """";
    BindSelect( m_sBaseSrc + ""000"", """+ dropIndustry1.ClientID +@""", defaultVal);
   }

   function Equality(val1,val2)
   {
    if (val1.length < val2.length || val2 == """")
     return false;
    return (val1.substr(0,val2.length) == val2)
   }

   function SetSelectedValue(oSel,val)
   {
    if (val == null)
     return;
    for(var i=0; i<oSel.length; i++)
    {
     if (Equality(val, oSel.options[i].value))
     {
      oSel.selectedIndex = i;

      if(oSel.ChildSelectName == null)
       oSel.fireEvent(""onchange"");   
      break;
     }
    }
   }

   function DoValue()
   {
    if(document.all." + txtIndustryValue.ClientID + @")
    {
     InitSelect(document.all."+ txtIndustryValue.ClientID +@".value);
    }
   }

   window.onload = function(){DoValue();}

   </script>";

   Page.RegisterClientScriptBlock("TheScript",strScript);
  }
 }
}

/// <summary>
/// 设计器
/// </summary>
public class CompanyIndustryDesigner : System.Web.UI.Design.ControlDesigner
{
 public override string GetDesignTimeHtml()
 {
  return "<select><option>请选择分类..</option></select>";
 }
}




==================  GetIndustry.aspx 代码 =====================
  private void Page_Load(object sender, System.EventArgs e)
  {
   string TopID;
   if (Request.QueryString["TopID"]==null)
    TopID = "000";
   else
    TopID = Request.QueryString["TopID"];


   CompanyStore store = new CompanyStore();
   DataSet ds = store.GetIndustryByTopID("IndustryList", TopID);

   = New new Writer the XmlTextWriter the XmlTextWriter (the Response.OutputStream, System.Text.Encoding.GetEncoding ( "UTF-. 8"));
   writer.Formatting = Formatting.Indented;
   writer.Indentation =. 4;
   writer.IndentChar = '';
   ds.WriteXml (Writer);
   writer.Flush ();
   writer.Close ();
   ds.Dispose ();
   Response.End ();
  }


================== database structure and stored procedure =======================
indu_id VARCHAR number 0. 9 (non-automatic, comprising the higher the lower number)
indu_name VARCHAR 50 0 Title
indu_topid varchar 9 0 higher ID
indu_order tinyint 1 0 sorting

the SELECT * the FROM Industry the WHERE coin_topid = @ TopID ORDER BY indu_order asc




Reproduced in: https: //www.cnblogs.com/ziyang/archive/2005/07/31/203929.html

Guess you like

Origin blog.csdn.net/weixin_33798152/article/details/93161287