Checkbox for images in an ASP.NET repeater controls

被刻印的时光 ゝ 提交于 2019-12-11 15:23:11

问题


I am working with ASP.NET. I am using a repeater to display images, and I also am using a check box for each image.

How can I select individual images and update their values as per image id?

My code is:

 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;
                }
            }
        }

This my control which i have use :

    <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" />

But in c# code, I am always getting IsChecked value = false . Even if I checked Checkbox and also when I click on pint button, I am getting only false . How can I solve this? If I checked the checkbox , it should return true.


回答1:


try with ItemDataBound and 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;
               ..... 
          }
       }    


来源:https://stackoverflow.com/questions/12068545/checkbox-for-images-in-an-asp-net-repeater-controls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!