【问题标题】:how to bind a value of datasource to a checkbox in datalist with Eval()如何使用 Eval() 将数据源的值绑定到 datalist 中的复选框
【发布时间】:2013-09-04 12:55:55
【问题描述】:

我的 asp.net 页面中有一个数据列表。我在代码隐藏中将数据源绑定到它 我在这个数据列表中有一个复选框。

 var n = from gi in DataContext.Context.GalleryImages
                join g in DataContext.Context.Galleries
                on gi.GalleryID equals g.GalleryID
                where g.UserID == UserID && gi.GalleryID==GalleryID
                select new
                {
                    GalleryID = g.GalleryID,
                    ImageDescription = gi.ImageDescription,
                    GalleryName = g.GalleryName,
                    ImageFileName = gi.ImageFileName,
                    IsAlbumImage = gi.IsAlbumImage,
                    ImageID=gi.ImageID
                };

        dlGalleryList.DataSource = n;
        dlGalleryList.DataBind();

当“IsAlbumImage”为真时,应选中复选框。 如何将此属性绑定到复选框?

【问题讨论】:

    标签: asp.net checkbox datalist


    【解决方案1】:

    它应该像这样绑定:

    <ItemTemplate>
        <asp:CheckBox id="MyCheckBox" runat="server"  Checked='<%#Eval("IsAlbumImage") %>' />
    </ItemTemplate>
    

    【讨论】:

      【解决方案2】:

      实际上你必须在数据列表中绑定复选框 1-(推荐)使用 Bind 或 Eval 直接从 ASP 代码绑定它

      <ItemTemplate>
          <asp:CheckBox id="MyCheckBox" runat="server"  Checked='<%#Eval("IsAlbumImage") %>' />
      </ItemTemplate>
      

      2- 在 ItemDataBound 事件上绑定它

      首先,您将事件处理程序添加到数据列表控件,并将布尔值添加到要在 itemdatabound 事件中使用的数据键

      <asp:DataList ID = "DataList1"  OnItemDataBound="DataListItemEventHandler"  DataKeys = "IsAlbumImage"/>
      

      然后你添加绑定 this 的 C# 代码

      protected void DataListItemEventHandler(object sender, DataListItemEventArgs e)
      {
      CheckBox checkbx = new CheckBox();
      checkbx = (CheckBox)e.Item.FindControl("MyCheckBox");
      checkbx.Checked = (bool) DataList1.DataKeys(e.Item.ItemIndex)("IsAlbumImage");
      }
      

      【讨论】:

        【解决方案3】:

        像这样:

        <asp:CheckBox
            ID="check"
            runat="server"
            Checked='<%# Eval("column_name").ToString().Equals("1") %>'
            />
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-07-25
          • 1970-01-01
          • 2012-02-12
          • 1970-01-01
          • 1970-01-01
          • 2023-03-14
          相关资源
          最近更新 更多