下拉列表框未从页面加载更新 [英] Drop down list box not updated from page load

查看:85
本文介绍了下拉列表框未从页面加载更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

难以在下拉列表框中加载来自tabl中第一条记录的数据 e。



我做错了什么?



使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Web;

使用System.Web.UI;

使用System.Web.UI.WebControls;

使用System.Configuration;



使用System.Data;

使用System.Data.Sql;

使用System.Data.SqlClient;

使用System.Data.SqlTypes;







命名空间EDAIF

{

公共部门级Loanmaster:System.Web.UI.Page

{

protected void Page_Load(object sender,EventArgs e)

{

// Page Load开始

if(!IsPostBack)

{

试试

{

SqlConnection connect = new SqlConnection();

connect.ConnectionString = ConfigurationManager.ConnectionStrings [ApplicationServices]。ConnectionString;

connect.Open();

SqlDataAdapter da = new SqlDataAdapter(@SELECT [ID_CODE],

[CUSTOMER],

[APPRS_NO],

[REG_CERT ],

[CURRENT_STATUS]

来自LOANMASTER,连接);





DataSet ds = new DataSet();

da.Fill(ds,LOANJOIN);



ddl_Status.SelectedValue = ds.Tables [LOANJOIN]。行[0] [CUR RENT_STATUS] .ToString();



txt_Id_Code.Text = ds.Tables [LOANJOIN]。行[0] [ID_CODE] .ToString();

txt_Mail1.Text = ds.Tables [LOANJOIN]。行[0] [MAIL1]。ToString();



lblstatus.Text =第一次成功记录;

connect.Close();

GridView1.SelectedIndexChanged + = new EventHandler(GridView1_SelectedIndexChanged);

txt_Id_Code.Enabled = false;

txt_Appraisal.Enabled = false;

txt_Customer.Focus();



}

catch(例外情况)

{

lblstatus.Text = ex.Message;

}



}





//加载结束

解决方案

Page_Init event,您必须使用从数据库中读取的数据初始化您的下拉列表,如下一个示例所示:

  protected   void  Page_Init( object  sender,EventArgs e)
{
if (!Page.IsPostBack)
{
try
{
//
// 初始化用作过滤器控件的中心下拉列表
//
列表<组> centerList = DataContext.Groups.ToList< Group>();
SortedDictionary< string,int> sortedData = new SortedDictionary< string,int>();
_groupDropDownList.DataSource = sortedData;
_groupDropDownList.DataValueField = value;
_groupDropDownList.DataTextField = key;
_groupDropDownList.DataBind();
//
_groupDropDownList.Items.Insert( 0 new ListItem( 所有组 0));
//
_groupDropDownList.SelectedIndex = 0 ;
}
catch (Exception ex)
{
RaGridViewEventLog.LogException(ex);
this .ErrorMessage = 加载错误来自数据库的数据!;
}
}
}


您正在设置状态ddl的选定值。但在此之前,您需要将下拉列表绑定一些项目。那么只有你可以在项目值上设置一个项目作为选定的基础。



并且还要注意你在列名的开头和结尾都有空格

更改

 dl_Status.SelectedValue = ds.Tables [  LOANJOIN]。行[ 0 ] [   CURRENT_STATUS ]。ToString(); 



to

 dl_Status.SelectedValue = ds.Tables [  LOANJOIN ] .Rows [ 0 ] [   CURRENT_STATUS 。 ]的ToString(); 


Having difficulty loading the drop down list box with data from the first record in the table.

What am I doing wrong ?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;

using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;



namespace EDAIF
{
public partial class Loanmaster : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Page Load Begins
if (!IsPostBack)
{
try
{
SqlConnection connect = new SqlConnection();
connect.ConnectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
connect.Open();
SqlDataAdapter da = new SqlDataAdapter(@"SELECT [ID_CODE] ,
[CUSTOMER] ,
[APPRS_NO] ,
[REG_CERT] ,
[CURRENT_STATUS]
FROM LOANMASTER ", connect);


DataSet ds = new DataSet();
da.Fill(ds, "LOANJOIN");

ddl_Status.SelectedValue = ds.Tables["LOANJOIN"].Rows[0][" CURRENT_STATUS "].ToString();

txt_Id_Code.Text = ds.Tables["LOANJOIN"].Rows[0]["ID_CODE"].ToString();
txt_Mail1.Text = ds.Tables["LOANJOIN"].Rows[0]["MAIL1"].ToString();

lblstatus.Text = "First record successful";
connect.Close();
GridView1.SelectedIndexChanged += new EventHandler(GridView1_SelectedIndexChanged);
txt_Id_Code.Enabled = false;
txt_Appraisal.Enabled = false;
txt_Customer.Focus();

}
catch (Exception ex)
{
lblstatus.Text = ex.Message;
}

}


//Load End

解决方案

In the Page_Init event, you have to init your drop down list with data read from the database, like in the next example:

protected void Page_Init(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        try
        {
            //
            // Init the center drop down list used as filter control
            //
            List<Group> centerList = DataContext.Groups.ToList<Group>();
            SortedDictionary<string, int> sortedData = new SortedDictionary<string, int>();
            _groupDropDownList.DataSource = sortedData;
            _groupDropDownList.DataValueField = "value";
            _groupDropDownList.DataTextField = "key";
            _groupDropDownList.DataBind();
            //
            _groupDropDownList.Items.Insert(0, new ListItem("All Groups", "0"));
            //
            _groupDropDownList.SelectedIndex = 0;
        }
        catch (Exception ex)
        {
            RaGridViewEventLog.LogException(ex);
            this.ErrorMessage = "Error in loading the data from the database!";
        }
    }
}


You are setting selected value of the status ddl. But before that you need to bind the drop down with some items. then only you can set one of item as selected base on the item value.

and also note that you have spaces at the beginning and at the end of your column name
change

dl_Status.SelectedValue = ds.Tables["LOANJOIN"].Rows[0][" CURRENT_STATUS "].ToString();


to

dl_Status.SelectedValue = ds.Tables["LOANJOIN"].Rows[0]["CURRENT_STATUS"].ToString();


这篇关于下拉列表框未从页面加载更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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