Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Button Onclick event within a Repeater?

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

782 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