0

我有一个 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 在网格视图中调用一个子程序,然后设置下拉列表的值(请参见此处),但是数据列表是否有类似的 OnRowDataBound 对象?

如何在数据列表中实现这一点?我还需要能够为 asp:radiobuttonlist 做类似的事情。

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

4

1 回答 1

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
      }

  }
于 2013-06-20T10:40:56.947 回答