我在一个可编辑的DataGrid里放了一下编辑模板DropDownList,
但在我点击DataGrid的编辑按纽时,DropDownList有时显示,有时就不显示,然后报错,请问这是怎么回事?请高手帮我看看代码,是哪里出了问题,谢谢!
//---------------定义DataGrid1--------------------
<asp:DataGrid id="DataGrid1" runat="server" Width="850px" BorderStyle="None" BackColor="White"
BorderColor="#CCCCCC" BorderWidth="1px" CellPadding="3" AutoGenerateColumns="False" PageSize="100"
DataKeyField="id" AllowPaging="True" OnEditCommand="DataGrid1_Edit" OnCancelCommand="DataGrid1_Cancel"
OnUpdateCommand="DataGrid1_Update" AllowSorting="True" OnItemDataBound="DataGrid1_ItemDataBound"
OnDeleteCommand="DataGrid1_Delete">
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#669999"></SelectedItemStyle>
<ItemStyle ForeColor="#000066"></ItemStyle>
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" ForeColor="White" BackColor="#CCCCCC"></HeaderStyle>
<FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="修改">
<ItemStyle Wrap="False"></ItemStyle>
</asp:EditCommandColumn>
<asp:TemplateColumn SortExpression="designer" HeaderText="设计师">
<HeaderStyle Width="120px"></HeaderStyle>
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "designer") %>' ID="Label2" />
</ItemTemplate>
<EditItemTemplate> <asp:DropDownList id="edit_designer" runat="server" SelectedIndex='<%# getIndex(DataBinder.Eval(Container.DataItem, "designer").ToString()) %>' 
         DataSource="<%# TempDataView%>" DataTextField="designer" DataValueField="designer" >
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Left" ForeColor="#000066" BackColor="White" Mode="NumericPages"></PagerStyle>
</asp:DataGrid>//---------------------后台绑定--------------------------
protected DataView TempDataView = new DataView();protected void DataGrid1_Edit(Object sender, DataGridCommandEventArgs E) 
{
Label_alert.Text="";
ds=de.designer_GetAllDesigner();
TempDataView = ds.Tables["tDesigner"].DefaultView; DataGrid1.EditItemIndex = (int)E.Item.ItemIndex;
init(Label_sqlStr.Text);//DataGrid1数据绑定
}
//------------------------------------
private void init(string sqlStr)
{
try
{
ds=te.tech_GetInfoByStr(sqlStr);
DataGrid1.DataSource=ds.Tables["tTechnique"].DefaultView;
if(!pageFlag_all)
{
pageFlag_all=true;
DataGrid1.CurrentPageIndex = 0;
}
DataGrid1.DataBind();
}
catch(Exception ee)
{
Label_alert.Text="<script>alert('"+ee.Message+"');</script>";
}
}