无法从下拉列表的第二个ID获取数据 [英] data is not fetch from 2nd id in dropdownlist

查看:63
本文介绍了无法从下拉列表的第二个ID获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试基于所选ID读取数据,但它仅读取第一个ID数据,当我从下拉列表中选择第二个ID时,它将选择第一个ID数据.从第二个ID读取数据的问题在哪里?

我的代码是

i am trying to read data based on selected id but it reads only 1st id data and when i select 2nd id from dropdownlist it select 1st id data. where is the problem to read data from 2nd id

my code is

protected void Page_Load(object sender, EventArgs e)
{
    //SqlConnection con = new SqlConnection(str);
    SqlConnection con = new SqlConnection(ConfigurationManager .ConnectionStrings["cn"].ConnectionString);  
    
    string com = "select * from abouts";
    SqlDataAdapter adpt = new SqlDataAdapter(com, con);
    DataTable dt = new DataTable();
    adpt.Fill(dt);
    DropDownList1.DataSource = dt;
    DropDownList1.DataBind();
    DropDownList1.DataTextField = "title";
    DropDownList1.DataValueField = "id";
    DropDownList1.DataBind();
}


protected void Button1_Click1(object sender, EventArgs e)
{
   // SqlConnection con = new SqlConnection(str);
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);  
    SqlCommand cmd = new SqlCommand("select * from abouts where id = '" + DropDownList1.SelectedValue + "'", con);
    SqlDataAdapter Adpt = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    Adpt.Fill(dt);
    GridView1.DataSource = dt;
    GridView1.DataBind();
    Label1.Text = "record found";
}


告诉我基于第二个ID读取数据的问题在哪里.


tell me where is the problem to read data based on 2nd id.

推荐答案

单击Button1时,将触发第一页加载事件,因此您的下拉列表将得到通过从数据库获取其值来重置.

尝试进行检查,无论是否回发.


When you click the Button1, at first page load event fires, so your dropdown list will get reset by fetching its values from the database.

Try to put a checking, whether its postback or not.


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
            {
//SqlConnection con = new SqlConnection(str);
SqlConnection con = new SqlConnection(ConfigurationManager .ConnectionStrings["cn"].ConnectionString);
 
string com = "select * from abouts";
SqlDataAdapter adpt = new SqlDataAdapter(com, con);
DataTable dt = new DataTable();
adpt.Fill(dt);
DropDownList1.DataSource = dt;
DropDownList1.DataBind();
DropDownList1.DataTextField = "title";
DropDownList1.DataValueField = "id";
DropDownList1.DataBind();
}
}



请尝试以下操作:
Hi,
Try the following:
protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack){
       fnBindDDL();
    }
}
private void fnBindDDL()
{
    //SqlConnection con = new SqlConnection(str);
    SqlConnection con = new SqlConnection(ConfigurationManager .ConnectionStrings["cn"].ConnectionString);  
    
    string com = "select * from abouts";
    SqlDataAdapter adpt = new SqlDataAdapter(com, con);
    DataTable dt = new DataTable();
    adpt.Fill(dt);
    DropDownList1.DataSource = dt;
    DropDownList1.DataBind();
    DropDownList1.DataTextField = "title";
    DropDownList1.DataValueField = "id";
    DropDownList1.DataBind();
}
protected void Button1_Click1(object sender, EventArgs e)
{
   // SqlConnection con = new SqlConnection(str);
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);  
    SqlCommand cmd = new SqlCommand("select * from abouts where id = '" + DropDownList1.SelectedValue + "'", con);
    SqlDataAdapter Adpt = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    Adpt.Fill(dt);
    GridView1.DataSource = dt;
    GridView1.DataBind();
    Label1.Text = "record found";
}



--- Amit



---Amit


更正的代码

Corrected code

protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostback)//I ADDED THIS LINE
{
//SqlConnection con = new SqlConnection(str);
SqlConnection con = new SqlConnection(ConfigurationManager .ConnectionStrings["cn"].ConnectionString); 
 
string com = "select * from abouts";
SqlDataAdapter adpt = new SqlDataAdapter(com, con);
DataTable dt = new DataTable();
adpt.Fill(dt);
DropDownList1.DataSource = dt;
DropDownList1.DataBind();
DropDownList1.DataTextField = "title";
DropDownList1.DataValueField = "id";
DropDownList1.DataBind();
}
}


这篇关于无法从下拉列表的第二个ID获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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