Solved

Button Onclick event within a Repeater?

Posted on 2009-07-10
2
522 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 AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

773 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