回传后下拉索引变为0 [英] Dropdown index become 0 after postback

查看:79
本文介绍了回传后下拉索引变为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的信用卡类型,expmonth,expyear有三个下拉列表框.来自CCtype的数据来自表CCMast,对于expmonth和expyear,我编写了函数.
但是问题出在页面回发,然后CCtype将变为0或其索引变为0.每当页面回发一次又一次在这些dropdownboxes中添加的项目时,我就在页面加载.SO上声明了expmonth和expyear函数.那么,如何解决这些问题呢?

I have three dropdownlist box for creditcardtype,expmonth,expyear . Data from the CCtype come from table CCMast and for expmonth and expyear I write the function .
But the problem comes the page postback then CCtype will become 0 or its index become 0 . And i declared the expmonth and expyear function on page load .SO whenever page postback the items added again and again in these dropdownboxes. So how remove these problems

推荐答案

首先,您需要将代码放入Page Load中,如果条件中包含这样的内容,还应将其放入其中...

First of All you need to put you code in Page Load and also put inside if condition containing like this...

 protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        //your code....
    }
}



然后再试一次,希望它能解决您的问题..

或者即使继续进行,也请尝试
将所有下拉列表的自动回发属性设置为true而不是false.....



then try this again i hope it will solve your problem..

OR even if it continues then try this
set all drop down''s auto post back property to true instead of false.....


为所有三个dropdownlist启用autopostback true ..
这是带有两个下拉列表的示例代码
受保护的void Page_Load(对象发送者,EventArgs e)
{

如果(!IsPostBack)
{

SqlCommand cmd =新的SqlCommand(从城市中选择城市名称",con);
con.Open();
SqlDataReader dr;
dr = cmd.ExecuteReader();
while(dr.Read())
{
DropDownList1.Items.Add(dr.GetString(0));

}
dr.Close();
con.Close();

}



}
受保护的void DropDownList2_SelectedIndexChanged(对象发送者,EventArgs e)
{
如果(DropDownList2.SelectedItem.Text == DropDownList1.SelectedItem.Text)
{
//DropDownList2.Items.Clear();

Response.Write("plz选择frm并放置差异");

}


}
受保护的void DropDownList1_SelectedIndexChanged(对象发送者,EventArgs e)
{
DropDownList2.Items.Clear();
DropDownList2.Items.Insert(0,"Select");
SqlCommand cmd2 =新的SqlCommand(从城市中选择城市名称",con);
con.Open();
SqlDataReader dr1;
dr1 = cmd2.ExecuteReader();
while(dr1.Read())
{
DropDownList2.Items.Add(dr1.GetString(0));

}
DropDownList2.Items.Remove(DropDownList1.SelectedItem.Text);
dr1.Close();
con.Close();
}
}
enable autopostback true...for all three dropdownlist..
here is the sample code with two dropdownlist
protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{

SqlCommand cmd = new SqlCommand("select cityname from city", con);
con.Open();
SqlDataReader dr;
dr = cmd.ExecuteReader();
while (dr.Read())
{
DropDownList1.Items.Add(dr.GetString(0));

}
dr.Close();
con.Close();

}



}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList2.SelectedItem.Text == DropDownList1.SelectedItem.Text)
{
// DropDownList2.Items.Clear();

Response.Write("plz select frm and to place diffrnt");

}


}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList2.Items.Clear();
DropDownList2.Items.Insert(0, "Select");
SqlCommand cmd2 = new SqlCommand("select cityname from city ", con);
con.Open();
SqlDataReader dr1;
dr1 = cmd2.ExecuteReader();
while (dr1.Read())
{
DropDownList2.Items.Add(dr1.GetString(0));

}
DropDownList2.Items.Remove(DropDownList1.SelectedItem.Text);
dr1.Close();
con.Close();
}
}


这篇关于回传后下拉索引变为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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