一个自定义控件(ascx),中有2个dropdownlist(DDL1,DDL2),还有一个button(Btn1),还有一个GRIDDDL1,2都有SelectedIndexChanged 事件。
加载完这个页面,我先执行,改变DDL1的选项,就会触发他的事件,然后绑定GRID;
再改变DDL2的选项的时候,为什么DDL1还会执行一次?
还有,如果这个时候再执行Btn1, DDL1,DDL2又会全部执行一次,为什么会出现这种情况,感觉好像是哪里的时间冲突了?怎么解决?求高手指点!!
加载完这个页面,我先执行,改变DDL1的选项,就会触发他的事件,然后绑定GRID;
再改变DDL2的选项的时候,为什么DDL1还会执行一次?
还有,如果这个时候再执行Btn1, DDL1,DDL2又会全部执行一次,为什么会出现这种情况,感觉好像是哪里的时间冲突了?怎么解决?求高手指点!!
还有设置两个DropDownList的AutopostBack为false
不知道楼主是怎么做的,代码贴下看看
DDL2事件是为了,根据其选项,找到GRID中相应的数据库中已有的数据将它打勾(gridview里有个CHCEKBOX),而btn事件是为了保存当前打勾的数据
public partial class DimensionMappingQuestion : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
//bind Census dropdownlist
var cenBasicCollection = Business.CensusBasic.QueryAllCensusBasic(false);
ValueMode.CensusBasic basic = new ValueMode.CensusBasic{Code = "", Name = ""};
cenBasicCollection.Insert(0,basic);
BindDdlist(DdlCensus, cenBasicCollection, "CODE", "NAME"); //bind dimension dropdownlist
var dimensionCollection = Business.Dimension.GetDimensionTree().ToLeafNodeCollecion();
ValueMode.Dimension dim = new ValueMode.Dimension { DimCode = "", DimName = "" };
dimensionCollection.Insert(0,dim);
BindDdlist(DdlDimension, dimensionCollection, "DIMCODE", "DIMNAME");
// }
if (!IsPostBack)
{
hfCensusCode.Value = DdlCensus.SelectedValue;
hfDimCode.Value = DdlDimension.SelectedValue; BindGrid();
}
} protected void gvDimMapping_RowDataBound(object sender, GridViewRowEventArgs e)
{
//如果是绑定数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
//鼠标经过时,行背景色变
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");
//鼠标移出时,行背景色变
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
}
} protected void DdlCensus_SelectedIndexChanged(object sender, EventArgs e)
{
hfCensusCode.Value = DdlCensus.SelectedValue;
BindGrid();
} protected void BtnSave_Click(object sender, EventArgs e)
{
try
{
foreach (GridViewRow row in gvDimMapping.Rows)
{
var cbDimMapping = row.FindControl("cbDimMapping") as CheckBox;
var hfQuesionId = row.FindControl("hfQuestionId") as HiddenField;
var hfQuestionCode=row.FindControl("hfQuestionCode") as HiddenField;
bool isChecked = cbDimMapping.Checked;
string dimCode = DdlDimension.SelectedValue;
string dimName =(DdlDimension.Items[DdlDimension.SelectedIndex].Text).Replace("-","");
string surveyCode = DdlCensus.SelectedValue;
if (isChecked)
{
int i = Business.Dimension.CreateDimensionQuestionMapping(dimCode, dimName, surveyCode, hfQuestionCode.Value);
}
}
}
catch (Exception ex)
{
throw ex;
} BindGrid();
} protected void DdlDimension_SelectedIndexChanged(object sender, EventArgs e)
{
hfDimCode.Value = DdlDimension.SelectedValue;
ValueMode.DimensionQuestionMappingCollection collection = Business.Dimension.QueryDimensionQuestionMappingByDimCodeAndSurveyCode(hfDimCode.Value, hfCensusCode.Value);
foreach (ValueMode.DimensionQuestionMapping dimMap in collection)
{
foreach (GridViewRow row in gvDimMapping.Rows)
{
var cbDimMapping = row.FindControl("cbDimMapping") as CheckBox;
var hfQuestionCode = row.FindControl("hfQuestionCode") as HiddenField;
if (dimMap.QuestionCode == hfQuestionCode.Value)
{
cbDimMapping.Checked =true ;
}
}
}
}
protected void gvDimMapping_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.gvDimMapping.PageIndex = e.NewPageIndex;
BindGrid();
} private void BindDdlist(DropDownList ddl, IList collection, string colValue, string colText)
{
ddl.DataSource = collection;
ddl.DataTextField = colText;
ddl.DataValueField = colValue;
ddl.DataBind();
} private void BindGrid()
{
ValueMode.CensusQuestionCollection collection = Business.CensusQuestion.QueryCensusQuestionsIsConditionByCensusCode(false, DdlCensus.SelectedValue);
gvDimMapping.DataSource = collection;
gvDimMapping.DataKeyNames = new string[] { "CODE" };
gvDimMapping.DataBind();
} }
<tr>
<td><label>选择问卷:</label></td>
<td><asp:DropDownList ID="DdlCensus" runat="server"
onselectedindexchanged="DdlCensus_SelectedIndexChanged" AutoPostBack="true" /></td>
<td>
</td>
</tr>
<tr>
<td><label>选择维度:</label></td>
<td><asp:DropDownList ID="DdlDimension" runat="server"
onselectedindexchanged="DdlDimension_SelectedIndexChanged" AutoPostBack ="true" /></td>
<td><asp:Button ID="BtnSave" runat="server" Text="绑定 " onclick="BtnSave_Click" /></td>
</tr>
<tr>
<td colspan="3">
<asp:GridView ID="gvDimMapping" runat="server" AutoGenerateColumns="False" Width="100%" AllowPaging="True" PageSize="10"
OnPageIndexChanging="gvDimMapping_PageIndexChanging"
OnRowDataBound="gvDimMapping_RowDataBound" >
<Columns>
{
if (!IsPostBack)
{
//bind Census dropdownlist
var cenBasicCollection = Business.CensusBasic.QueryAllCensusBasic(false);
ValueMode.CensusBasic basic = new ValueMode.CensusBasic{Code = "", Name = ""};
cenBasicCollection.Insert(0,basic);
BindDdlist(DdlCensus, cenBasicCollection, "CODE", "NAME"); //bind dimension dropdownlist
var dimensionCollection = Business.Dimension.GetDimensionTree().ToLeafNodeCollecion();
ValueMode.Dimension dim = new ValueMode.Dimension { DimCode = "", DimName = "" };
dimensionCollection.Insert(0,dim);
BindDdlist(DdlDimension, dimensionCollection, "DIMCODE", "DIMNAME");
// }
hfCensusCode.Value = DdlCensus.SelectedValue;
hfDimCode.Value = DdlDimension.SelectedValue; BindGrid();
}
}
{
if (!IsPostBack)
{
//bind Census dropdownlist
var cenBasicCollection = Business.CensusBasic.QueryAllCensusBasic(false);
ValueMode.CensusBasic basic = new ValueMode.CensusBasic { Code = "", Name = "" };
cenBasicCollection.Insert(0, basic);
BindDdlist(DdlCensus, cenBasicCollection, "CODE", "NAME"); //bind dimension dropdownlist
var dimensionCollection = Business.Dimension.GetDimensionTree().ToLeafNodeCollecion();
ValueMode.Dimension dim = new ValueMode.Dimension { DimCode = "", DimName = "" };
dimensionCollection.Insert(0, dim);
BindDdlist(DdlDimension, dimensionCollection, "DIMCODE", "DIMNAME");
// } hfCensusCode.Value = DdlCensus.SelectedValue;
hfDimCode.Value = DdlDimension.SelectedValue; BindGrid();
} }