更新期间DROPDOWNLIST默认值pre-选择 [英] dropdownList default value pre-selected during update

查看:82
本文介绍了更新期间DROPDOWNLIST默认值pre-选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2表:
artikulli

I have 2 tables : artikulli

tema    varchar(250)    
abstrakti   text
data_publikimit date    
path    varchar(350)    
keywords    varchar(350)    
kategoria_id    int

kategoria

kategoria

id  int
emertimi    varchar(350)    

我有一个DropDownList至极显示所有emertimi,当用户选择一个节省的ID表artikulli的emertimi列kategoria_id。
我的问题是与更新。当我执行更新下拉不显示为缺省$ P $对选择存在于数据库中的值,而是在列表中的第一个值。

I have a dropdownList wich displays all "emertimi" and when the user select one it saves the id for that emertimi in table "artikulli" in column "kategoria_id". My problem is with update. When i perform an update dropdown doesn't displays as default pre-selected the value that exists in database but instead the first value in the list.

DropDownList drpdKategoria = e.Item.FindControl("drpdKategoria") as DropDownList;
            SqlConnection con = new SqlConnection(connection);
            string Qry = "select * from kategoria";
            string id = Request.QueryString["id"];                                 
            SqlDataAdapter da = new SqlDataAdapter(Qry, con);
            SqlDataAdapter did = new SqlDataAdapter(id, con);               
            DataSet ds = new DataSet();
            con.Open();
            da.Fill(ds);
            did.Fill(ds);
            string kategoria_id = "select kategoria_id from artikulli where id='" + id + "'";
            SqlDataAdapter dk = new SqlDataAdapter(kategoria_id, con);
            dk.Fill(ds);
            string emertimi = "select emertimi from kategoria where id='" + kategoria_id + "'";
            SqlDataAdapter de = new SqlDataAdapter(emertimi, con);
            de.Fill(ds);
            drpdKategoria.DataSource = ds;
            drpdKategoria.DataValueField = "id";
            drpdKategoria.DataTextField = "emertimi";              
            drpdKategoria.DataBind();               
            var defaultValue = drpdKategoria.Items.FindByText(emertimi);
            if (defaultValue != null)
                drpdKategoria.SelectedValue = defaultValue.Value;
            con.Close();
            con.Dispose();
            ds.Dispose();
            da.Dispose();
            did.Dispose();
            dk.Dispose();
            de.Dispose();

我究竟做错了?

推荐答案

我不知道如果我得到你的权利,但我还是决定写,因为我得到了很多的烦恼像这样的你描述。如果你设置好的的AutoPostBack =真作为控制的 drpdKategoria 的,每次你选择一个值的页面refreshs。

I don't know if I got you right, but I decided to write because I got many troubles like this one you're describing. If you setted AutoPostBack=true for the control drpdKategoria, everytime you select a value in it the page refreshs.

如果您将数据绑定到 drpdKategoria 在Page_Load,您可以使用此:

If you bind the data to drpdKategoria in the Page_Load, you can use this:

     protected void Page_Load(object sender, EventArgs e)

     {
          if (!IsPostBack)

          {
               //bind data here
          }
     }

这篇关于更新期间DROPDOWNLIST默认值pre-选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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