programatically access button(s) produced dynamically in a Repeater

Last post 04-22-2007 11:28 AM by french_duke. 5 replies.

Sort Posts:

  • programatically access button(s) produced dynamically in a Repeater

    04-21-2007, 11:10 AM
    • Loading...
    • french_duke
    • Joined on 02-06-2007, 2:18 PM
    • Dundee, Scotland
    • Posts 89

    Hi all

    This is a tough one for me. I have a page which uses a repeater to list a set of users who have requested to be friends with the currently logged in user. Beside each request, I have an asp:button with the id set to the unique username of each requestee. The page looks like this:

    These people have requested to be your friend:

    View George W. Bushs profile George W. Bush
    21/04/2007 00:00:00 accept btn
    Hi, will you be my friend decline btn


    View Stu Listons profile Mr T
    04/10/2007 14:13:41 accept btn
    Add me as a friend, crazy foo' decline btn

    What I want to do with this is use the button onlclick event to UPDATE a column in my 'friends' table, like (AcceptedByFriend = 'True')

    The problem that i am having is that, because my buttons (and their IDs) are created dynamically, I dont know how to refer to them using VB, can anyone help?

    Thanks in advance!

    Duke
    Filed under: , , , ,
  • Re: programatically access button(s) produced dynamically in a Repeater

    04-22-2007, 2:30 AM
    Answer
    • Loading...
    • Liming
    • Joined on 01-09-2006, 8:21 PM
    • Mclean, VA
    • Posts 984

    Yep, i know exactly what you are talking about.. the right way is actually to hooke up onItemCommand to your repeater and then add a CommandName and CommandArgument to yoru buttons, for example

    <asp:repeater id="Requested_Friends" onItemCommand="Accept_Decline_Friends" ......>

      <ItemTemplate>

                 <asp:button id="Accept" commandName="Accept" commandArgument='<%#Eval("FriendID)"%>' runat="server" />
                 <asp:button id="Decline" commandName="Decline" commandArgument='<%#Eval("FriendID)"%>' runat="server" />
      </ItemTemplate>

    </asp:repeater>

    and then in your

    protected void Accept_Decline_Friends( blah)
    {

              String friend_id = e.CommandArgument.ToString();

             if (e.CommandName == "Accept")
             {       // do your stuff

              }
              else if e.CommandName == "Decline") {
                      // do your stuff
             }

    }

     

  • Re: programatically access button(s) produced dynamically in a Repeater

    04-22-2007, 7:38 AM
    • Loading...
    • french_duke
    • Joined on 02-06-2007, 2:18 PM
    • Dundee, Scotland
    • Posts 89

    Nice one Liming

     Was a bit worried I hadn't got that across right but you seem to have nailed it. I'm working in VB and this is my first .Net project, so will give the solution a go and post if I have any more problems.

    Thanks buddy

    Duke
  • Re: programatically access button(s) produced dynamically in a Repeater

    04-22-2007, 7:57 AM
    • Loading...
    • french_duke
    • Joined on 02-06-2007, 2:18 PM
    • Dundee, Scotland
    • Posts 89


    Hi

    Back again quicker than I thought... converted to vb alright, but im never sure what is going on when declaring the subroutine. I have...

        Protected Sub Accept_Decline_Friends (???)
            Dim friend_id As String = e.CommandArgument.ToString
            If (e.CommandName = "Accept") Then
                ' do your stuff
            ElseIf (e.CommandName = "Decline") Then
                ' do your stuff
            End If
        End Sub

     Usually I let visual studio create these for me so my understanding is poor, it may not be as comlicated as i think, but can anyone help me out?

     

    Duke
  • Re: programatically access button(s) produced dynamically in a Repeater

    04-22-2007, 10:14 AM
    • Loading...
    • Liming
    • Joined on 01-09-2006, 8:21 PM
    • Mclean, VA
    • Posts 984

    Hey Duke, are you asking about the parameter that goes into the "( )"? It's suppose to be

    Protected Sub Accept_Decline_Friends(object sender, RepeaterCommandEventArgs e)

    Normally, I never can remember what parameter to use either, so I just type in "Repeater" and then visual studio auto-suggest comes up with a list of names, and "RepeaterCommandEventArgs" is the one seems correct for thigns related to "Command".. tha'ts how I normally pick them.. and sure enough, it's correct because otherwise, the page won't compile nor load without telling you an error message

    hope that helps.


     

  • Re: programatically access button(s) produced dynamically in a Repeater

    04-22-2007, 11:28 AM
    • Loading...
    • french_duke
    • Joined on 02-06-2007, 2:18 PM
    • Dundee, Scotland
    • Posts 89

    Hi again

    Think this is very close to working. I am now getting an error on the update and im not sure what the problem is. Here's the message:

    Incorrect syntax near '('.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '('.

    Source Error:

    Line 27:         acceptOrDeclineFriendship.UpdateParameters.Add("Response", answer)
    Line 28:         acceptOrDeclineFriendship.UpdateParameters.Add("FriendID", friend_id)
    Line 29:         acceptOrDeclineFriendship.Update()
    Line 30: 
    Line 31:     End Sub

    The full code section is here (I am working in VB):

           'retrieve id of requestee and the answer accept/decline
            Dim friend_id As String = e.CommandArgument.ToString
            Dim answer As String = e.CommandName.ToString
    
            'add the parameters
            acceptOrDeclineFriendship.UpdateParameters.Add("Response", answer)
            acceptOrDeclineFriendship.UpdateParameters.Add("FriendID", friend_id)
            acceptOrDeclineFriendship.Update()

     By inserting a breakpoint before the update command, i can see that the values of 'answer' and 'friend_id' have been successfully retrieved. Here is the sqldatasource on page if this makes what im going a bit clearer:

        
        <!---- update query when user has accepted or declined the friendship ---->
        <asp:SqlDataSource ID="acceptOrDeclineFriendship" runat="server" ConnectionString="<%$ xxx %>"
            UpdateCommand="UPDATE Friends SET (ApprovedByFriend = @Response) WHERE (FriendID = @UserID) AND (UserID = @FriendID)">
            <UpdateParameters>
                <asp:ControlParameter Name="UserID" ControlID="userIdValue" />     (label control text is set to logged in user's id on page load)
            </UpdateParameters>
        </asp:SqlDataSource>
    One thing that is complicating this is that im interchanging UserID and FriendID a lot, in hindsight i should have used 'Invitee' and 'Invited' or something. 
    Here is how my Friends table works:
    ID		PK integer
    UserID (Invitee) UniqueId
    FriendID (Invited) UniqueId
    ApprovedByUser True/False/declined whatever
    ApprovedByFriend True/False/declined whatever
    Requested Date/Time






    If anyone knows what is going wrong here I am very grateful for any help at all. Thanks again
    Duke
Page 1 of 1 (6 items)
Microsoft Communities
Page view counter