DropdownList用了验证控件(RequiredFieldValidator)后,SelectedIndexChanged事件不再起作用(AutoPostBack="True")(新手上路,多多指教)
请各位大虾帮忙。
我的页面有个发送按钮,如果DropDownList的selectedvalue为空的话,验证控件就出现,但是出现后,再选其他选项后,就不会再触发selectedIndexChanged事件了,再点击,又会触发。请问这是怎么回事?
再验证控件起作用前,会触发selectedIndexChanged事件。
个人分析是因为点击发送时已经触发PostBack,故再选择就不会触发SelectedIndexChanged事件,请教解决方法,小女子不胜感激!
前台代码: <asp:DropDownList ID="ddlModule" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlModule_SelectedIndexChanged" >
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="ddlModule"
ErrorMessage="*请选所属模块"></asp:RequiredFieldValidator></td>
后台: protected void ddlModule_SelectedIndexChanged(object sender, EventArgs e)
{
hiModuleID.Value = ddlModule.SelectedValue;
FilltbBussDept(); //填充部门
}
private void FilltbBussDept()
{
DataSet ds = CommonHelper.ExecuteDataSet(ApplicationInfo.LogicString, CommandType.Text, "SELECT BelongDeptName FROM Module WHERE ModuleID='" + ddlModule.SelectedValue.ToString() + "' ");
if (ds.Tables[0].Rows.Count > 0)
{ tbBussDept.Text = ds.Tables[0].Rows[0]["BelongDeptName"].ToString(); }
else
tbBussDept.Text = "";
ds = null;
}
------解决方案--------------------最好不用验证控件,有的时候有js冲突
protected void ddlModule_SelectedIndexChanged(object sender, EventArgs e)
{
if(ddlModule.selectindex<-1)
{
hiModuleID.Value = ddlModule.SelectedValue;
FilltbBussDept(); //填充部门
}
else
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('请选择一个dropdownlist!')", true);}
}
private void FilltbBussDept()
{
DataSet ds = CommonHelper.ExecuteDataSet(ApplicationInfo.LogicString, CommandType.Text, "SELECT BelongDeptName FROM Module WHERE ModuleID= '" + ddlModule.SelectedValue.ToString() + " ' ");
if (ds.Tables[0].Rows.Count > 0)
{ tbBussDept.Text = ds.Tables[0].Rows[0]["BelongDeptName"].ToString(); }
else
tbBussDept.Text = "";
ds = null;
}