Link to home
Start Free TrialLog in
Avatar of ingriT
ingriTFlag for Netherlands

asked on

LinkButton created on PostBack; OnClick Event not fired

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
Avatar of HainKurt
HainKurt
Flag of Canada image

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?
ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada 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
Avatar of SAMIR BHOGAYTA
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();

}
Avatar of ingriT

ASKER

@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!
Avatar of ingriT

ASKER

It was not the complete solution. It did hint me to the complete solution (see below)
Avatar of ingriT

ASKER

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