经典ASP.NET删除事件不会在动态生成的DataGrid控件中删除链接调用会在、控件、事件、链接

2023-09-06 11:32:57 作者:无味

我有一个经典的asp.net应用程序。我有标记的数据源作为数据网格用户控件:

 < ASP:SqlDataSource的ID =数据源=服务器的DeleteCommand =UPDATE tblLSystem SET LS_Deleted ='1'WHERE LS_ID = @ LS_ID
    的SelectCommand =SELECT * FROM vwLSystem WHERE LS_Deleted ='0'ORDER BY LS_CreatedOnOnSelected =DataSource_SelectedOnDeleted =DataSource_SelectedOnDeleting =DataSource_Deleting>
    < D​​eleteParameters>
        < ASP:参数名称=LS_IDTYPE =对象/>
    < / DeleteParameters>
< / ASP:SqlDataSource的>
 

在删除LinkBut​​ton的模板声明为:

 < ASP:模板列HEADERTEXT =SORTEX pression =可见=真>
    <的ItemTemplate>
       < ASP:LinkBut​​ton的ID =btnDelete的CommandName =删除=服务器文本=删除/>
    < / ItemTemplate中>
< / ASP:的TemplateField>
 

后面的类名的code是阶级StudLSystemGrid。而在这个类中的DataSource_Deleting事件函数是:

 保护无效DataSource_Deleting(对象发件人,SqlDataSourceCommandEventArgs E)
{
}
 
基于AJAX.NET技术的DataGrid控件开发

该用户控件用于在另一asp.net页面。它被登记在页为:

 <%@注册SRC =控制/ LSystemGrid.ascx变量名=LSystemGrid标签preFIX =UC4%>
 

在同一页它用来静态创建一个用户控制是这样的:

 < UC4:LSystemGrid ID =LSystemGrid1=服务器ShowAuditFields =FALSE/>
 

我还需要建立在code动态视情况而后面的多个用户控件。所以我有一个占位符标记页:

 < ASP:占位符ID =PLACEHOLDER1=服务器/>
 

和相同的codeS在code后面生成用户控件:

  ucLControl =
      LoadControl(控制/ LSystemGrid.ascx)
      作为StudLSystemGrid;
    ucLControl.ID =LSystemGridPD+ i.ToString();
    ucLControl.ShowAuditFields = FALSE;
    ucLControl.SqlDataSource.ConnectionString = ConnectionManager.ConnectionString;
    ucLControl.DataBind();
    PlaceHolder1.Controls.Add(ucLControl);
 

的静态生成的用户控件工作正常。点击删除链接可以调用前面提到的功能DataSource_Deleting。然而,动态生成的用户控件无法正常工作。该功能DataSource_Deleting不打了该项目不能被删除。

在HTML源代码中静态地标记用户控件的删除链接:

 <一的onclick ={警报(有);返回false;};
的id =ctl00_PageBody_LSystemGrid1_BaseGrid_ctl02_btnDelete
href="javascript:__doPostBack('ctl00$PageBody$LSystemGrid1$BaseGrid$ctl02$btnDelete','')">Delete</a>
 

有关从code动态生成的用户控件的背后是:

 &LT;一的onclick ={警报('这里');返回false;};
的id =LSystemGridPD0_BaseGrid_ctl13_btnDelete
HREF =JavaScript的:__ doPostBack('LSystemGridPD0 $ BaseGrid $ ctl13 $ btnDelete','')&GT;删除&LT; / A&GT;
 

任何人都知道这里有什么问题?我们如何能确定数据网格在使用中删除的LinkBut​​ton以这种方式落后code?

在删除模式

感谢

更新:用户控件是动态的生成 -

 保护无效的Page_Load(对象发件人,EventArgs的)
{
    preRender + = LSystem_ preRender;
}

无效LSystem_ preRender(对象发件人,EventArgs的)
{
    ucLControl =
      LoadControl(控制/ LSystemGrid.ascx)
      作为StudLSystemGrid;
    ucLControl.ID =LSystemGridPD0;
    ucLControl.ShowAuditFields = FALSE;
    ucLControl.SqlDataSource.ConnectionString = ConnectionManager.ConnectionString;
    ucLControl.DataBind();
    PlaceHolder1.Controls.Add(ucLControl);
}
 

更新2:

更新2:用户控件是动态的生成 -

 保护无效的Page_Load(对象发件人,EventArgs的)
{
    ucLControl =
      LoadControl(控制/ LSystemGrid.ascx)
      作为StudLSystemGrid;
    ucLControl.ID =LSystemGridPD0;
    ucLControl.ShowAuditFields = FALSE;
    ucLControl.SqlDataSource.ConnectionString = ConnectionManager.ConnectionString;
    ucLControl.EnableViewState = TRUE;
    ucLControl.DataBind();
    PlaceHolder1.Controls.Add(ucLControl);
}
 

解决方案

发现出了问题。数据已经绑定后

  ucLControl = LoadControl(控制/ LSystemGrid.ascx)作为StudLSystemGrid;
 

调用ucLControl.DataBind();又抹了有限的属性。

删除后行ucLControl.DataBind();,删除事件,可触发

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

   ucLControl = LoadControl("Controls/LSystemGrid.ascx")       as StudLSystemGrid;

Invoking ucLControl.DataBind(); again wipes out the bounded attributes.

After removed the line "ucLControl.DataBind();", the delete event can be triggered.