[英]set input text based on selected value from asp:DropDownList
I've a DropDownList and beside it an input text. 我有一个DropDownList,旁边有一个输入文本。 The dropdown is populated from Database.
下拉列表是从数据库填充的。
Utility.cs 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;
}
}
This DataTable dt has 3 columns BILLING_HEADING, BILLING_TYPE_ID, BILLING_AMOUNT
此数据表dt具有3列
BILLING_HEADING, BILLING_TYPE_ID, BILLING_AMOUNT
My requirement is when user selects a value ie BILLING_HEADING from the dropdown list its corresponding BILLING_AMOUNT should be shown in the input text beside it. 我的要求是,当用户从下拉列表中选择一个值(即BILLING_HEADING)时,其对应的BILLING_AMOUNT应该显示在旁边的输入文本中。 I know I've to use
OnSelectedIndexChanged
but how to show the corresponding field value from DataTable on selected item. 我知道我必须使用
OnSelectedIndexChanged
但是如何在所选项目上显示来自DataTable的相应字段值。
asp page code 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 code 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;
}
}
You can do it like this: 您可以这样做:
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"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.