将数据库值检索到ASP.NET没有重复值的数据绑定下拉列表

时间:2015-12-07 08:33:56

标签: c# asp.net

每当用户点击“更新个人资料”按钮时,他将被引导到其中的页面,他在注册期间保存的所有信息将显示在文本框中,并在下载列表中显示在页面加载中。 (下拉列表是数据绑定)

使用下面的代码,我能够做到,但是当我在DDLDepartment(第一个下拉列表)中更改值时,DDLGroup(第二个下拉列表)自动回复并复制值。

DDLDepartment和DDLGroup是相互关联的,因为DDLGroup是根据DDLDepartment中选择的值进行过滤的

<!doctype html>
<html>
    <head>
        <title>Generate xml for Bus Route</title>
    </head>
    <body>
        <form name='usrxml' method='post'>
            <input type='text' name='number' placeholder='Bus number' />
            <input type='text' name='depart' placeholder='Departing from' />
            <input type='text' name='destination' placeholder='Travelling to' />
            <input type='text' name='times' placeholder='Times of travel' />

            <input type='submit' value='Create xml' />
        </form>
        <?php
            if( $_SERVER['REQUEST_METHOD']=='POST' ){

                /* username??? */
                $username='fred.bloggs';
                $filename=__DIR__.DIRECTORY_SEPARATOR.$username.'bus_route.xml';

                $number         =   $_POST['number'];
                $depart         =   $_POST['depart'];
                $destination    =   $_POST['destination'];
                $times          =   $_POST['times'];

                $dom=new DOMDocument('1.0', 'utf-8' );
                $root=$dom->appendChild( $dom->createElement('TT') );
                $bus=$root->appendChild( $dom->createElement('BUS') );


                $bus->appendChild( $dom->createElement('NUMBER', $number ) );
                $bus->appendChild( $dom->createElement('LEAVING', $depart ) );
                $bus->appendChild( $dom->createElement('DESTINATION', $destination ) );
                $bus->appendChild( $dom->createElement('TIME', $times ) );

                $dom->save( $filename );

                echo 'Route added to: '.$filename;
            }
        ?>
    </body>
</html>

html mark-up

  protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack) // important
        {

            DataTable dt = new DataTable();
            con.Open();
            SqlDataReader myReader = null;


            string cmdstr = "SELECT * from USERSONLINE where Username = '" + Session["Username"] + "'";
            SqlCommand userExist = new SqlCommand(cmdstr, con);

            myReader = userExist.ExecuteReader();

            while (myReader.Read())
            {
                txtEmpID.Text = (myReader["EmpId"].ToString());
                txtFirstName.Text = (myReader["FirstName"].ToString());
                txtLastName.Text = (myReader["LastName"].ToString());
                txtUsername.Text = (myReader["Username"].ToString());
                ddlDepartment.SelectedItem.Text = (myReader["Department"].ToString());
                ddlGroup.SelectedItem.Text = (myReader["GroupName"].ToString());

            }
            con.Close();
        }   
    }

1 个答案:

答案 0 :(得分:0)

选择部门和组时,请使用以下

ddlDepartment.Items.FindByValue((myReader["Department"].ToString())).Selected = true;
ddlGroup.Items.FindByValue((myReader["GroupName"].ToString())).Selected = true;