【问题标题】:Display dropdownlist data to textbox将下拉列表数据显示到文本框
【发布时间】:2015-12-29 10:39:11
【问题描述】:

嗨,我怎么知道如何将数据从下拉列表显示到文本框?我只能为名称和 ID 这样做,但不能为我的价格做同样的事情。

例如,当我从下拉列表中选择橙色时,id、名称和价格将显示在它们各自的文本框中。谢谢!

protected void Page_Load(object sender, EventArgs e)
{      
    if (Page.IsPostBack == false)
    {
        bindDropDownList();     
    }
}

private SqlDataReader getReader()
{   
    string strConnectionString = ConfigurationManager.ConnectionStrings["Yasha"].ConnectionString;
    SqlConnection myConnect = new SqlConnection(strConnectionString);

    string strCommandText = "SELECT productID, productName, productPrice from Product";

    SqlCommand cmd = new SqlCommand(strCommandText, myConnect);

    myConnect.Open();
    SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
    return reader;
} 

private void bindDropDownList()
{
    ddlProduct.DataSource = getReader();
    ddlProduct.DataTextField = "productName";
    ddlProduct.DataValueField = "productID";      
    ddlProduct.DataBind();
}

protected void ddlProduct_SelectedIndexChanged(object sender, EventArgs e)
{
    tbProductID.Text = ddlProduct.SelectedValue.ToString();
    tbProduct.Text = ddlProduct.SelectedItem.Text;
    tbPrice.Text = ?;
}

【问题讨论】:

    标签: asp.net drop-down-menu webforms textbox


    【解决方案1】:

    您可以在SelectedIndexChanged 事件中调用数据库,或者您可以将从getReader 返回的数据(以另一种形式-ProductId、Price 的字典)保存到ViewState 中以在您的@ 中查询987654324@事件。

    就我个人而言,我会避免使用ViewState,并使用ProductId 轻松调用您的数据库以获取费用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-10
      • 1970-01-01
      • 2015-05-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多