Solved

LinkButton created on PostBack; OnClick Event not fired

Posted on 2011-03-21
6
2,094 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:HainKurt
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:
HainKurt 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now