Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 22nd, 2004, 01:12 PM
Authorized User
 
Join Date: Jan 2004
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to rathbird
Default Button onclick function in Repeater

This may sound like a very basic question, but I can't find the answer anywhere!

I have a repeater:

<asp:Repeater id="PendPmtRepeater" runat="server">
 <ItemTemplate>
   <tr>
    <td class="small">
      <%# DataBinder.Eval(Container.DataItem, "ApptDate")%></td>
    <td class="small" align="right">
      <%# DataBinder.Eval(Container.DataItem, "Timeslot")%></td>
    <td class="small" align="center">
       <%# DataBinder.Eval(Container.DataItem, "shorthand")%></td>
    <td class="small" align="center">$
       <%# DataBinder.Eval(Container.DataItem, "amount")%></td>
       <td class="small" align="center">
         <asp:Button id="Button1" runat="server"
         OnClick="MyRepeaterButton_Click" Text="Pay" /></td>
  </tr>
 </ItemTemplate>
</asp:Repeater>

This works fine, I've got a datasource and databinding. My problem is the button. When I click the button, it fires the correct event:

Public Sub MyRepeaterButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

End Sub

But I don't know how to access the information from this row in the repeater! I need the AppointmentID to call the correct procedure. How do I know which button was pressed when there can be more than 1?

Help greatly appreciated :)

thanks

 
Old March 22nd, 2004, 03:54 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You can access the buttons parent control, which should be the RepeaterItem. From there you can get at whatever you need within that repeater item.

CType(button.Parent, RepeaterItem)
 
Old March 22nd, 2004, 04:56 PM
Authorized User
 
Join Date: Jan 2004
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to rathbird
Default

I don't mean to sound obtuse, but I have no idea how to implement what you're saying. The expression you gave me returns a type. How do I "get at whatever you need within that repeater item"? Do you have any sample code for me?

I tried many things, and the following line of code worked:
Dim apptid As Int16 = CType(sender, Button).ToolTip

Now i tried this:
Dim apptid As Int16 = CType(Button1.Parent, Button).ToolTip

but it kept giving me a Null Object reference expception, so I gave up.

thanks for your help
 
Old March 23rd, 2004, 01:24 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You might want to consider using a DataList or DataGrid for this task. (Particularly because a DataGrid will do all the table html for you.) Both the DataList and DataGrid have a DataKeys collection in which you can store the key field for the row of data that is bound to the list control's item template. So when you have a button click, you can ask for the button's parent which will be the DataListItem or DataGridItem. From that can get the ItemIndex, and then use that index in the DataKeys collection to get the row key that is associated with the list item.

I haven't used a repeater for what you are trying to do, but I think you might be able to accomplish the same thing using the repeater. Similar to what I explain above when working with a DataList or DataGrid, you can ask for the button's parent which should give you the RepeaterItem. Then you can look for the necessary control in the RepeaterItem using FindControl.

I think the problem you might have been having with this:
Dim apptid As Int16 = CType(Button1.Parent, Button).ToolTip
is that Button1 doesn't exist. Button1 exists in the repeater template but is not instantiated within the page itself. It lives in the repeater's collection of items and occurs more than once. Plus, Button1.Parent will never be a button, it will be the repeater item.

The main problem I see with what you have in your first post is that you are writing out literals to the repeater template. I think you'll have a hard time on the code side finding the values because they are all literals. If you bound the data elements to labels, an actual Literal control, or any other web control, then you should be able to find the controls in the button handler and get the information out of them.

I hope this helps.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old December 9th, 2004, 02:05 PM
Registered User
 
Join Date: Dec 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I don't know, did you solve your problem or not? But i found solution for this problem, and i wanted to share with you.

Repeater control has an ItemCommand event. We need to use this event now. When you create this event;
you will need to declare e as RepeaterCommandEventArgs; and after you can access the buttons CommandArgument value with this code
string fID = e.CommandArgument.ToString();

So, firstly you need to bind ID value to your buttons CommandArgument property.
       <td class="small" align="center">
         <asp:Button id="Button1" runat="server"
          CommandArgument="<%# DataBinder.Eval(Container.DataItem, "id")%>" Text="Pay" /></td>
After, you should create onItemCommand event for the repeater
<asp:Repeater id="PendPmtRepeater" onItemCommand="ButtonFire" runat="server">
At the end,
you can access the id value by this code.
string fID = e.CommandArgument.ToString();
So if you want to redirect to another page; you can do.
Response.Redirect["another_page.aspx?id="+ fID];

There is another way to access this id value.
But it is harder than this. you can use this easily.


 
Old December 15th, 2004, 07:20 PM
Authorized User
 
Join Date: Jan 2004
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to rathbird
Default

Wow! That's a great solution. Believe it or not, to make my code work I just created a dynamic table. That's alot of overhead to get the functionality I wanted, especially since I have to recreate it every time there's a postback event. I can't wait to try your solution. Thanks.

 
Old December 24th, 2004, 01:25 PM
Registered User
 
Join Date: Dec 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

here i am binding data to the repeater but i want to
bind one to this to the label in the reapeater

vijaya kumar
 
Old December 24th, 2004, 01:28 PM
Registered User
 
Join Date: Dec 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

please any one help me

vijaya kumar
 
Old December 29th, 2004, 01:57 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

First of all, I don't think expecting an answer within 3 minutes is very fair. Please allow for some time for others to read, get interested in, and answer your question. Posting a follow up just 3 minutes after your initial post isn't a good thing to do.

That said, it might also help if you provide some background about the problem. I read your question, but I have absolutely no idea what it's about. Is it related to the initial post or is it a new subject?

You'll need to describe the problem in more detail, provide some background, and if possible, post the code you're having problems with.

Cheers,

Imar
 
Old December 31st, 2004, 01:44 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Not only that Imar, but i wonder if this belongs in a different thread?

Hal Levy
I am here to help you, not do it for you.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Button OnClick problem john6630 ASP.NET 2.0 Basics 2 September 21st, 2007 05:44 PM
Button onClick Apocolypse2005 Javascript 1 November 25th, 2005 09:37 AM
Adding an OnClick procedure to repeater johno Classic ASP Databases 1 September 15th, 2005 03:36 AM
How to use Onclick function for this jaya2109 BOOK: Beginning ASP 3.0 5 June 22nd, 2004 07:28 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.