如何将文本框绑定到下拉列表?

时间:2016-12-29 07:48:07

标签: c# asp.net

我有一个下拉列表,允许用户选择SupplierName。有效。 问题是我需要用相应的SupplierEmail填充文本框:

protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
{
    //  FetchData();
    DataTable dt = new DataTable();
    con.Open();

    SqlCommand cmd1 = new SqlCommand("ePMT_SP_BindSupplierDDLToTextBox", con);
    cmd1.Parameters.Add(new SqlParameter("@SupplierEmail",ddlSupplier.SelectedItem));            
    // txtSupplierEmail.Text = dt.Rows[0]["SupplierEmail"].ToString(); 
    // SqlDataReader datareader = cmd.ExecuteReader();                
    foreach (DataRow row in dt.Rows)
    {
        txtSupplierEmail.Text = row["SupplierEmail"].ToString();
    }
}

ePMT_SP_BindSupplierDDLToTextBox的存储过程:

SELECT SupplierEmail 
FROM dbo.EPMT_MASTER_SUPPLIER 
WHERE SupplierName = @SupplierName

ePMT_SP_BindSupplier的存储过程:

SELECT SupplierName 
FROM SSMC_EPMT.dbo.EPMT_MASTER_SUPPLIER 

private void BindDropdownlist()
{
    DataTable dtSupplier = eBiz.BindSupplier();
    ddlSupplier.DataSource = dtSupplier;
    ddlSupplier.DataTextField = "SupplierName";
    ddlSupplier.DataValueField = "SupplierName";
    ddlSupplier.DataBind();
    ddlSupplier.Items.Insert(0, new ListItem("--Select--", "0"));
}

<label class="col-md-4 control-label" for="inputRefNo">
     Vendor :<span style="color: red">*</span>
</label>
<div class="input-group col-md-8">
    <asp:DropDownList ID="ddlSupplier" OnSelectedIndexChanged="ddl1_SelectedIndexChanged" runat="server" CssClass="form-control" AutoPostBack="true"></asp:DropDownList>
</div>

2 个答案:

答案 0 :(得分:0)

试试这个;

tabIndicator

答案 1 :(得分:0)

我找到了解决方案。

我必须创建另一个存储过程,然后将其链接到下拉列表名称:

 String sql = "SELECT * from dbo.EPMT_MASTER_SUPPLIER where SUPPLIERName = '" + ddlSupplier.SelectedItem.Text + "' ";

在抽出我要弹出数据的所有文本框之前。