Solved

Button Onclick event within a Repeater?

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
optimize  c# code 7 49
Editing XSL files, 2 21
ConsoleSql 1 21
SQL best practice question 3 20
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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

11 Experts available now in Live!

Get 1:1 Help Now