ASP. C # Select dynamically load Data

Source: Internet
Author: User

1, description through the ASP. NET, using jquery, C # language to the select control to dynamically load data. Front-end pages use an. aspx-type HTML page that uses controller controllers on MVC in the background

2. Webconfig Setting the connection string

<configuration>
<connectionStrings>
<add name= "Connectstr" connectionstring= "Data source=192.168.1.105;initial catalog=testdb; Persist Security info=true; User id=sa;pwd=sa; "providername=" System.Data.SqlClient "/>
</connectionStrings>
</configuration>

3. Get the connection string

Public class DBHelper
    {
// <summary>
/// Get database connection string
// </summary>
// <returns></returns>
Public static String Getconnstr ()
        {
return webconfigurationmanager.connectionstrings["ConnStr"]. ToString ();
        }
    }

4. Data sheet used

My data sheet is in Oracle, and you can use SQL database

CREATE TABLE "Dmkj_sysinfo_ts". " T_user "
("ID" number (10,0),
"NAME" VARCHAR2 (255 BYTE),
"DESCRIBE" VARCHAR2 (255 BYTE)
}

5. Front page code

Description: You need to refer to the library file for jquery yourself

1<%@ Page language="C #"autoeventwireup="true"Codebehind="Xx.aspx.cs"inherits="xx. LoginPage"%>2 3<! DOCTYPE HTML Public"-//W3C//DTD XHTML 1.0 transitional//en" "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD">4"http://www.w3.org/1999/xhtml">56<meta http-equiv="Content-type"Content="text/html; Charset=utf-8"/>7<title></title>8   //the library file for jquery is introduced here9 Ten One<body> A<div > -                                  -                                          the<SelectId="tt01"style="width:150px; height:40px;font-size:17px;line-height:40px;padding-left:60px;"> -</Select> -</div> -<script type="Text/javascript"> + $ (function () { - getuserlist (); +  A }); at function Getuserlist () { -$.post ("Login/getuser", function (data) { -                 varTable =data;  -$("#tt01"). empty ();//first clear the contents of select now -$("#tt01"). Append ("<option selected= ' selected ' value=0> Please select User: </option>"); -                  for(vari =0; i < table.length; i++) { in                     varitem =Table[i]; -$("#tt01"). Append ("<option value="+ Item.id +">"+ Item.text +"</option>"); to                 } +                 //data returned in JSON format -},"JSON"); the         } *  $</script>Panax Notoginseng</body> -View Code

6. Backstage Code

1    Publicjsonresult GetUser ()2         {3             stringSqlstr ="SELECT * from Hymgs_sysinfo_fj.t_office";4OleDbDataReader dr =Myexecutereader (SQLSTR);5List<officeentity> myList =NewList<officeentity>();6              while(Dr. Read ())7             {8Officeentity Ob1 =Newofficeentity () {9id = Convert.ToInt32 (dr["PID"]. ToString ()),TenText = dr["NAME"]. ToString () One                 }; A Mylist.add (OB1); -             } - Dr. Close (); the             returnJson (myList, jsonrequestbehavior.denyget); -         } -  -          Public StaticOleDbDataReader Myexecutereader (stringstrSQL) +         {     -      +                         //Get connection string A stringstrconn=dbhelper.getconnstr (); atOleDbConnection oracn=NewOleDbConnection (strconn); -OleDbCommand Oracmd =NewOleDbCommand (STRSQL,ORACN); -             Try -             { - Oracn.open (); -OleDbDataReader oradr=Oracmd.executereader (commandbehavior.closeconnection); in                 returnOradr; -             } to                  +             Catch(OleDbException E) -             {         the                 Throw NewException (e.message); *             } $             finallyPanax Notoginseng             { -                 //oracmd.dispose (); the                 //oracn.close (); +             } A}
View Code

7. Entity class

   Public class userentity    {        publicintgetset;}          Public string Get Set ; }    }

ASP. C # Select dynamically load Data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.