[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" AutoPostBack="True" 
            onselectedindexchanged="DropDownList1_SelectedIndexChanged"
            AppendDataBoundItems="True"> 
            <asp:ListItem>請選擇</asp:ListItem>
        </asp:DropDownList>
    
    </div>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:companyConnectionString %>" 
        SelectCommand="SELECT * FROM [employee]"></asp:SqlDataSource>
    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
    </form>
</body>
</html>

 

[c#]

String connstr = System.Configuration.ConfigurationManager.ConnectionStrings

["companyConnectionString"].ConnectionString;
 /*

String connstr ="Data Source=SHINN-684D56581\\SQLEXPRESS;

Initial Catalog=company;

Persist Security Info=True;User ID=test;Password=test";
*/          
    
    protected void Page_Load(object sender, EventArgs e)
    {
        
        //避免跳回原值
        if (!IsPostBack)
        {
            SqlConnection conn = new SqlConnection(connstr);
            string strsql = "select distinct  LNAME,SSN  from employee inner join works_on

            on SSN=ESSN";


            SqlDataAdapter da = new SqlDataAdapter(strsql, connstr);
            DataSet ds = new DataSet();
            da.Fill(ds, "test");

            DropDownList1.DataTextField = "LNAME";
            DropDownList1.DataValueField = "SSN";

            DropDownList1.DataSource = ds.Tables["test"].DefaultView;
            DropDownList1.DataBind();
        }
        
    }

         

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
       

    try
        {
            string in_put = DropDownList1.SelectedValue;
            string strsql = "select * from employee inner join works_on on SSN=ESSN

            where SSN =" + in_put;

            SqlDataAdapter da = new SqlDataAdapter(strsql, connstr);
            DataSet ds = new DataSet();
            da.Fill(ds, "test1");

            GridView1.DataSource = ds.Tables["test1"].DefaultView;
            GridView1.DataBind();
        }
        catch (Exception ex) {
            GridView1.Columns.Clear();
            GridView1.DataBind();
        }

            
        
    }

arrow
arrow
    文章標籤
    c# DropDownList
    全站熱搜

    米拉尤咕 發表在 痞客邦 留言(0) 人氣()