Solved

Button Onclick event within a Repeater?

Posted on 2009-07-10
2
526 Views
Last Modified: 2012-05-07
I think this is pretty easy, but I just can't seem to get it to work.

What I want to do is have the onclick fire off the function that I created for it.
------------------ASP.Net Page------------------------------
    <asp:Repeater ID="Repeater1" runat="server">
        <ItemTemplate>
            <asp:Button ID="btnProduct" runat="server" Text='<%#  DataBinder.Eval(Container.DataItem, "Products")%>' OnClick='SelectProduct(<%#  DataBinder.Eval(Container.DataItem, "ProductID")%>)' />
        </ItemTemplate>
    </asp:Repeater>
 
 
 
------------------------Code--------------------------------
    protected void SelectProduct(int ProductID)
    {
        AnswerMainDTO AuditAnswer = (AnswerMainDTO)Session["AuditAnswer"]; 
        AuditAnswer.Product = ProductID;
        Session["AuditAnswer"] = AuditAnswer;
        Response.Redirect("~/AuditMain.aspx");
    }

Open in new window

0
Comment
Question by:Meps
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 13

Accepted Solution

by:
crazyman earned 95 total points
ID: 24824393
Onclick must match the correct signature of the delegate, ie (object,EventArgs) and you can concatinate a string to assign to the Onclick.

Try this approach
<asp:Repeater ID="Repeater1" runat="server">
        <ItemTemplate>
            <asp:Button ID="btnProduct" runat="server" Text='<%#  DataBinder.Eval(Container.DataItem, "Products")%>' OnClick="btnProduct_Click" CommandArgument='<%#  DataBinder.Eval(Container.DataItem, "ProductID")%>' />
        </ItemTemplate>
    </asp:Repeater>
 
 
 
 
------------------------Code--------------------------------
protected void btnProduct_Click(object sender, EventArgs e)
    {
        Button btn = sender as Button;
        int id = int.Parse(btn.CommandArgument);
        SelectProduct(id);
    }
 
    protected void SelectProduct(int ProductID)
    {
        AnswerMainDTO AuditAnswer = (AnswerMainDTO)Session["AuditAnswer"]; 
        AuditAnswer.Product = ProductID;
        Session["AuditAnswer"] = AuditAnswer;
        Response.Redirect("~/AuditMain.aspx");
    }
 

Open in new window

0
 
LVL 3

Author Closing Comment

by:Meps
ID: 31602084
Works perfectly, Thank you so much.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question