Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Button Onclick event within a Repeater?

Posted on 2009-07-10
2
Medium Priority
?
527 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 380 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

704 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