【问题标题】:Loading Drop-DownList from session and save it to DB从会话加载下拉列表并将其保存到数据库
【发布时间】:2014-05-02 05:49:34
【问题描述】:


我想知道如何从网格加载会话数据并将其传递到
下拉列表的下一页并将其保存在数据库中。

例如:我在第 1 页名称、地址、国家/地区的网格数据

protected void PassData(object sender, EventArgs e)
    {
        GridViewRow gr = ((sender as LinkButton).NamingContainer as GridViewRow);

        Session["Name"] = gr.Cells[3].Text.Trim();
        Session["Address"] = gr.Cells[4].Text.Trim();
        Session["Country"] = gr.Cells[5].Text.Trim();
   }

我正要在第 2 页的 Country 下拉列表中加载 Country 值。
我的下拉列表具有预加载的国家/地区列表,我必须加载
我在我的 Session[“Country”] 中拥有的国家/地区值。
例如:

CountryID  Country
   1       USA
   2       AUSTRALIA
   3       CHINA

所以我使用了代码..

ddlCountry.SelectedItem.Text = Session["Country"].ToString();

在我的页面加载中加载国家列表

private void BindCountry()
    {
            DataTable dt = new DataTable();
            dt = objUser.SelAllCountry();

            objCommon.BindDropDown(ddlCountry, "CountryID", "CountryName", dt);

            ddlCountry.SelectedValue = "1";        

    }

ddlCountry.SelectedItem.Text=我的会话值存在于其中,但是 无法将其存储在我的数据库中..

int country 
objCustomer.Country = int.Parse(ddlCountry.SelectedValue);//state to pass the country value.. i know its wrong i need help over here to save my data.

我无法保存新值,它始终显示默认值 1 USA
页面加载

protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            BindCountry();
            //BindState(1);
        }
}

谁能帮我解决这个问题.. 谢谢-提前..

【问题讨论】:

  • 向我们展示您的 Page_load() 事件代码

标签: c# asp.net session


【解决方案1】:

我认为问题是您在page_load() 中绑定下拉列表,请尝试将绑定代码放入!IsPostback 条件块中。

编辑

尝试使用以下语法设置选定的索引。

ddlCountry.SelectedIndex = ddlCountry.Items.IndexOf(ddlCountry.Items.FindByText(Session["Country"].ToString()));

上面应该换成下面一行

ddlCountry.SelectedItem.Text = Session["Country"].ToString();

【讨论】:

  • 实际上我在 !IsPostback 中使用它
【解决方案2】:

您正在选择函数中的第一个值 BindCountry()..避免选择那个..

private void BindCountry()
    {
            DataTable dt = new DataTable();
            dt = objUser.SelAllCountry();
            objCommon.BindDropDown(ddlCountry, "CountryID", "CountryName", dt);
    }

避开这条线..ddlCountry.SelectedValue = "1";

【讨论】:

  • 您是否在页面加载中绑定下拉菜单?
  • 请发布您的页面加载事件?
  • 请在session中传入国家的id,​​使用ddlCountry.Selectedvalue=session["id"].tostring();
  • 执行此行后 - ddlCountry.SelectedItem.Text = Session["Country"].ToString(); - 您的下拉菜单是否选择了特定国家/地区?
  • 我的下拉菜单将值设置为 AUSTRALIA 但无法保存。如果我保存,它会保存第一个值。
猜你喜欢
  • 1970-01-01
  • 2014-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多