【问题标题】:Checkbox for images in an ASP.NET repeater controlsASP.NET 中继器控件中的图像复选框
【发布时间】:2012-08-22 07:50:02
【问题描述】:

我正在使用 ASP.NET。我正在使用中继器来显示图像,并且我还为每个图像使用了一个复选框。

如何选择单个图像并根据图像 ID 更新它们的值?

我的代码是:

 protected void Button3_Click(object sender, EventArgs e)
        {
            foreach (RepeaterItem ritem in Repeater1.Items)
            {
                CheckBox btn = ritem.FindControl("CheckBox1") as CheckBox;
                if (btn.Checked == true)
                {
                    string chrck = btn.Text;
                }
            }
        }

这是我使用的控件:

    <asp:Repeater ID="Repeater1" runat="server">
         <ItemTemplate>
               <br />
               <img ID="ImageZoom" runat="server" 
                    src='<%# DataBinder.Eval(Container.DataItem, "ImageUrl") %>'  
                    style="display: inline; height:auto; left: 0pt; top: 0pt; 
                    width:auto;" />
                <asp:CheckBox ID="CheckBox1" runat="server" Enabled="True" 
                      Text='<%# DataBinder.Eval(Container.DataItem, "ImageId") %>' 
                      oncheckedchanged="RepeaterCheckBox_CheckedChanged"/> 
          </ItemTemplate>
    </asp:Repeater>
    <br />
    <br />
    <asp:Button ID="Button1" runat="server" onclick="Button3_Click" Text="Pint" />

但在 c# 代码中,我总是得到 IsChecked value = false 。即使我检查了 Checkbox 并且当我点击 pint 按钮时,我也只会得到 false 。我该如何解决这个问题?如果我选中复选框,它应该返回 true。

【问题讨论】:

    标签: c# asp.net repeater


    【解决方案1】:

    试试ItemDataBound 和 FindControl

    void Repetarer_ItemDataBound(Object Sender, RepeaterItemEventArgs e) 
    {
              if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
              {
                   var control = e.Item.FindControl("CheckBox1") as CheckBox;      
                   string result = control.Text;
                   ..... 
              }
           }    
    

    【讨论】:

    • 我只想要选定图像的文本。我希望通过使用按钮单击事件
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    相关资源
    最近更新 更多