ASP.Net:需要有关绑定到数据库列的下拉列表的帮助

发布于 2024-08-27 02:52:55 字数 893 浏览 2 评论 0原文

我创建了数据库,其中包含

 - MemName
 - monthlyAmt
 - CurrentInstAmt

我已使用 DropDownList 框绑定 Memname 列的列;
在 DropDownList 框中选择 memname 值时,currentInstAmtmonthlyamt 的相应值应显示在 Textbox 中。

我是 ASP.NET

代码的初学者 -

DataSet dsMemname = new DataSet();
        try
        {
            con.ConnectionString = strcon;
            con.Open();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "usp_Memcollection";
            SqlDataAdapter adp = new SqlDataAdapter(cmd.CommandText, con);
            adp.Fill(dsMemname);
            ddlmemname.DataTextField = "MemName";
            ddlmemname.DataSource = dsMemname;
            ddlmemname.DataBind();
        }
        catch (Exception ex)
        {

            throw ex;
        }

I have created database which has columns

 - MemName
 - monthlyAmt
 - CurrentInstAmt

I have bound the Memname column with a DropDownList box;
onselection of memname value in DropDownList box, the corresponding values of currentInstAmt and monthlyamt should be displayed in Textbox.

I am beginer in asp.net

code -

DataSet dsMemname = new DataSet();
        try
        {
            con.ConnectionString = strcon;
            con.Open();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "usp_Memcollection";
            SqlDataAdapter adp = new SqlDataAdapter(cmd.CommandText, con);
            adp.Fill(dsMemname);
            ddlmemname.DataTextField = "MemName";
            ddlmemname.DataSource = dsMemname;
            ddlmemname.DataBind();
        }
        catch (Exception ex)
        {

            throw ex;
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遇见了你 2024-09-03 02:52:56

编辑:
首先,设置下拉列表的 DataValueField,就像 DataTextField 一样:

ddlmemname.DataValueField = "MemName";

END EDIT

设置下拉列表的 autopostback="True" 如下:

<asp:dropdownlist id="DropDownList1" runat="server" autopostback="true">
</asp:dropdownlist>

然后在后面的代码中为您的页面,为下拉列表添加一个事件,如下所示:

Protected Sub DropDownList1_SelectedIndexChanged _
(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles DropDownList1.SelectedIndexChanged
    Dim selval As String = DropDownList1.SelectedValue

    TextBox1.Text = getMonthlyAmt(selval)
    TextBox2.Text = getCurrentAmt(selval)
End Sub

两个 get() 方法是您自己的代码,用于查找相关值。此外,您可能希望以某种方式将它们优化为一次调用,具体取决于您从何处提取它们以及性能考虑因素。

EDIT:
First, set your dropdownlist's DataValueField, just like the DataTextField: as so:

ddlmemname.DataValueField = "MemName";

END EDIT

Set your dropdownlist's autopostback="True" as so:

<asp:dropdownlist id="DropDownList1" runat="server" autopostback="true">
</asp:dropdownlist>

and then in the code behind for your page, add an event for your drop down as so:

Protected Sub DropDownList1_SelectedIndexChanged _
(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles DropDownList1.SelectedIndexChanged
    Dim selval As String = DropDownList1.SelectedValue

    TextBox1.Text = getMonthlyAmt(selval)
    TextBox2.Text = getCurrentAmt(selval)
End Sub

The two get() methods are your own code to look up your relevant values. Also, you may want to optimize them to one call in some manner depending on where you're pulling them from and performance considerations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文