今天项目中遇到一个关于DataGrid的 ItemDataBound自动判断下拉列表默认值绑定问题
18:32:46
问题出现:如果某栏目在数据库中则选取DropDownList中其原值作为默认被选定值(正常),然后查询一条不在数据库中的记录,则异常发生了,
问题:如果该栏目不在数据库中则报异常
调试发现:是(DataBinder.Eval(e.Item.DataItem,"prgColumn")).ToString()无法被ddl.Items.FindByText()接受造成的异常。
但是我仍要实现:如果某栏目在数据库中则选取默认值
那么:为了实现目的,我想到了引入临时变量,遍历DropDownList的Text项来判断这个栏目是否能被当作某人选定值
protected void objDataGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemIndex!=-1)
{
e.Item.Attributes.Add("onmouseover","this.style.background='#e4e8ef'");
e.Item.Attributes.Add("onmouseout","this.style.background='white'");
LinkButton btnDelete=(LinkButton)e.Item.Cells[5].Controls[0];
if(btnDelete!=null)
btnDelete.Attributes["OnClick"]="javascript:return confirm('您确定要删除该项吗?');";
}
SqlDataAdapter da=new SqlDataAdapter("select ptId ,ptName from ax_ProgColumn order by ptId",ProData.SqlConnection);
DataSet ds=new DataSet();
da.Fill(ds);
if(e.Item.ItemType==ListItemType.EditItem)
{
DropDownList ddl=(DropDownList)e.Item.FindControl("dpColumnE");
ddl.DataTextField ="ptName";
ddl.DataValueField = "ptId";
ddl.DataSource=ds.Tables[0].DefaultView;
ddl.DataBind();
bool d=false;
for(int i=0;i<ddl.Items.Count;i++)
{
if((DataBinder.Eval(e.Item.DataItem,"prgColumn")).ToString()==ddl.Items[i].Text){d=true;}
}
if(d)
//如果该项存在于新绑定的下拉列表则选择数据库内原值的作为默认
ddl.Items.FindByText((DataBinder.Eval(e.Item.DataItem,"prgColumn")).ToString()).Selected=true;
}
}
问题出现:如果某栏目在数据库中则选取DropDownList中其原值作为默认被选定值(正常),然后查询一条不在数据库中的记录,则异常发生了,
问题:如果该栏目不在数据库中则报异常
调试发现:是(DataBinder.Eval(e.Item.DataItem,"prgColumn")).ToString()无法被ddl.Items.FindByText()接受造成的异常。
但是我仍要实现:如果某栏目在数据库中则选取默认值
那么:为了实现目的,我想到了引入临时变量,遍历DropDownList的Text项来判断这个栏目是否能被当作某人选定值
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)