【问题标题】:How to assign textbox.text to selected dropdownlist value?如何将 textbox.text 分配给选定的下拉列表值?
【发布时间】:2013-02-16 20:07:10
【问题描述】:

我有一个用户选择产品的页面。然后我希望用选定的 ddl 值填充文本框值,以允许他们在必要时更新该值。我的 ddl 被正确填充。

当我调试它时,文本框 uxProductFamilyUpvalue 似乎被分配了 ddlProductFamilies.SelectedValue 但是当我返回页面时,文本框为空。

有什么想法吗? TY

Imports ThomasOE.Globals
Imports ThomasOE.Utilities
Imports ThomasOE.Product
Imports System.Data
Imports System.Web.UI.WebControls

Public Class frmProducts
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load                
        If Not IsPostBack Then
            FillProductTypesDropdown()
            DisplayLoginDetails(Master, Session.Item(SSNLOGINNAME), Session.Item(SSNCURRENTDATETIME))
        End If

    End Sub

    Private Sub FillProductTypesDropdown()
        Dim objProduct As New Product

        Try

            objProduct.FillProductTypeDS()

            ddlProductTypes.Items.Clear()
            ddlProductTypes.Items.Add(String.Empty)

            For i As Integer = 0 To objProduct.ProductTypeDS.Tables(0).Rows.Count - 1
                ddlProductTypes.Items.Add(objProduct.ProductTypeDS.Tables(0).Rows(i).Item("product_type"))
            Next

            objProduct = Nothing

        Catch ex As Exception

        End Try

    End Sub

    Private Sub FillProductFamiliesDropdown()

        Dim objProduct As New Product

        Try

            objProduct.FillProductFamilyDS(ddlProductTypes.Text)

            ddlProductFamilies.Items.Clear()
            ddlProductFamilies.Items.Add(String.Empty)

            For i As Integer = 0 To objProduct.ProductFamilyDS.Tables(0).Rows.Count - 1
                ddlProductFamilies.Items.Add(objProduct.ProductFamilyDS.Tables(0).Rows(i).Item("product_family"))               
            Next

            objProduct = Nothing

        Catch ex As Exception

        End Try


    End Sub

    Protected Sub ddlProductTypes_SelectedIndexChanged1(sender As Object, e As EventArgs) Handles ddlProductTypes.SelectedIndexChanged
        FillProductFamiliesDropdown()

    End Sub

    Private Sub UpdateProductPage()        
        uxProductTypeUp.Text = ddlProductTypes.SelectedValue
        uxProductFamilyUp.Text = ddlProductFamilies.SelectedItem.Text

    End Sub

    Protected Sub ddlProductFamilies_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlProductFamilies.SelectedIndexChanged
        'UpdateProductPage()            
        uxProductFamilyUp.Text = ddlProductFamilies.SelectedItem.Text

    End Sub
End Class

【问题讨论】:

  • 返回页面时下拉列表是否也是空的?
  • 没有。这具有正确的选定项目。我能够为 page_load 上的文本框分配一个静态值...但是

标签: asp.net vb.net


【解决方案1】:

问题是由于您已经加载了该页面。 在 DDL 中更改值的方式可以使用 jQuery 的 AJAX 调用。

在站点主标题中包含 jQuery

<script type="text/javascript" src="js/jquery-1.11.2.min.js"></script>

在你有你的 DDL 的 aspx 页面上添加类似的东西

    //DropDownList1 on change AJAX
    $("#<%=DropdownList1.ClientID %>").change(function () {
        GetData();
    });

   function GetData() {
        $.ajax({
            type: "POST",
            //here you have to call vb.net function
            url: "Default.aspx/GetDataVB",
            data: '{ddl_val: "' + $("#<%=DropDownList1.ClientID%>")[0].value + '" }',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: GetData_OnSuccess,
            failure: function (response) {
                alert(response.d);
            }
    });
    }
    function GetData_OnSuccess(response) {
        $("#<%=TextBox1.ClientID%>").html(response.d);
    }

进入vb代码

 <System.Web.Services.WebMethod()> _
 Public Shared Function GetDataVB(ByVal ddl_val As String) As String

     return ddl_val

 end function

【讨论】:

    【解决方案2】:

    您可以在下拉列表中附加一个OnItemSelected事件,并在后面的代码中的事件定义中设置TextBox的文本

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-20
      相关资源
      最近更新 更多