在回发中保留动态下拉列表的值 [英] retaining value of dynamic dropdownlist on postback

查看:68
本文介绍了在回发中保留动态下拉列表的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有个问题..
我已经将数据库(sqlserver 2008 express)中的数据动态地绑定到了一个下拉列表.(在VS2010上.每当我将默认值更改为下拉列表中的其他值时……它返回到原始值(而不是选定的值) )....我也尝试在Page Init上编写代码...但是在回发后,它并没有在dropdownbox中显示所有值....请帮助..

这是代码(dropdownlist autopostback为true)


Page_init
{
...连接....
da.fill(dt);
dropdownlist1.datasource = dt;
dropdownlist1.datatextfield ="city"
dropdownlist1..databind();
}


问候.

Hi , i have a issue..
i have dynanically bounded the data from the database(sqlserver 2008 express) to a dropdownlist.(on VS2010. Whenever i change the value from default to something else in dropdownlist...it comes back to the original value(and not the selected one)....i tried to write the code on Page Init as well... but then it does not shows the value at all in dropdownbox after postback....Please help..

here is the code(dropdownlist autopostback is true)


Page_init
{
...connection....
da.fill(dt);
dropdownlist1.datasource=dt;
dropdownlist1.datatextfield="city"
dropdownlist1..databind();
}


Regards.

推荐答案

您必须检查是否触发了postback 事件.我在下面放了一些代码.

You have to check whether postback event is fired or not. I put some code below.

protected override void OnInit(EventArgs e)
    {
            base.OnInit(e);

            if (!IsPostBack)
            {
                // Put your logic here.
              da.fill(dt);
              dropdownlist1.DataSource=dt;
              dropdownlist1.DataTextField="city"
              dropdownlist1.DataValuefield="city" // you may need to add it.
              dropdownlist1.Databind();

            }

    }



希望对您有帮助.



I hope this helps you well.


if(!ispostback)
{
sqldataadapter da =新sqldataadapter(从表中选择*",cn);
datatable dt = new datatable();
da.fill(dt);
dropdownlist1.datasource = dt;
dropdownlist1.datatextfield ="col";
dropdownlist1.datavaluefield ="col";
dropdownlist1.databind();
dropdownlist1.items.insert(0,"Select");
}
if(!ispostback)
{
sqldataadapter da=new sqldataadapter("select * from table",cn);
datatable dt=new datatable();
da.fill(dt);
dropdownlist1.datasource=dt;
dropdownlist1.datatextfield="col";
dropdownlist1.datavaluefield="col";
dropdownlist1.databind();
dropdownlist1.items.insert(0,"Select");
}


您需要在回发后重新创建,希望能解决问题
You Need To Recreate again after postback i hope solves the problem


这篇关于在回发中保留动态下拉列表的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