Link to home
Start Free TrialLog in
Avatar of rakhy_rakey
rakhy_rakeyFlag for United States of America

asked on

Asp.Net Repeater Control

Hi Guys...

I have  a repeater control in my page to display the summary. The last column in each row in repeater control has 2 links "Update/Delete"

I have a link called Update in my repeater control. I need to trigger the event to make "Update" link to be clicked.

Generally, When user clicks Update or Delete link on my repeater control, the following method will handle the event.

 Public Sub Repeater_ItemCommand(ByVal sender As Object, ByVal ee As RepeaterCommandEventArgs) Handles RepeaterList.ItemCommand

So, I want to call the same above method by sending CommandName as "Update" and a sequence number to select the particular item in the repeater control.

I tried to do it by calling the following way:

 Repeater_ItemCommand(Me, New RepeaterCommandEventArgs(RepeaterList.Items(SeqNum - 1), "Update", Nothing))

When I call the above method, I am getting error like "Object reference is not set to an instance of an object"

I am unable to find the mistake.

Please tell me how to invoke  the method

Thanks in advance.

Rakhy.
       
   
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

Here's a sample repeater control with a LinkButton for "Update" with the important part in bold.

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
            <ItemTemplate>
                        <div style="width:100px;float:left"><%# DataBinder.Eval(Container, "DataItem.eagleid") %></div>
                        <div style="width:100px;float:left"><%# DataBinder.Eval(Container, "DataItem.displayname") %></div>
                        <div style="width:100px;float:left"><asp:LinkButton ID="updateLink" runat="server" Text="Update" OnClick="Repeater1_UpdateClick" CommandArgument='<%# DataBinder.Eval(Container, "DataItem.eagleid") %>'></asp:LinkButton></div>                                        
                <div style="clear:both"></div>
            </ItemTemplate>
        </asp:Repeater>

