根据asp:DropDownList中的选定值设置输入文本

时间:2017-09-08 18:20:44

标签: c# asp.net datatable dropdown

我有一个DropDownList,旁边是输入文本。下拉列表从数据库填充。

Utility.cs

public DataTable Get_Student_Billing_Type(DropDownList lb, int faculty)
        {
            try
            {
                DataTable dt = new DataTable();
                SqlParameter param1 = new SqlParameter("@P_BILLING_FACULTY", SqlDbType.Int);
                param1.Value = faculty;
                dt = DbAccessHelper.ExecuteDataSet("P_GET_BILLING_TYPE", new SqlParameter[] { param1 }, true).Tables[0];
                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        lb.Items.Add(new ListItem(row["BILLING_HEADING"].ToString(), row["BILLING_TYPE_ID"].ToString()));

                    }
                }
                return dt;

            }
            catch (Exception)
            {

                return null;
            }
        }

此DataTable dt有3列BILLING_HEADING, BILLING_TYPE_ID, BILLING_AMOUNT

我的要求是当用户从下拉列表中选择一个值,即BILLING_HEADING时,其对应的BILLING_AMOUNT应显示在旁边的输入文本中。我知道我使用OnSelectedIndexChanged但是如何在所选项目上显示DataTable中的相应字段值。

asp页面代码

<div class="form-group">
    <label for="inputType" class="col-md-2 control-label">Type</label>
    <div class="col-md-6">
        <asp:DropDownList ID="DDL_type" class="form-control input_text" OnSelectedIndexChanged="DDL_type_SelectedIndexChanged" runat="server"></asp:DropDownList>
    </div>
    <div class="col-md-3">
        <input type="text" class="form-control input_text" id="inputTypeAmount" placeholder="AMOUNT" readonly="true" autocomplete="off" />
    </div>
</div>

aspx代码

protected void GetType()
        {
            try
            {
                DataTable dt = new DropDown().Get_Patient_Billing_Type(DDL_type, 1);
            }
            catch (Exception)
            {

                throw;
            }
        }

        protected void DDL_type_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
            // how to write logic so that the corresponding BILLING_AMOUNT show for selected BILLING_HEADING

            }
            catch (Exception)
            {

                throw;
            }
        }   

1 个答案:

答案 0 :(得分:1)

你可以这样做:

var selectedRow = dt.AsEnumerable()
                     .Where(row => row.Field<int>("BILLING_TYPE_ID") == Convert.ToInt32(DDL_type.SelectedValue)).FirstOrDefult();

yourTextBox.Text = Convert.ToString(selectedRow.Field<int>("BILLING_AMOUNT"));