我在detailsview中的TemplateField中放入了DropDownList组件,我数据库中有一名为state的字段,
我想DropDownList中的选定项与State字段进行绑定,state为1时,DropDownlist的第一项为选定,state=2时,第二项选定。但我采用下面两种做法都出错。方法一、使用selectedValue进行绑定,从源代码视图切换到设计视图即报错
  <asp:TemplateField HeaderText="审批结果">
      <EditItemTemplate>
          <asp:DropDownList ID="DropDownList1" SelectedValue="<%#Eval("State") %>" runat="server" >
                <asp:ListItem Value="2">未处理</asp:ListItem>
                <asp:ListItem Value="3">同意</asp:ListItem>
                <asp:ListItem Value="4">不同意</asp:ListItem>
           </asp:DropDownList>
       </EditItemTemplate>
   </asp:TemplateField>
方法二、使用selected="<%#(Eval("state")=2)%>",也报错。
  <asp:TemplateField HeaderText="审批结果">
      <EditItemTemplate>
          <asp:DropDownList ID="DropDownList1" runat="server" >
               <asp:ListItem Value="2" selected="<%#(Eval("state")=2)%>">未处理</asp:ListItem>
               <asp:ListItem Value="3" selected="<%#(Eval("state")=3)%>">同意</asp:ListItem>
                <asp:ListItem Value="4" selected="<%#(Eval("state")=4)%>">不同意</asp:ListItem>
           </asp:DropDownList>
       </EditItemTemplate>
   </asp:TemplateField>我该如何做??

解决方案 »

  1.   

    你可以在.CS文件里实现啊
    DropDownList1.SelectedValue= NO.(你读出来的值)
    即可实现呀
      

  2.   

    把代码加在事件ItemDataBound中。
    switch( e.Item.Cells[i].Text)
    {
    case "1":
    DropDownList mydrop= (DropDownList).Item.Cells[0].Controls[0].FindControl("dropdownlistID")
    mydrop.SelectedIndex=1;
    break;
             case "2":
     ..................
    }
      

  3.   

    但这样写都不太方便啊,难道就不能通过绑定eval来实现吗?
      

  4.   

    championchen79的正解.你的方法没有用过.
    <asp:ListItem Value="2" selected="<%#(Eval("state")==2)%>">未处理</asp:ListItem>试下.可以的话告诉我:)
      

  5.   

    我用的vb的语言。== 是c#的
    我在detailsview中是没有显示state列的,所以使用 e.Item.Cells[i].Text不行。
    在vb文件中写代码是可以的。
      

  6.   

    <EditItemTemplate>的话,应该要用Bind吧,因为编辑状态要求双向绑定。
      

  7.   

    用bind也不可以阿.而且我是自己写itemupdating事件处理程序来更新的.
      

  8.   

    好像不能直接在页面中进行绑定,你必须在后台对它赋值让它选中,并且要注意,设定当前选择项时一定要先判断,如果没有,当下拉框中的选择项没有相对应的值时,将出错.if(this.DropDownList.Items.FindByValue(值)!=null)
    {
    this.DropDownList.SelectedValue= 值;
    }
      

  9.   

    “用bind也不可以阿.而且我是自己写itemupdating事件处理程序来更新的.”——如果你自己写ItemUpdating,那用Eval就可以了。
      

  10.   

    if(this.DropDownList.Items.FindByValue(值)!=null)
    {
    this.DropDownList.SelectedValue= 值;
    }