then in code behind, create the event handler.
Protected Sub Repeater1_UpdateClick(ByVal sender As Object, ByVal e As EventArgs)
        Dim lb As LinkButton = CType(sender, LinkButton)
        Dim idStr As String = lb.CommandArgument 'this identifies the row clicked
        '''Do whatever you need to do here now that you have an identifier for the row clicked
    End Sub

Open in new window

Avatar of rakhy_rakey

ASKER

Hi tommyboy...thank for the reply.

But I think, you did not get exactly my problem.

What ever you have posted, I already have that part working with me.

The thing is (I am referring to your code now);  I need to invoke or call Repeater1_UpdateClick function with parameter as "Update" to make it  "Update" link clicked and one more parameter as "sequence number" which indicates the item number or row number in the repeater control for that particular "Update" link clicked..

I think it explains clearly now.

Thanks again,

Rakhy....
In my code, I only show the "Update" link but I assumed you would add another LinkButton next to it that handles "Delete". Each would go to a different handler so you will know what action was chosen. No need for the parameter "Update" or "Delete". The only parameter you need for both LinkButtons is the "sequence number" as the CommandArgument and that will indicate what row was clicked.
Tommy,

Here my problem is not about action chosen. I have to make the action to be happen with out clicking on any link when I come to this page from some other page depending upon certain criteria.

In this context, I want to have the result displayed should be the same with the result that will come when I click Update link manually. I think you are lacking to understand the problem.

I will explain the some other thing which I have done successfully.

I have a button called "Add".

Generally, When I click the button I will display a Panel. The button click event handler is

Protected Sub buttonAdd_Click(ByVal Sender As Object, ByVal ee As EventArgs) Handles ButtonAdd.Click
   ' Display a Panel1 blah blah
End Sub

So, here my requirement is: when I come from some other page, depending on certain criteria on that particular page, I need to call the buttonAdd_Click method this page on the page load itself, so that postback will happen and it will display panel1 for me.

To full fill this requirement, What I did is; I just called buttonAdd_Click(Me, New EventArgs()) method in the page load it self, which in turn triggered the event and displayed the panel for me.

Now, The same kind of scenario is the Repeater control problem.

With out clicking the Update link, I need to display the panel just by calling the method Repeater_ItemCommand. So like I explained in my earlier posts, I have 2 links Update and Delete. I have to get the result which will come when I click "Update" link. I am unable to pass the CommandName as"Update" and corresponding sequence  number to select a particular row or Item

Thanks,
Rakhy.


Ok, I think I understand now.

Trying to go through the ItemCommand event seems like a convoluted way to do an update of the repeater. If you are coming from another page and want to trigger an update, why not set a flag for use in the Page_Load event of the page that holds the repeater. You could use session state or query strings. For example, have the sending page contain a query string in the url that holds the sequence number of the row being modified as one parameter and another parameter that sets whether it's an Update or a Delete. Then in the Page_Load of the repeater page, look for the parameters in the query string and act on it by modifying the repeater's data source and re-binding before rendering to the page.
Tommy,

I am able to get the sequence number from other page and able display the other panel in which I need to show the data which actually should happen when user clicks Update on Repeater Control.

When I display like this, with out invoking the Update link on Repeater contro, my validation controls are not working. Generally validation controls work when post back happens right...

So, this is the reason for me to try to trigger Update link on repeater control which in turn display the panel with bounded data.

To make this happen, I tried like the following way to invoke Repeater_ItemCommand method in the page load.

Repeater_ItemCommand(Me, New RepeaterCommandEventArgs(RepeaterList.Items(SeqNum - 1), "Update", Nothing))

I think there is some syntax problem in the above invocation. Can you please advice ?

The Actual method is:

 Public Sub Repeater_ItemCommand(ByVal sender As Object, ByVal ee As RepeaterCommandEventArgs) Handles RepeaterList.ItemCommand

 If ee.CommandName = "Update" Then
    'Do some thing
  Else
   'Do Some thing
  End If
End Sub


I hope you get the clear picture now and my problem also very clearly....

Thanks,
Rakhy
   
ASKER CERTIFIED SOLUTION
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi...

Tommyboy...Thank you very much for the reply.

Sorry for the delay in reply. As I was busy with some other work. but this is still pending issue.

I applied your solution. It is going to the required method Repeater1_ItemCommand.

But some how, it is still unable to resolve the problem.

The difference I found between the successful case and this case is;

In my successful case, I used checkboxes and set the autopost back to true.

in this case, I am using repeater control which is being loaded from user control and repeater control does not have autopostback property.

I even put the repeater control in Update panel and set the asynchronous post back trigger to true for the event name ItemCommand. But still because of some problem, my page is unable to perform post back internally

Can you please help me some more about this problem, so that I can come out of this.

Already your solution helped me to call Repeater1_ItemCommand method by giving the proper command event args.

Thanks a lot tommy.

thanks,
Rakhy.
Instead of trying to invoke the ItemCommand event directly, use javascript to force an "Update" postback of the page as soon as it is rendered.

Method #1:

http://stackoverflow.com/questions/5009508/trigger-automatic-postback-in-javascript

Method #2:

http://forums.asp.net/t/1127869.aspx/1

Method #1: You could set a conditional in Page_Load (a SeqNum is present boolean for example) that if true, would register a client script block to the page that would force an immediate postback. The condition would then need to be false to prevent an endless loop.

Method #2: Similar to above except register a client script block that would trigger the actual Update button on the proper row of the repeater when the page is rendered.
Hi...Tommyboy...

I will try it in couple of days. Sorry for the late reply. I was busy in doing some other work, as manager did not force about this.

I will get back to you about this.

Thanks,
Rakhy.
Hi Tommy boy,

I tried your suggestions in the last post. but it still it did not post back.

But I finally got output with my idea and one of the coding you gave in one of the posts in this thread.

Any thanks a alot for helping me.

I just want to explain about this to you:

What I did is: Earlier, I was able to check the Checkbox by invoking the checkbox event args. It worked properly. The auto post back event for this is set to true.

My repeater control will be displayed depending on the check box checked. So as I have 2 sequence numbers, one for check box value and one for item in repeater control. so with the help of checkbox sequence number, I am selecting the checkbox and invoking the event to check the checkbox. And in the selected index of that checkbox, I am invoking the repeater item command. Now it works perfectly. Thank you very much Tommy boy.

you helped me greatly.  

you post id 36959865 helped me.

thanks,
Rakhy.