dropdownlist选择值问题 [英] dropdownlist selected value problem

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

问题描述

 OleDbConnection con; 
StringBuilder StrBul;
DataSet ds;
OleDbDataAdapter da;
OleDbCommand cmd;

protected void Page_Load(object sender,EventArgs e)
{
DataLoad();
}
public void DataLoad()
{
connection();
DataTable dt = new DataTable();
StrBul = new StringBuilder();
StrBul.AppendFormat(SELECT Country_Id,Country_Name FROM Country_Registration);
da = new OleDbDataAdapter(StrBul.ToString(),con);
ds = new DataSet();
da.Fill(dt);
ddlcountry.DataSource = dt;
ddlcountry.DataTextField =Country_Name;
ddlcountry.DataValueField =Country_Id;
ddlcountry.DataBind();
}
public void connection()
{
string StrConnection = ConfigurationManager.ConnectionStrings [EGPApplication]。ConnectionString;
con = new OleDbConnection(StrConnection);
if(con.State == ConnectionState.Open)
con.Close();
else
con.Open();
}
protected void btnSubmit_Click(object sender,EventArgs e)
{

connection();
string a = ddlcountry.SelectedValue;

StrBul = new StringBuilder();
StrBul.AppendFormat(从Servicecharge_Details中选择max(SERVICECHARGE_ID));
da = new OleDbDataAdapter(StrBul.ToString(),con);
ds = new DataSet();
da.Fill(ds);
Int32 int32Id = 0;
if(ds.Tables [0] .Rows [0] .ItemArray [0] .ToString()==)
{
int32Id = 1;
}
else
{
int32Id = Convert.ToInt32(ds.Tables [0] .Rows [0] .ItemArray [0] .ToString())+ 1;
}
StrBul = new StringBuilder();
StrBul.AppendFormat(插入Servicecharge_Details值({0},'{1}',{2},{3},{4}),int32Id,txtbankname .Text,txtservicecharge .Text,ddlcountry。 SelectedValue,1);
cmd = new OleDbCommand(StrBul.ToString(),con);
cmd.ExecuteNonQuery();
ClientScript.RegisterClientScriptBlock(GetType(),aa,alert('Data inserted successfully'),true);
清除();

}









我使用此代码从数据库的dropdownlist中获取country_id。但是当我将该值插入另一个表时,country_id应该在所有insert语句中相同。例如

如果我选择india作为国家,id为1

如果我选择美国一个也得到相同的country_id 1 ...但实际值是2.



我该如何解决这个pblm。



是否有其他任何解决方案?plz帮助我纠正错误





谢谢

解决方案

将您的代码更改为此。





  protected   void  Page_Load( object  sender,EventArgs e)
{
if (!Page.IsPostBack)
{
DataLoad();
}
}


在页面加载中添加IsPostback [ ^ ]



 受保护  void  Page_Load( object  sender,EventArgs e)
{
if (!IsPostback)
DataLoad ();
}


当你点击按钮时页面再次发回,你的下拉列表再次绑定到第一个索引,这就是为什么它变得相同每次都值。



只需添加



是(!ispostback)

{

binddropdown();

}





它应该工作

OleDbConnection con;
StringBuilder StrBul;
DataSet ds;
OleDbDataAdapter da;
OleDbCommand cmd;

protected void Page_Load(object sender, EventArgs e)
{
    DataLoad();
}
public void DataLoad()
{
    connection();
    DataTable dt = new DataTable();
    StrBul = new StringBuilder();
    StrBul.AppendFormat("SELECT Country_Id,Country_Name FROM Country_Registration");
    da = new OleDbDataAdapter(StrBul.ToString(), con);
    ds=new DataSet ();
    da.Fill(dt);
    ddlcountry.DataSource = dt;
    ddlcountry.DataTextField = "Country_Name";
    ddlcountry.DataValueField = "Country_Id";
    ddlcountry.DataBind();
}
public void connection()
{
    string StrConnection = ConfigurationManager.ConnectionStrings["EGPApplication"].ConnectionString;
    con = new OleDbConnection(StrConnection);
    if (con.State == ConnectionState.Open)
        con.Close();
    else
        con.Open();
}
protected void btnSubmit_Click(object sender, EventArgs e)
{

        connection();
        string a = ddlcountry.SelectedValue;

        StrBul = new StringBuilder();
        StrBul.AppendFormat("Select max(SERVICECHARGE_ID) from Servicecharge_Details");
        da = new OleDbDataAdapter(StrBul.ToString(), con);
        ds = new DataSet();
        da.Fill(ds);
        Int32 int32Id = 0;
        if (ds.Tables[0].Rows[0].ItemArray[0].ToString() == "")
        {
            int32Id = 1;
        }
        else
        {
            int32Id = Convert.ToInt32(ds.Tables[0].Rows[0].ItemArray[0].ToString()) + 1;
        }
        StrBul = new StringBuilder();
        StrBul.AppendFormat("Insert into Servicecharge_Details Values({0},'{1}',{2},{3},{4})", int32Id, txtbankname .Text, txtservicecharge .Text ,ddlcountry.SelectedValue   ,1 );
        cmd = new OleDbCommand(StrBul.ToString(), con);
        cmd.ExecuteNonQuery();
        ClientScript.RegisterClientScriptBlock(GetType(), "aa", "alert('Data inserted successfully')", true);
        Clear();

    }





Im using this code for get country_id in dropdownlist from database. but while im inserting that value to another table the country_id should be same in all insert statement.example
if i select india as country the id is 1
if i select USA that one also getting the same country_id 1...but actual value is 2.

how can i solve this pblm.

is any other solution for that?plz help me to correct that error


Thanks

解决方案

Change your code to this.


protected void Page_Load(object sender, EventArgs e)
       {
           if (!Page.IsPostBack)
           {
               DataLoad();
           }
       }


in page load add IsPostback[^]

protected void Page_Load(object sender, EventArgs e)
    {
      if(!IsPostback)
         DataLoad();
    }


when you click button the page post back again and your dropdownlist bind again go to the first index and that is why it is getting same value every time.

just add

is(!ispostback)
{
binddropdown();
}


it should work.


这篇关于dropdownlist选择值问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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