Solved

LinkButton created on PostBack; OnClick Event not fired

Posted on 2011-03-21
6
2,110 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

911 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

19 Experts available now in Live!

Get 1:1 Help Now