处理在DataGrid中的DropDownList的事件

DropDownList没有CommandName属性,所以不能用ItemCommand事件,不过你可以在DataGrid的模板列中加入的DropDownList控件(aspx):
< asp:TemplateColumnHeaderText = " 车辆情况 " >
< ItemTemplate >
< asp:DropDownListid = " carinfolist " runat = " server " OnSelectedIndexChanged = " carinfolist_SelectedIndexChanged "
AutoPostBack
= " True " >
< asp:ListItemValue = " 1 " Selected = " True " > 正常 </ asp:ListItem >
< asp:ListItemValue = " 0 " > 停用 </ asp:ListItem >
</ asp:DropDownList >
</ ItemTemplate >
</ asp:TemplateColumn >

注意:OnSelectedIndexChanged事件及AutoPostBack="True"

一、DropDownList的动态绑定,只需在DataGrid1_ItemDataBound的事件中,取出数值进行匹配.代码如下:

private void LiqDatagrid1_ItemDataBound( object sender,System.Web.UI.WebControls.DataGridItemEventArgse)
... {
if((e.Item.ItemType==ListItemType.Item)||(e.Item.ItemType==ListItemType.AlternatingItem))
...{
stringStrPower=e.Item.Cells[6].Text.Trim();//用隐藏列取出数据
DropDownListdrpcarinfo=(DropDownList)e.Item.Cells[5].FindControl("carinfolist");
for(inti=0;i<drpcarinfo.Items.Count;i++)
...{
if(StrPower.Equals(drpcarinfo.Items[i].Value))
...{
drpcarinfo.Items[i].Selected
=true;
}

else
...{
drpcarinfo.Items[i].Selected
=false;
}

}

}

}

二、触发DataGrid中DropDownList的事件

protected void carinfolist_SelectedIndexChanged( object sender,System.EventArgse)
... {
stringdroplist=((DropDownList)sender).SelectedValue;
DropDownListddl
=(DropDownList)sender;
TableCellcell
=(TableCell)ddl.Parent;
DataGridItemitem
=(DataGridItem)cell.Parent;
newcomponents.DBCarInfo().carInfonew_Update(Convert.ToInt32(item.Cells[4].Text),droplist);
Response.Write(
"<script>alert('!');</script>");
}

你可能感兴趣的:(UI,Web,asp)