DataGrid中嵌入DropDownList无法设置SelectIndex
以为是一个Bug。找了半天才发现,将SelectedValue=Category.ID错误写成SelectedValue=ID,巨汗!
<asp:TemplateColumn HeaderText="行业">
<ItemTemplate>
<asp:Label id=Label1 runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"CateGory.Name", "{0}") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id=drpColCategory runat="server" SelectedValue='<%# DataBinder.Eval(Container.DataItem,"CateGory.ID", "{0}") %>' DataSource="<%# TopCategory %>" DataValueField="ID" DataTextField="Name">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
不过在无谓的问题查找当中,发现了一个奇怪的情况。aspx文件中如些定义
<EditItemTemplate>
<asp:DropDownList id="drpColCategory" runat="server"></asp:DropDownList>
</EditItemTemplate>
并意图在ItemCreate事件中为DropDownList绑定数据及指定SelectedValue,SelectedValue似乎总被忽略掉,她始终选择第一项。相关代码如下
private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
Control ctl = e.Item.FindControl("drpColCategory");
if(ctl==null)
return;
if(e.Item.DataItem==null)//提交表格
return;
Entity.Advertisement ad = (Entity.Advertisement)e.Item.DataItem;
this.listCategory((DropDownList)ctl,ad.CateGory.ID);
}
private void listCategory(DropDownList pDropControl,int pCateID)
{
Entity.Locator.CategoryLocator catLoc = new Mag.Entity.Locator.CategoryLocator(this.DbConnect);
IList list = catLoc.SelectTops();
pDropControl.DataSource = list;
pDropControl.DataTextField = "Name";
pDropControl.DataValueField = "ID";
if(pCateID>-1)
pDropControl.SelectedValue = pCateID.ToString();
pDropControl.DataBind();
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
不过在无谓的问题查找当中,发现了一个奇怪的情况。aspx文件中如些定义
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
并意图在ItemCreate事件中为DropDownList绑定数据及指定SelectedValue,SelectedValue似乎总被忽略掉,她始终选择第一项。相关代码如下
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.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/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.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/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)