【Asp.Net】Dropdownlist的autoPostBack属性

参考链接:

http://www.cnblogs.com/zfanlong1314/p/3595298.html

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="_131.WebForm1" %>
<!DOCTYPE html>
< html  xmlns = "http://www.w3.org/1999/xhtml" >
< head  runat = "server" >
     < title ></ title >
</ head >
< body >
     < form  id = "form1"  runat = "server" >
     < div >
         < asp:DropDownList  ID = "DropDownList1"  runat = "server"   OnSelectedIndexChanged = "DropDownList1_SelectedIndexChanged"  AutoPostBack = "True" ></ asp:DropDownList >
         < br  />
         < asp:Label  ID = "Label1"  runat = "server"  Text = "Name" ></ asp:Label >
         < asp:TextBox  ID = "TextBox1"  runat = "server" ></ asp:TextBox >
         < br  />
         < asp:Label  ID = "Label2"  runat = "server"  Text = "Age" ></ asp:Label >
         < asp:TextBox  ID = "TextBox2"  runat = "server" ></ asp:TextBox >
         < br  />
         < asp:Label  ID = "Label3"  runat = "server"  Text = "Remark" ></ asp:Label >
         < asp:TextBox  ID = "TextBox3"  runat = "server" ></ asp:TextBox >
     </ div >
     </ form >
</ body >
</ html >


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using  System;
using  System.Collections.Generic;
using  System.Configuration;
using  System.Data;
using  System.Data.SqlClient;
using  System.Linq;
using  System.Web;
using  System.Web.UI;
using  System.Web.UI.WebControls;
namespace  _131
{
     public  partial  class  WebForm1 : System.Web.UI.Page
     {
         protected  void  Page_Load( object  sender, EventArgs e)
         {
             if  (!IsPostBack)
             {
                 string  ConStr = ConfigurationManager.ConnectionStrings[ "strCon" ].ConnectionString;
                 SqlConnection con =  new  SqlConnection(ConStr);
                 SqlCommand cmd =  new  SqlCommand( "select * from tb_sellInfo" , con);
                 if  (con.State.Equals(ConnectionState.Closed))
                 {
                     con.Open();
                 }
                 SqlDataReader sqr = cmd.ExecuteReader();
                 if  (sqr.HasRows)
                 {
                     while  (sqr.Read())
                     {
                         DropDownList1.Items.Add(sqr[ "Name" ].ToString());
                         DropDownList1.DataTextField =  "Name" ;
                         DropDownList1.DataBind();
                     }
                     ListItem li =  new  ListItem( "请选择" "0" );
                     DropDownList1.Items.Insert(0, li);
                 }
                 sqr.Close();
                 con.Close();
             }
         }
         protected  void  DropDownList1_SelectedIndexChanged( object  sender, EventArgs e)
         {
             string  sqlcmd =  "select * from tb_sellInfo where Name='"  + Convert.ToString(DropDownList1.SelectedValue)+ "'" ;
             string  ConStr = ConfigurationManager.ConnectionStrings[ "strCon" ].ConnectionString;
             SqlConnection con =  new  SqlConnection(ConStr);
             con.Open();
             SqlCommand cmd =  new  SqlCommand(sqlcmd, con);
             SqlDataReader sqr = cmd.ExecuteReader();
             sqr.Read();
             TextBox1.Text = sqr[ "Name" ].ToString();
             TextBox2.Text = sqr[ "Age" ].ToString();
             TextBox3.Text = sqr[ "UpdateDate" ].ToString();
             sqr.Close();
             con.Close();
              
         }
     }
}





      本文转自daniel8294 51CTO博客,原文链接:http://blog.51cto.com/acadia627/1899240 ,如需转载请自行联系原作者




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值