这里发生的事情是,每当我从dropdownlist
中选择一个值时,页面都会获得回发,然后它再次来到" ---选择 - - "
我尝试了here链接,但它没有效果
if (DtMainA.Rows.Count > 0)
{
DataTable DtGoodType = new DataTable();
ObjPriDaAdp = new OracleDataAdapter("select mkey, vehicle_no from XXCUS.XXGID_AUDIT_ENTRY where mkey= '" + Hid_MKey.Value + "'", ObjPriCon);
ObjPriDaAdp.Fill(DtGoodType);
if (DtGoodType.Rows.Count > 0)
{
cmdVehicleNo.DataTextField = "vehicle_no";
cmdVehicleNo.DataValueField = "mkey";
cmdVehicleNo.DataSource = DtGoodType;
cmdVehicleNo.DataBind();
cmdVehicleNo.Items.Insert(0, new ListItem("--- Select ---", "0"));
}
}
另见我的aspx: -
<asp:DropDownList ID="cmdVehicleNo" runat="server" Style="width: 50%" AutoPostBack="true"
OnSelectedIndexChanged="cmdVehicleNo_SelectedIndexChanged">
<asp:ListItem Value="0" Selected="True" >--- Select ---</asp:ListItem>
</asp:DropDownList>
更新
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (ObjPriCon.State == System.Data.ConnectionState.Closed)
{
ObjPriCon.Open();
}
if (hidAttachid.Value == "")
{
hidAttachid.Value = "0";
}
ObjPriCmd = new OracleCommand("Select count(MKEY)cntrow from XXCUS.XXGID_AUDIT_ENTRY", ObjPriCon);
FunFillData();
ObjPriCon.Close();
fillProject();
fillGoodType();
}
}
protected void CmbProject_SelectedIndexChanged(object sender, EventArgs e)
{
// ClientScript.RegisterStartupScript(this.GetType(), "SuccessScript", "window.location.href='FrmCentral_Monitoring_Entry_OUT.aspx?userid=" + Hid_userid.Value + "';", true);
if (CmbProject.SelectedIndex > 0)
{
DtCombo = FillComboBox("select mkey, vehicle_no from XXCUS.XXGID_AUDIT_ENTRY where time_out IS NULL and Project_id='" + CmbProject.SelectedValue + "'");
cmdVehicleNo.DataTextField = "vehicle_no";
cmdVehicleNo.DataValueField = "mkey";
cmdVehicleNo.DataSource = DtCombo;
cmdVehicleNo.DataBind();
cmdVehicleNo.Items.Insert(0, new ListItem("--- Select ---", "0"));
if (DtCombo.Rows.Count == 1)
{
cmdVehicleNo.SelectedIndex = 1;
}
DtCombo.Clear();
}
Funclear();
}
答案 0 :(得分:1)
当前代码非常明显的问题是,无论何时从下拉列表中选择任何项目,它都会执行 PostBack 并触发%appdata%\Sublime Text 2\Packages
事件。在这种情况下,我不知道为什么你再次填充下拉列表。理想情况下,您应该在初始页面加载中仅将下拉列表绑定一次,如下所示: -
CmbProject_SelectedIndexChanged
最后,在选定的选项更改事件中,您应该只编写用户更改选择时要执行的操作的逻辑: -
if (!IsPostBack)
{
BindDropDownList(); //Call this method
}
//Write a separate method to bind the dropdown
private void BindDropDownList()
{
DataTable DtGoodType = new DataTable();
ObjPriDaAdp = new OracleDataAdapter("select mkey, vehicle_no from XXCUS.XXGID_AUDIT_ENTRY where mkey= '" + Hid_MKey.Value + "'", ObjPriCon);
ObjPriDaAdp.Fill(DtGoodType);
if (DtGoodType.Rows.Count > 0)
{
cmdVehicleNo.DataTextField = "vehicle_no";
cmdVehicleNo.DataValueField = "mkey";
cmdVehicleNo.DataSource = DtGoodType;
cmdVehicleNo.DataBind();
cmdVehicleNo.Items.Insert(0, new ListItem("--- Select ---", "0"));
}
}