Passing value of Label inside repeater to next page on ImageButton Click

Donald Symmons 3,066 Reputation points
2023-12-21T22:25:23.5+00:00

Is it possible pass not only value of an Image, but also value of Label inside repeater control to next page on ImageButton click?

I have a repeater control that displays images in ImageButton and also displays Label values from database. When I click on any ImageButton, I can pass the image that is displayed in that ImageButton to next page. But I also want to pass the value of the Label that is displayed below each image accordingly. For example if there are 3 images displayed Image1, Image 2 and Image3. Image1 has its name displayed under it as first Image

Images Label value

Image1 first Image

Image2 second Image

Image3 third Image

So if I click on the ImageButton of Image1, I should pass Image1 value and it's corresponding Label value to next page. If I click Image2 then it should pass Image2 and the Text "second Image" to next page, and so on...

I don't want to add button to the repeater control or use asp:Button click, I just want to click on an ImageButton, as I explained above.

HTML

<div class="row" style="width: 100%; padding-top: 6px;">
                            <asp:Repeater runat="server" ID="reptrImg">
                                <ItemTemplate>
                                    <div class="col-sm-4 col-md-4">
                                        <div class="thumbnail" style="margin: 0 auto; padding: 10px;">
                                            <asp:ImageButton ID="ImageButton1" CssClass="ImageButton" runat="server" ImageUrl='<%# "data:image/jpeg;base64," + Convert.ToBase64String((byte[])Eval("Data")) %>' OnClick="ImageButton_Click" Style="width: 100%; height: auto; margin: 0 auto; border-radius: 1px; background-size: cover; border: 1px solid #f0f1f2;" />
                                            <asp:Label ID="LblDesc" runat="server" Style="font-weight: 500;" Text='<%# Eval("ImgName") %>'></asp:Label>
                                        </div>
                                    </div>
                                </ItemTemplate>
                            </asp:Repeater>
                        </div>

C#

protected void Page_Load(object sender, EventArgs e)
        {
               string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand("SELECT * FROM  ImageTable"))
                    {
                        using (SqlDataAdapter sda = new SqlDataAdapter())
                        {
                            cmd.Connection = con;
                            sda.SelectCommand = cmd;
                            using (DataTable dt = new DataTable())
                            {
                                sda.Fill(dt);
                                reptrImg.DataSource = dt;
                                reptrImg.DataBind();
                            }
                        }
                    }
                }
}

protected void ImageButton_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton imagebuton = sender as ImageButton;
            string image = imagebuton.ImageUrl;
            using (SqlConnection con = new SqlConnection())
            {
                con.ConnectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
                using (SqlCommand cmd = new SqlCommand("Select * FROM ImageTable WHERE Data = @Data", con))
                {
                    cmd.Parameters.AddWithValue("@Data", image);
                    con.Open();
                    SqlDataReader dr = cmd.ExecuteReader();
                    if (dr.Read())
                    {
                        string value = HttpContext.Current.Request["LblDesc"];
                        Session["Image"] = image;
                        Session["imgname"] = value;
                        Response.Redirect("/infoPage.aspx");
                    }
                    con.Close();
                }
            }
        }

Next Page:

<asp:Image Class="imgfile" ID="imgfile" runat="server" />
<asp:Label ID="LblDept" runat="server" Text=""></asp:Label>
protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Image"] != null)
            {
                imgfile.ImageUrl = Session["Image"].ToString();
            }
            if (Session["imgname"] != null)
            {
                LblDept.Text = Session["imgname"].ToString();
            }
        }
Developer technologies | .NET | Other
Developer technologies | ASP.NET | Other
Developer technologies | C#
{count} votes

Accepted answer
  1. Lan Huang-MSFT 30,191 Reputation points Microsoft External Staff
    2023-12-22T03:13:19.8366667+00:00

    Hi @Donald Symmons,

    When the ImageButton is clicked, you can first use the NamingContainer property to reference the Repeater Item.

    Then use the FindControl method to reference the Label and ImageButton controls from the Repeater Item.

    The modified code is as follows, other codes have not been changed.

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
                    using (SqlConnection con = new SqlConnection(constr))
                    {
                        using (SqlCommand cmd = new SqlCommand("SELECT * FROM  ImageTable"))
                        {
                            using (SqlDataAdapter sda = new SqlDataAdapter())
                            {
                                cmd.Connection = con;
                                sda.SelectCommand = cmd;
                                using (DataTable dt = new DataTable())
                                {
                                    sda.Fill(dt);
                                    reptrImg.DataSource = dt;
                                    reptrImg.DataBind();
                                }
                            }
                        }
                    }
                }
            }
    
            protected void ImageButton_Click(object sender, ImageClickEventArgs e)
            {
                ImageButton btn = (ImageButton)sender;
                RepeaterItem item = (RepeaterItem)btn.NamingContainer;
                ImageButton imagebuton = (ImageButton)item.FindControl("ImageButton1");
                string image = imagebuton.ImageUrl;
                Label lblName = (Label)item.FindControl("LblDesc");
                Session["Image"] = image;
                Session["imgname"] = lblName.Text.ToString();
                Response.Redirect("/infoPage.aspx");
    
            }
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.