【问题标题】:changing label text on selectedindexchanged of dropdown更改下拉列表的 selectedindexchanged 上的标签文本
【发布时间】:2013-03-29 08:38:09
【问题描述】:

我需要根据下拉列表中选择的名称更改地址(下拉列表下方)...(它们都仅供一个用户使用)...! (我不知道为什么一直说你的帖子似乎只是代码!!!!!!)

     public DataSet BindDropDownListToAUserAddress2()
    {
        UserFunctions objGetSession = new UserFunctions();
        string strSession = objGetSession.GetEmailFromSession();

        DataSet dsUserAddress = new DataSet();
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);
        con.Open();
        string strQuery = "(SELECT FirstName +' '+  LastName as FullName, *  FROM AUserAddress inner join AState on AUserAddress.State_ID = AState.ID inner join ACountry on AUserAddress.Country_ID = ACountry.ID inner join AUser on AUserAddress.AUser_ID=AUser.ID where AUser.Email='" + strSession + "')";
        SqlCommand cmd = new SqlCommand(strQuery, con);
        using (SqlDataAdapter da = new SqlDataAdapter(cmd))
        da.Fill(dsUserAddress, "AUserAddress");
        con.Close();
        return dsUserAddress;
    }



 protected void ddlName_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataSet dsUserAddress = objBindDDL.BindDropDownListToAUserAddress2();
        string strSession = objGetSession.GetEmailFromSession();
        ddlName.SelectedValue = "FullName";
        if (objBindDDL.BindDropDownListToAUserAddress2() != null && objBindDDL.BindDropDownListToAUserAddress2().Tables.Count > 0)
        {
            lblDisplayFirstName.Text = dsUserAddress.Tables["AUserAddress"].Rows[0]["FirstName"].ToString();
            lblDisplayLastName.Text = dsUserAddress.Tables["AUserAddress"].Rows[0]["LastName"].ToString();
            lblDisplayAddressLine1.Text = dsUserAddress.Tables["AUserAddress"].Rows[0]["AddressLine1"].ToString();
            lblDisplayAddressLine2.Text = dsUserAddress.Tables["AUserAddress"].Rows[0]["AddressLine2"].ToString();
            lblDisplayAddressLine3.Text = dsUserAddress.Tables["AUserAddress"].Rows[0]["AddressLine3"].ToString();
            lblDisplayCity.Text = dsUserAddress.Tables["AUserAddress"].Rows[0]["City"].ToString();
            lblDisplayState.Text = dsUserAddress.Tables["AUserAddress"].Rows[0]["StateName"].ToString();
            lblDisplayCountry.Text = dsUserAddress.Tables["AUserAddress"].Rows[0]["CountryName"].ToString();
            lblDisplayPostalCode.Text = dsUserAddress.Tables["AUserAddress"].Rows[0]["PostalCode"].ToString();
        }
    }

【问题讨论】:

  • 如何让地址(下拉菜单下方)根据下拉列表中的选中项改变??
  • 尝试为下拉列表设置 AutoPostBack="true"。而且,我不太确定为什么要在代码中执行此操作 ddlName.SelectedValue = "FullName";
  • 首先,只需考虑您对数据库进行了多少次访问,为什么不能将值存储在Viewstate。其次我希望Autopostback 属性在.aspx 页面中设置为True ,为什么绑定在SelectedIndexChanged Event里面。
  • 我将更改绑定位置。 AutoPostBack 是真的。下一个?
  • 使用你的id填充标签文本,根据SelectedChangedIndex事件中的ID查询viewstate。

标签: asp.net data-binding drop-down-menu


【解决方案1】:

当您将数据绑定到下拉列表时

 ddlStatus.DataTextField = "FullName";
 ddlStatus.DataValueField = "Id";

onselected 索引更改事件

int ID =Convert.Toint32(ddlName.SelectedValue)

通过使用此 ID 从数据表中仅选择此 ID 记录并显示

【讨论】:

    猜你喜欢
    • 2018-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多