用DropDownList控件试试看,它有一个Text和一个Value的。

解决方案 »

  1.   

    第一种方法就是一楼的那个,初始化数据的时候你就可以绑定成text和value,一个显示,一个作为值。
    如果楼主不像用droplist,就是要用textbox,那楼主可以先找到这个name对应的id在插入好了,呵呵,我想楼主还是会用droplist的对吧
      

  2.   

    SqlConnection Conn = new SqlConnection( "server=localhost;database=bbsno1;uid=sa;pwd=sa" );
    string SQL_Select = "select id, ItemName from TABLE1 order by id desc";
    //构造一个SqlDataAdapter
    SqlDataAdapter myAdapter = new SqlDataAdapter( SQL_Select, Conn);
    //开始读取数据
    Conn.Open();
    DataSet dataSet = new DataSet();
    myAdapter.Fill( dataSet,"Table1" );
    Conn.Close();
    //开始绑定DropDownList
    //指定DropDownList使用的数据源
    ddlArea.DataSource = dataSet.Tables["Table1"].DefaultView;
    //指定DropDownList使用的表里的那些字段
    ddlArea.DataTextField = "area_name"; //dropdownlist的Text的字段
    ddlArea.DataValueField = "area_id";//dropdownlist的Value的字段
    ddlArea.DataBind(); 
    ddlArea.SelectedValue就是外键id,接下去数据库的插入操作你应该会吧?
      

  3.   

    asp.net夜话之八:数据绑定控件
    在asp.net中所有的数据库绑定控件都是从BaseDataBoundControl这个抽象类派生的,这个抽象类定义了几个重要属性和一个重要方法:DataSource属性:指定数据绑定控件的数据来源,显示的时候程序将会从这个数据源中获取数据并显示。DataSourceID属性:指定数据绑定控件的数据源控件的ID, 显示的时候程序将会根据这个ID找到相应的数据源控件,并利用这个数据源控件中指定方法获取数据并显示。DataBind()方法:当指定了数据绑定控件的DataSource属性或者DataSourceID属性之后,再调用DataBind()方法才会显示绑定的数据。并且在使用数据源时,会首先尝试使用DataSourceID属性标识的数据源,如果没有设置DataSourceID时才会用到DataSource属性标识的数据源。也就是说DataSource和DataSourceID两个属性不能同时使用。数据绑定控件的DataSource控件属性必须是一个可以枚举的数据源,如实现了ICollection、IEnumerable或IListSource接口的类的实例。
      

  4.   


    private void BindUserList() 
        { 
            //实例化Connection对象 
            SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=AspNetStudy;Persist Security Info=True;User ID=sa;Password=sa"); 
            //实例化Command对象 
            SqlCommand command = new SqlCommand("select UserID,RealName from UserInfo", connection); 
            SqlDataAdapter adapter = new SqlDataAdapter(command); 
            DataTable data = new DataTable(); 
            adapter.Fill(data);         ddlUserList.DataTextField = "RealName";//指定下拉列表中的文字显示部分 
            ddlUserList.DataValueField = "UserID";//指定下拉列表中的值部分 
            ddlUserList.DataSource = data; 
            ddlUserList.DataBind(); 
        } 
      

  5.   

    Click the link to solve your problem.Good luck!
      

  6.   

    1.先绑定表A,页面上放个dorpdownlist把下面这个方法放到page_load事件里加if(!ispostback){Bind();}private void Bind() 
        { 
           
            SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=你的数据库名;Persist Security Info=True;User ID=sa;Password=sa"); 
            SqlDataAdapter adapter = new SqlDataAdapter(select * from area", connection); 
            DataSet ds = new DataSet();
            adapter.Fill(ds,"area"); 
            DataTable dt = ds.Table[0].DefultView; 
           ddl.DataTextField = "rea_name";//指定下拉列表中的文字显示部分 
            ddl.DataValueField = "area_id";//指定下拉列表中的值部分 
            ddl.DataSource = data; 
           ddl.DataBind(); 
        } 
    2 取公司信息和ddl的值SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=你的数据库名;Persist Security Info=True;User ID=sa;Password=sa"); 
    connection.Open();
    string strCommpanyInfo = txtcor_name.Text.Trim();
    string strCommpanyareid ddl.SelectedValue;
    string strsql = "insert into company(com_name,area_id)values('" + strCommpanyInfo + "','" +strCommpanyareid +"')");
    SqlCommand cmd = new SqlCommand(strsql,connection);
    cmd.ExecuteNonQuery();
    connection.Close();好了给你写完了直接就能用了