在datalist的EditItemTemplate中设置下拉列表值

时间:2013-06-20 09:07:27

标签: asp.net vb.net datalist

我有一个asp:datalist,允许用户进入编辑模式来编辑数据。我在edititemtemplate中有一个下拉列表,我试图根据数据源中的字段设置所选值,但不知道如何执行此操作。以下是我的asp.net代码示例:

<asp:DataList ID="dlOtherSubjects" runat="server" DataKeyField="ID" EnableViewState="True" 
         OnEditCommand="Edit_Command"  
         OnUpdateCommand="Update_Command"
         OnCancelCommand="Cancel_Command" 
         OnDeleteCommand="Delete_Command" 
         Width="700">
        <ItemTemplate>
            <table width="700"  cellspacing="2" cellpadding="2">
            <tr>
            <td width="350" class="Label">Type</td>
            <td width="350"><%#Eval("Type")%></td>
            </tr>
            </table>
            <table width="700">
            <tr>
            <td align="left"> 
            <asp:ImageButton ImageUrl="images/Editbutton.png"  CommandName="Edit" 
                            Runat="server" ID="lbedit" />
            <asp:ImageButton ImageUrl="images/Deletebutton.png"  CommandName="Delete" 
                            Runat="server" ID="lbdelete" />
            </td>                  
            </tr>                
            </table>                 
        </ItemTemplate>
        <EditItemTemplate>
            <table width="700" bgcolor="#BFD8D9" cellspacing="2" cellpadding="2">
            <tr>
            <td class="Label">Type</td>
                <asp:DropDownList ID="ddlEType" runat="server">
                <asp:ListItem Value="Household/Family Member" Text=" Family/Household Member"/>
                <asp:ListItem Value="Significant Other" Text="Significant Other (Non Household)"/>
            </asp:DropDownList> 
            </td>
            </tr>
            </table>  
            <br />
            <table width="700">
            <tr>
            <td align="left">
            <asp:ImageButton ImageUrl="images/Updatebutton.png" CommandName="Update" 
                          Runat="server" ID="lbupdate" />
            <asp:ImageButton ImageUrl="images/Cancelbutton.png" CommandName="Cancel" 
                          Runat="server" ID="lbcancel" />              
            </td>
            </tr>
            </table>   
        </EditItemTemplate>
        </asp:DataList>

我找到了一个示例,其中使用OnRowDataBound调用sub来在gridview中完成 然后设置下拉列表的值(参见here)但是对于数据列表是否有类似的OnRowDataBound对象?

如何在datalist中实现这一目标?我还需要能够为asp:radiobuttonlist做类似的事情。

任何帮助将不胜感激。感谢

1 个答案:

答案 0 :(得分:1)

您是否尝试过DataList OnItemDataBound?

 <asp:DataList ID="dlOtherSubjects" runat="server" DataKeyField="ID"    EnableViewState="True" 
     OnEditCommand="Edit_Command"  
     OnUpdateCommand="Update_Command"
     OnCancelCommand="Cancel_Command" 
     OnDeleteCommand="Delete_Command" 
     Width="700"
     OnItemDataBound="Item_Bound"
    >


  protected void Item_Bound(Object sender, DataListItemEventArgs e)
  {
     if (e.Item.ItemType == ListItemType.Item || 
         e.Item.ItemType == ListItemType.AlternatingItem)
     {
        var yourLabel = (Label)e.Item.FindControl("Label");
        //add your logic
      }

  }