Solved

LinkButton created on PostBack; OnClick Event not fired

Posted on 2011-03-21
6
2,165 Views
Last Modified: 2012-05-11
I have a page with an UpdatePanel, this UpdatePanel contains a ListView with several items from a asp:SqlDataSource. This ListView also has a DataPager. The UpdatePanel has the properties "ChildrenAsTriggers=false" and "UpdateMode=Conditional". And the DataPager Id is the ASyncPostBackTrigger for the UpdatePanel.

<asp:UpdatePanel ID="upListOfItems" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="pListOfItems" />
</Triggers>
<ContentTemplate>
<asp:ListView DataSourceID="dsItems" ID="lvItems" runat="server">
<LayoutTemplate>
<asp:PlaceHolder runat="server" ID="itemPlaceholder" />
</LayoutTemplate>
<ItemTemplate>
<%#Eval("Title")%>
</ItemTemplate>
</ListView>
<asp:DataPager runat="server" ID="pListOfItems" PageSize="10" PagedControlID="lvItems">
</asp:DataPager>
</ContentTemplate>
</UpdatePanel>

Open in new window


The Items in the ItemTemplate have more than only this Title ofcourse, they look something like this:

<ItemTemplate>
<asp:UpdatePanel ID="upItem" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSetItemStatus" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="btnShowSubItems" EventName="Click" />
</Triggers>
<ContentTemplate>
<div class="item">
<div class="text">
<div class="top">
<h3>
<%#Eval("Title") %>
</h3>
</div>
</div>
<div class="description">
<%#Eval("Description") %>
</div>
<div class="menu">
<asp:LinkButton ID="btnSetItemStatus" runat="server" OnClick="BtnSetItemStatus_Click" CommandArgument='<%#Eval("ItemId") %>' Text="change status" />
</div>
<div class="notifications">
<asp:LinkButton ID="btnShowSubItems" runat="server" OnClick="BtnShowSubItems_Click" CommandArgument='<%#Eval("ItemId") %>' Text="show sub items" />
</div>
<asp:Panel ID="pnlSubItems" runat="server" Visible="false">
</asp:Panel>
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>

Open in new window


When the button BtnShowSubItems is clicked, the function BtnShowSubItems_Click fills the pnlSubItems with several sub item divs.
These div's have the same buttons as the items above, so each sub-item as a button to change the status of the sub item.
This button is created in the code behind as follows:

LinkButton setSubItemStatus = new LinkButton();
setSubItemStatus.Text = "change status";
setSubItemStatus.ID = "setitemstatus_" + subitemid.ToString();
setSubItemStatus.CommandArgument = subitemid.ToString();
setSubItemStatus.CausesValidation = false;
setSubItemStatus.Click += new EventHandler(BtnSetItemStatus_Click);

Open in new window


Everything is show correctly, but the event as set in the last line is not fired.

I believe this has something to do with the button click creating a PostBack. But I wonder how I can get this situation to work, because I cannot create these buttons in the Page_Init function as described on several other fora/topics.

Thanks for your help,
Ingrid
0
Comment
Question by:ingriT
  • 3
  • 2
6 Comments
 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 35181824
what about this:

setSubItemStatus.CommandArgument = subitemid.ToString();
-->
setSubItemStatus.CommandArgument = "DYNALINK_" + subitemid.ToString();

protected void grdProducts_RowCommand(Object sender, GridViewCommandEventArgs e)  {  
    if (e.CommandArgument.StartsWith("DYNALINK_") BtnSetItemStatus_Click(sender)

do you have RowCommand event? maybe it is hitting this when you click linkbutton, did you test this event?
0
 
LVL 51

Accepted Solution

by:
Huseyin KAHRAMAN earned 500 total points
ID: 35181863
also you can set CommandName

setSubItemStatus.CommandArgument = subitemid.ToString();
-->
setSubItemStatus.CommandName = "DYNALINK";
setSubItemStatus.CommandArgument = subitemid.ToString();

then on RowCommand

protected void grdProducts_RowCommand(Object sender, GridViewCommandEventArgs e)  {  
    if (e.CommandName == ("DYNALINK") BtnSetItemStatus_Click(sender);
0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 35181946
Hi, try this code

private LinkButton createButton(string title, int index)

{

LinkButton lnk = new LinkButton();

//lnk.ID = System.Guid.NewGuid().ToString("N") + "_" + index.ToString();//Event does not fire if I set ID with Guid

lnk.ID = index.ToString();

lnk.Text = title;

lnk.CommandArgument = index.ToString();

lnk.Click += new EventHandler(lnkPager_Click);

return lnk;

}

 

protected void lnkPager_Click(object sender, EventArgs e) //Page index changed function

{

LinkButton lnk = (LinkButton)sender;

CurrentPageIndex = int.Parse(lnk.CommandArgument);

System.Data.SqlClient.SqlConnection oDetailsPageDB = new SqlConnection(CDatabase.CONNECTION_STRING);

oDetailsPageDB.Open();

CUserSession oUser = (CUserSession)Session["USER"];

CManagercontent oMancontent = CManagercontent.getManagercontent(oUser, m_nCategoryKey);

bindData(oDetailsPageDB,oUser,oMancontent);

SaveCheckBoxStatus();

}
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Author Comment

by:ingriT
ID: 35183530
@samirbhogayta
I'm not sure what to do with your code?

@HainKurt
Your solution doesn't work, because the subitems are not separate rows, they are a dynamic part of the items in the rows of the original listview. This did get me to think, that I could render the sub items in a listview within the listview of items. So I'm trying that right now to see if that does the trick!
0
 
LVL 6

Author Closing Comment

by:ingriT
ID: 35183733
It was not the complete solution. It did hint me to the complete solution (see below)
0
 
LVL 6

Author Comment

by:ingriT
ID: 35183783
In the ItemTemplate for the main items I created the following construction:
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:ListView ID="subItems" runat="server" DataSourceId="dsSubItems">
<LayoutTemplate>
<asp:Placeholder Id="itemPlaceHolder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<%#Eval("Title") %>
<asp:LinkButton runat="server" CommandArgument='<%#Eval("ItemId") %>'
Text="change status" OnClick="SetItemStatus_Click" />
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource id="dsSubItems" runat="server" SelectCommandType="StoredProcedure" SelectCommand="Item_GetSubItems">
<SelectParameters>
<asp:Parameter Name="itemId" />
</SelectParameters>
</asp:SqlDataSource>
</ContentTemplate>
</asp:UpdatePanel>

Open in new window


And then I set the itemId parameter for this SqlDataSource in the OnClick for the ShowSubItems.

ListView lvItemReactions = (ListView)pnlItems.FindControl("lvItemReactions");
SqlDataSource dsSubItems = (SqlDataSource)pnlItems.FindControl("dsSubItems");
dsSubItems.SelectParameters["itemId"].DefaultValue = itemId.ToString();
lvItems.DataBind();

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unlocking a column in excel using C# 17 26
linq .concat equivalent with expression syntax 4 22
Selenium and Xpath 4 28
Build a string of emails from a gridview 2 13
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
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 antispam), the admini…

685 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