Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

LinkButton created on PostBack; OnClick Event not fired

Posted on 2011-03-21
6
Medium Priority
?
2,304 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 61

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 61

Accepted Solution

by:
HainKurt earned 1500 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
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 anti-spam), the admin…
Suggested Courses

885 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