Solved

Button Onclick event within a Repeater?

Posted on 2009-07-10
2
521 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now