正在从会话加载下拉列表并将其保存到数据库

本文关键字:保存 数据库 下拉列表 会话 加载 | 更新日期: 2023-09-27 18:01:21


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

例如:第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页的国家下拉列表中加载国家值。
我的下拉列表有预先加载的国家/地区列表,我必须加载
我在会话["国家/地区"]中的国家值
例如:

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。已选择项目。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美国
页面加载

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindCountry();
            //BindState(1);
        }
}

有人能帮我解决这个问题吗。。提前谢谢。。

正在从会话加载下拉列表并将其保存到数据库

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

编辑

请尝试使用以下语法设置所选索引。

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

以上应替换为线下

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

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

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

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