0

我必须从链接到 DB 的下拉列表中选择值,如何编写代码以在选择值时显示表文本框中的数据。

例如,当我选择类型时,它将显示来自 DB 的表产品和模型数据

好心提醒。

<asp:Table ID="QueryTypeTBL" runat="server">

         <asp:TableRow ID="TableRow5" runat="server" ForeColor="White">
        <asp:TableHeaderCell BorderColor="Black" BorderWidth="1" HorizontalAlign="Left" BackColor="Blue">
        Type :
        </asp:TableHeaderCell>
         <asp:TableHeaderCell BorderColor="Black" BorderWidth="1" BackColor="ControlLight" HorizontalAlign="Left" ForeColor="Black">
            <asp:DropDownList ID="test1" runat="server" RepeatDirection="Horizontal" AutoPostBack="True" OnSelectedIndexChanged="DateTypeRD_Select" ></asp:DropDownList>
        </asp:TableHeaderCell>
        </asp:TableRow>
       </asp:Table> 

    <asp:TableRow ID="TableRow2" runat="server" ForeColor="White">
        <asp:TableHeaderCell BorderColor="Black" BorderWidth="1" HorizontalAlign="Left" BackColor="Blue">
        PRoduct
        </asp:TableHeaderCell>
        <asp:TableHeaderCell BorderColor="Black" BorderWidth="1" BackColor="ControlLight" HorizontalAlign="Left">
            <asp:TextBox ID="Prod1" runat="server" ></asp:TextBox>

        </asp:TableHeaderCell> 
        <asp:TableHeaderCell BorderColor="Black" BorderWidth="1"  HorizontalAlign="Left" BackColor="Blue">
        Model :
        </asp:TableHeaderCell>
        <asp:TableHeaderCell BorderColor="Black" BorderWidth="1" BackColor="ControlLight" HorizontalAlign="Left">
            <asp:TextBox ID="Prod2" runat="server" ></asp:TextBox>

        </asp:TableHeaderCell> 
    </asp:TableRow>

页面后面的代码:

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                QueryDetTBL.Visible = false;
                FillWasteCode();
            }
        }

    private void FillWasteCode()
    {
        test1.Items.Clear();

        string selectSQL = "SELECT WASTE_CODE,WASTE_TYPE FROM [CIMProRPT01].[dbo].[WM_TYPE]ORDER BY WASTE_CODE";

        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CIMProRPT01ConnectionString"].ConnectionString);
        SqlCommand cmd = new SqlCommand(selectSQL, con);
        SqlDataReader reader;

        try
        {
            con.Open();
            reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                ListItem newItem = new ListItem();
                newItem.Text = reader["WASTE_CODE"].ToString();
                newItem.Value = reader["WASTE_CODE"].ToString();
                test1.Items.Add(newItem);
            }
            reader.Close();
        }
        catch (Exception err)
        {
            lblResults.Text = "Error reading list of Transaction Type. ";
            lblResults.Text += err.Message;
        }
        finally
        {
            con.Close();
        }

    }

    protected void DateTypeRD_Select(Object sender, EventArgs e)
    {
        QueryDetTBL.Visible = true;
       {   
       }         
    }
4

0 回答 0