I have a classic asp.net application. I have a datagrid user control with the MarkUp data source as:
<asp:SqlDataSource ID="DataSource" runat="server" DeleteCommand="UPDATE tblLSystem SET LS_Deleted='1' WHERE LS_ID=@LS_ID"
SelectCommand="SELECT * FROM vwLSystem WHERE LS_Deleted='0' order by LS_CreatedOn" OnSelected="DataSource_Selected" OnDeleted="DataSource_Selected" OnDeleting = "DataSource_Deleting">
<DeleteParameters>
<asp:Parameter Name="LS_ID" Type="Object" />
</DeleteParameters>
</asp:SqlDataSource>
The delete linkbutton template is declared as:
<asp:TemplateField HeaderText="" SortExpression="" Visible="True">
<ItemTemplate>
<asp:LinkButton ID="btnDelete" CommandName="Delete" runat="server" Text="Delete" />
</ItemTemplate>
</asp:TemplateField>
The code behind class name is "class StudLSystemGrid". And the DataSource_Deleting event function in this class is:
protected void DataSource_Deleting(object sender, SqlDataSourceCommandEventArgs e)
{
}
This user control is used in another asp.net page. It is registered in the page as:
<%@ Register Src="Controls/LSystemGrid.ascx" TagName="LSystemGrid" TagPrefix="uc4" %>
In the same page it is used to create one user control statically like this:
<uc4:LSystemGrid ID="LSystemGrid1" runat="server" ShowAuditFields="false" />
I also need to create multiple user controls depending on the situation dynamically in code behind. So I have a placeholder in MarkUp page:
<asp:PlaceHolder id="PlaceHolder1" runat="server"/>
And same codes in code behind to generate the user control:
ucLControl =
LoadControl("Controls/LSystemGrid.ascx")
as StudLSystemGrid;
ucLControl.ID = "LSystemGridPD" + i.ToString();
ucLControl.ShowAuditFields = false;
ucLControl.SqlDataSource.ConnectionString = ConnectionManager.ConnectionString;
ucLControl.DataBind();
PlaceHolder1.Controls.Add(ucLControl);
The statically generated user control works fine. Clicking the delete link can invoke the function "DataSource_Deleting" mentioned earlier. However the dynamically generated user control does not work. The function "DataSource_Deleting" is not hit and the item can not be deleted.
The html source for the delete link in statically markup user control is:
<a onclick="{alert('There'); return false;} ;"
id="ctl00_PageBody_LSystemGrid1_BaseGrid_ctl02_btnDelete"
href="javascript:__doPostBack('ctl00$PageBody$LSystemGrid1$BaseGrid$ctl02$btnDelete','')">Delete</a>
For dynamically generated user control from code behind is:
<a onclick="{alert('Here'); return false;} ;"
id="LSystemGridPD0_BaseGrid_ctl13_btnDelete"
href="javascript:__doPostBack('LSystemGridPD0$BaseGrid$ctl13$btnDelete','')">Delete</a>
Anybody knows what is wrong here? How can we determine the datagrid is in delete mode when using delete linkbutton in this way in code behind?
Thanks
Update: The user control is dynamically generated from -
protected void Page_Load(object sender, EventArgs e)
{
PreRender += LSystem_PreRender;
}
void LSystem_PreRender(object sender, EventArgs e)
{
ucLControl =
LoadControl("Controls/LSystemGrid.ascx")
as StudLSystemGrid;
ucLControl.ID = "LSystemGridPD0";
ucLControl.ShowAuditFields = false;
ucLControl.SqlDataSource.ConnectionString = ConnectionManager.ConnectionString;
ucLControl.DataBind();
PlaceHolder1.Controls.Add(ucLControl);
}
Update 2:
Update 2: The user control is dynamically generated from -
protected void Page_Load(object sender, EventArgs e)
{
ucLControl =
LoadControl("Controls/LSystemGrid.ascx")
as StudLSystemGrid;
ucLControl.ID = "LSystemGridPD0";
ucLControl.ShowAuditFields = false;
ucLControl.SqlDataSource.ConnectionString = ConnectionManager.ConnectionString;
ucLControl.EnableViewState = true;
ucLControl.DataBind();
PlaceHolder1.Controls.Add(ucLControl);
}
Found out the problem. Data has been bound after
Invoking ucLControl.DataBind(); again wipes out the bounded attributes.
After removed the line "ucLControl.DataBind();", the delete event can be triggered.