2015-01-20 2 views
0

각 응용 프로그램을 나타내는 여러 sqlserver 테이블이 있습니다. 이 모든 테이블을 mantain (보기/편집/삽입/삭제)해야합니다. 이 응용 프로그램 테이블에는 모두 ID라는 기본 기본 키 열이 있습니다.ASP.NET 동적 목록 뷰 웹 응용 프로그램

이 작업을 수행하기 위해 이러한 응용 프로그램 테이블을 유지 관리하기위한 페이지를 빌드하는 기본 SqlServer 구성 테이블을 만들었습니다.

dbo.ApplGenerator_Applications : 이 테이블은 웹 응용 프로그램의보기 창에서 데이터를 정렬하는 추가 필드와 실제 SQLSERVER 테이블에 응용 프로그램을 매핑합니다.

CREATE TABLE [dbo].[ApplGenerator_Applications](
    [ApplicationName] [varchar](50) NOT NULL, 
    [RealTable] [varchar](255) NOT NULL, 
    [OrderBy] [varchar](255) NOT NULL, 
    [Desc_OrderBy] [varchar](255) NOT NULL 
) 

기본 키의 이름과 유형이 항상 같기 때문에이 정보를 응용 프로그램 구성 테이블에 추가 할 필요가 없습니다.

ApplGenerator_Master : 가 • • 데이터 연계하여

CREATE TABLE [dbo].[ApplGenerator_Master]( [ApplicationName] [varchar](50) NOT NULL, [FieldId] [varchar](50) NOT NULL, [FieldTitle] [varchar](50) NULL, [FieldName] [varchar](255) NULL, [FieldAlign] [varchar](50) NULL, [FieldWidth] [varchar](50) NULL, [Position] [int] NULL ) 

, 이러한 PIN이 할 수있는 쿼리를 작성 • 목록보기 열을 구축 검색 기능과 헤더를 구축 :
이 표가 기본이다 두 개의 구성 테이블은 listview 오브젝트 (필터 및 데이터)를 빌드하고 데이터 목록보기를 채우는 기본입니다.

Master.aspx :

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Master.aspx.cs" Inherits="ApplicationGenerator.Master" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <link href="/Styles/StyleSheet.css" rel="stylesheet" type="text/css" /> 
    <script src="/Scripts/Script.js" type="text/javascript"></script> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
     <ajax:ToolkitScriptManager ID="MasterScriptManager" runat="server"></ajax:ToolkitScriptManager> 
     <script type="text/javascript"> 
      document.body.onload = function() { resizeContent('pageContent'); } 
      document.body.onresize = function() { resizeContent('pageContent'); } 
     </script> 
     <asp:UpdatePanel ID="myUpdatePanel" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true"> 
     <ContentTemplate> 
      <asp:DataPager ID="Master_Data_DataPager" runat="server" 
       PagedControlID="Master_Data_ListView" 
       PageSize="50"> 
       <Fields> 
        <asp:NextPreviousPagerField ButtonType="Image" 
               FirstPageImageUrl="/Images/DataPager/first.png" 
               NextPageImageUrl="/Images/DataPager/next.png" 
               PreviousPageImageUrl="/Images/DataPager/previous.png" 
               LastPageImageUrl="/Images/DataPager/last.png" 
               ShowFirstPageButton="True" 
               ShowNextPageButton="False" /> 
        <asp:NumericPagerField /> 
        <asp:NextPreviousPagerField ButtonType="Image" 
               FirstPageImageUrl="/Images/DataPager/first.png" 
               NextPageImageUrl="/Images/DataPager/next.png" 
               PreviousPageImageUrl="/Images/DataPager/previous.png" 
               LastPageImageUrl="/Images/DataPager/last.png" 
               ShowLastPageButton="True" 
               ShowPreviousPageButton="False" /> 
       </Fields> 
      </asp:DataPager> 
      <asp:ListView ID="Master_Filter_ListView" runat="server"> 
      </asp:ListView> 
      <div id="pageContent" style="overflow:auto;"> 
       <asp:ListView ID="Master_Data_ListView" runat="server" 
        DataKeyNames="ID" DataSourceID="Master_Data_SqlDataSource" 
        OnPagePropertiesChanging="Master_Data_ListView_PagePropertiesChanging"> 
       </asp:ListView> 
       <asp:SqlDataSource ID="Master_Data_SqlDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" 
        SelectCommand="dbo.ApplGenerator_Master_Data_GetList" SelectCommandType="StoredProcedure" 
        DeleteCommand="dbo.ApplGenerator_Master_DeleteRecord" DeleteCommandType="StoredProcedure" 
        OnSelecting="ApplicationUsersSqlDataSource_Selecting" 
        OnDeleting="ApplicationUsersSqlDataSource_Deleting"> 
        <DeleteParameters> 
         <asp:Parameter Direction="Input" Name="ID" /> 
        </DeleteParameters> 
       </asp:SqlDataSource> 
      </div> 
     </ContentTemplate> 
     </asp:UpdatePanel> 
    </form> 
</body> 
</html> 

Master.aspx.cs

using System; 
using System.Collections.Generic; 
using System.Data; 
using System.Data.SqlClient; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.HtmlControls; 
using System.Web.UI.WebControls; 

namespace ApplicationGenerator 
{ 
    public partial class Master : System.Web.UI.Page 
    { 
     private static string sConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString(); 
     private static string sApplication = ""; 

     private static DataTable dtApplication = null; 
     private static DataTable dtFilter = null; 

     public struct ExternalReferencesStruct 
     { 
      public ListView DataListView; 
     } 

     public class myClass 
     { 
      private ExternalReferencesStruct classObjs; 

      public myClass(ExternalReferencesStruct paramObjs) 
      { 
       classObjs = paramObjs; 
      } 
     } 

     protected void Page_Init(object sender, EventArgs e) 
     { 
      sApplication = Request.QueryString["application"]; 

      dtApplication = ApplicationGenerator_Application_Get(); 
      dtFilter = ApplicationGenerator_Master_Filter_GetList(); 

      Build_MasterFilterListView(); 
      Build_MasterDataListView(); 

      Master_Filter_ListView.DataBind(); 
     } 

     protected void Page_Load(object sender, EventArgs e) 
     { 
      //if (!this.Page.IsPostBack) 
      //{ 
       Master_Data_ListView.DataBind(); 
      //} 
      string scriptString = "<script LANGUAGE='javascript'>resizeContent('pageContent');</script>"; 
      ScriptManager.RegisterStartupScript(this, this.GetType(), "myScript_Load", scriptString, false); 
     } 

     #region Master_Filtro_ListView 
     public void Build_MasterFilterListView() 
     { 
      ExternalReferencesStruct ExternalObjs = new ExternalReferencesStruct(); 
      ExternalObjs.DataListView = Master_Data_ListView; 

      Master_Filter_ListView.LayoutTemplate = new MasterFilterLayoutTemplate(ExternalObjs); 
      Master_Filter_ListView.ItemTemplate = new MasterFilterItemTemplate(); 
      Master_Filter_ListView.EmptyDataTemplate = new MasterFilterLayoutTemplate(ExternalObjs); 
     } 

     public class MasterFilterLayoutTemplate : myClass, ITemplate 
     { 
      ExternalReferencesStruct ExternalReferences = new ExternalReferencesStruct(); 
      public MasterFilterLayoutTemplate(ExternalReferencesStruct paramExternalReferences) 
       : base(paramExternalReferences) 
      { 
       ExternalReferences = paramExternalReferences; 
      } 

      protected void ApplyFilter_Click(object sender, ImageClickEventArgs e) 
      { 
       ExternalReferences.DataListView.DataBind(); 
      } 

      public void InstantiateIn(System.Web.UI.Control container) 
      { 
       HtmlTable myTable = new HtmlTable(); 
       myTable.Width = "100%"; 
       myTable.Border = 0; 
       myTable.CellPadding = 0; 
       myTable.CellSpacing = 0; 
       myTable.Style.Add("table-layout", "fixed"); 

       HtmlTableRow row = null; 
       HtmlTableRow rowF = null; 
       HtmlTableCell cell = null; 
       TextBox tbFiltro = null; 

       row = new HtmlTableRow(); 
       rowF = new HtmlTableRow(); 
       row.ID = "Row_Names"; 
       rowF.ID = "Row_Filters"; 

       ImageButton ib = null; 

       //New Record 
       cell = new HtmlTableCell(); 
       cell.Width = "20px"; 
       cell.Align = "center"; 
        ib = new ImageButton(); 
        ib.ID = "NewRecImageButton"; 
        ib.ImageUrl = "/Images/Applications/New.png"; 
        ib.ToolTip = "New Record"; 
        ib.OnClientClick = "javascript:ViewEditDetail('" + sApplication + "', '-1');return false;"; 
        cell.Controls.Add(ib); 
       row.Cells.Add(cell); 

       //Filter 
       cell = new HtmlTableCell(); 
       cell.Width = "20px"; 
       cell.Align = "center"; 
       ib = new ImageButton(); 
       ib.ID = "FilterImageButton"; 
       ib.ImageUrl = "/Images/Applications/Filter.png"; 
       ib.ToolTip = "Apply Filtro"; 
       ib.Click += new ImageClickEventHandler(ApplyFilter_Click); 
       cell.Controls.Add(ib); 
       rowF.Cells.Add(cell); 

       //Field Names 
       foreach (DataRow dtrow in dtFilter.Rows) 
       { 
        //Header - Field Titles 
        cell = new HtmlTableCell(); 
        cell.Width = dtrow["FieldWidth"].ToString(); 
        cell.Align = "center"; 
        cell.Style.Add("font-weight", "bold"); 
        cell.Controls.Add(new LiteralControl(dtrow["FieldTitle"].ToString())); 
        row.Cells.Add(cell); 

        //Header - Filter TextBoxes 
        cell = new HtmlTableCell(); 
        cell.Width = dtrow["FieldWidth"].ToString(); 
        tbFiltro = new TextBox(); 
        tbFiltro.ID = dtrow["FieldId"].ToString(); 
        tbFiltro.Width = new Unit("99%"); 
        cell.Controls.Add(tbFiltro); 
        rowF.Cells.Add(cell); 
       } 
       myTable.Rows.Add(row); 
       myTable.Rows.Add(rowF); 

       //Container para Items 
       row = new HtmlTableRow(); 
       row.ID = "itemPlaceholder"; 
       myTable.Rows.Add(row); 

       container.Controls.Add(myTable); 
      } 
     } 

     public class MasterFilterItemTemplate : ITemplate 
     { 
      public void InstantiateIn(System.Web.UI.Control container) 
      { 
      } 
     } 
     #endregion 

     #region Master_Data_ListView 
     public void Build_MasterDataListView() 
     { 
      ExternalReferencesStruct ExternalObjs = new ExternalReferencesStruct(); 
      ExternalObjs.DataListView = Master_Data_ListView; 

      Master_Data_ListView.LayoutTemplate = new MasterDataLayoutTemplate(ExternalObjs); 
      Master_Data_ListView.ItemTemplate = new MasterDataItemTemplate(ExternalObjs); 
      Master_Data_ListView.EmptyDataTemplate = new MasterDataLayoutTemplate(ExternalObjs); 
     } 

     public class MasterDataLayoutTemplate : myClass, ITemplate 
     { 
      ExternalReferencesStruct ExternalReferences = new ExternalReferencesStruct(); 
      public MasterDataLayoutTemplate(ExternalReferencesStruct paramExternalReferences) 
       : base(paramExternalReferences) 
      { 
       ExternalReferences = paramExternalReferences; 
      } 

      public void InstantiateIn(System.Web.UI.Control container) 
      { 
       HtmlTable myTable = new HtmlTable(); 
       myTable.Width = "100%"; 
       myTable.Border = 0; 
       myTable.CellPadding = 0; 
       myTable.CellSpacing = 0; 
       myTable.Style.Add("table-layout", "fixed"); 

       HtmlTableRow row = null; 
       HtmlTableCell cell = null; 

       row = new HtmlTableRow(); 

       //Button Delete 
       cell = new HtmlTableCell(); 
       cell.Width = "20px"; 
       cell.Align = "center"; 
       row.Cells.Add(cell); 

       //Field Names 
       foreach (DataRow dtrow in dtFilter.Rows) 
       { 
        //Header - Field Names 
        cell = new HtmlTableCell(); 
        cell.Width = dtrow["FieldWidth"].ToString(); 
        row.Cells.Add(cell); 
       } 
       myTable.Rows.Add(row); 

       //Item Container 
       row = new HtmlTableRow(); 
       row.ID = "itemPlaceholder"; 
       myTable.Rows.Add(row); 

       container.Controls.Add(myTable); 
      } 
     } 

     public class MasterDataItemTemplate : myClass, ITemplate 
     { 
      ExternalReferencesStruct ExternalReferences = new ExternalReferencesStruct(); 
      public MasterDataItemTemplate(ExternalReferencesStruct paramExternalReferences) 
       : base(paramExternalReferences) 
      { 
       ExternalReferences = paramExternalReferences; 
      } 

      public void InstantiateIn(System.Web.UI.Control container) 
      { 
       HtmlTableRow row = new HtmlTableRow(); 

       row.DataBinding += new EventHandler(row_DataBinding); 

       DataRowView dataRowView = ((ListViewDataItem)container).DataItem as DataRowView; 
       if (dataRowView != null) 
       { 
        string sPK = dataRowView[0].ToString(); 
        row.Attributes.Add("onclick", "ViewEditDetail('" + sApplication + "', '" + sPK + "');"); 
        row.Attributes.Add("onmouseout", "MouseOut(this);"); 
        row.Attributes.Add("onmouseover", "MouseOver(this);"); 
        row.Attributes.Add("title", "View/Edit Details"); 
        row.Style.Add("cursor", "pointer"); 
       } 
       container.Controls.Add(row); 
      } 

      protected void row_DataBinding(object sender, EventArgs e) 
      { 
       HtmlTableRow row = (HtmlTableRow)sender; 
       DataRowView dataRowView = ((ListViewDataItem)row.NamingContainer).DataItem as DataRowView; 
       string sPK = dataRowView[0].ToString(); 

       HtmlTableCell cell = null; 
       ImageButton ib = null; 

       //Button Delete 
       cell = new HtmlTableCell(); 
       cell.Width = "20px"; 
       cell.Align = "center"; 
       ib = new ImageButton(); 
       ib.ID = "DelImageButton"; 
       ib.CommandName = "Delete"; 
       ib.ImageUrl = "/Images/Applications/Delete.png"; 
       ib.ToolTip = "Delete Record"; 
       ib.OnClientClick = "javascript:return myConfirm('Proceed with Record Elimination?');"; 
       cell.Controls.Add(ib); 
       row.Controls.Add(cell); 

       int i = 1; 
       foreach (DataRow dtrow in dtFilter.Rows) 
       { 
        cell = new HtmlTableCell(); 
        cell.Width = dtrow["FieldWidth"].ToString(); 
        Literal MyLiteral = new Literal(); 
        MyLiteral.Text = dataRowView[i++].ToString(); 

        cell.Controls.Add(MyLiteral); 
        row.Controls.Add(cell); 
       } 
      } 
     } 
     #endregion 

     #region Data Functions 
     private static DataTable ApplicationGenerator_Application_Get() 
     { 
      string sStrSP = ""; 

      SqlConnection conn = null; 
      SqlCommand cmd = null; 
      SqlDataAdapter da = null; 
      DataSet ds = null; 

      sStrSP = "dbo.ApplGenerator_Application_Get"; 

      conn = new SqlConnection(sConnectionString); 
      cmd = new SqlCommand(); 
      ds = new DataSet(); 
      cmd.Connection = conn; 
      cmd.CommandType = CommandType.StoredProcedure; 
      cmd.CommandText = sStrSP; 
      cmd.Parameters.Add(new SqlParameter("@ApplicationName", sApplication)); 

      conn.Open(); 
      da = new SqlDataAdapter(cmd); 
      da.Fill(ds); 

      DataTable dt = ds.Tables[0]; 

      cmd.Dispose(); 
      da.Dispose(); 
      ds.Dispose(); 
      conn.Close(); 

      return dt; 
     } 

     private static DataTable ApplicationGenerator_Master_Filter_GetList() 
     { 
      string sStrSP = ""; 

      SqlConnection conn = null; 
      SqlCommand cmd = null; 
      SqlDataAdapter da = null; 
      DataSet ds = null; 

      sStrSP = "dbo.ApplGenerator_Master_Filter_GetList"; 

      conn = new SqlConnection(sConnectionString); 
      cmd = new SqlCommand(); 
      ds = new DataSet(); 
      cmd.Connection = conn; 
      cmd.CommandType = CommandType.StoredProcedure; 
      cmd.CommandText = sStrSP; 
      cmd.Parameters.Add(new SqlParameter("@ApplicationName", sApplication)); 

      conn.Open(); 
      da = new SqlDataAdapter(cmd); 
      da.Fill(ds); 

      DataTable dt = ds.Tables[0]; 

      cmd.Dispose(); 
      da.Dispose(); 
      ds.Dispose(); 
      conn.Close(); 

      return dt; 
     } 

     private static DataTable ApplicationGenerator_Master_Data_GetList(ListView FilterListView) 
     { 
      DataTable dtWhere = new DataTable(); 
      dtWhere.Clear(); 
      dtWhere.Columns.Add("FieldId"); 
      dtWhere.Columns.Add("FieldText"); 

      string sFilterId = ""; 
      string sFilterText = ""; 
      TextBox tbFilter = null; 
      foreach (DataRow dtrow in dtFilter.Rows) 
      { 
       sFilterId = dtrow["FieldId"].ToString(); 
       tbFilter = (TextBox)FilterListView.Controls[0].FindControl(sFilterId); 
       if (tbFilter != null) 
       { 
        sFilterText = tbFilter.Text; 
        if (sFilterText != "") 
        { 
         DataRow dtWhereRow = dtWhere.NewRow(); 
         dtWhereRow["FieldId"] = sFilterId; 
         dtWhereRow["FieldText"] = sFilterText; 
         dtWhere.Rows.Add(dtWhereRow); 
        } 
       } 
      } 

      DataTable dt = new DataTable(); 

      SqlConnection conn = new SqlConnection(sConnectionString); 
      SqlCommand cmd = null; 
      conn.Open(); 

      try 
      { 
       string sStrSP = "dbo.ApplGenerator_Master_Data_GetList"; 
       cmd = new SqlCommand(sStrSP, conn); 
       cmd.CommandType = CommandType.StoredProcedure; 
       cmd.Parameters.Add(new SqlParameter("@ApplicationName", sApplication)); 
       cmd.Parameters.Add(new SqlParameter("@dtWhere", dtWhere)); 

       SqlDataAdapter adapter = new SqlDataAdapter(cmd); 
       adapter.Fill(dt); 
      } 
      finally 
      { 
       cmd.Dispose(); 
       if (conn != null) 
        conn.Close(); 
      } 

      return dt; 
     } 
     #endregion 

     #region Master_Data_ListView 
     protected virtual void Master_Data_ListView_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e) 
     { 
      string scriptString = ""; 

      Master_Data_DataPager.SetPageProperties(e.StartRowIndex, e.MaximumRows, false); 
      Master_Data_ListView.DataBind(); 

      scriptString = "<script LANGUAGE='javascript'>resizeContent('pageContent');</script>"; 
      ScriptManager.RegisterStartupScript(this, this.GetType(), "myScript_PageChanging", scriptString, false); 
     } 
     #endregion 

     #region Master_Data_SqlDataSource 
     protected void ApplicationUsersSqlDataSource_Selecting(object sender, SqlDataSourceSelectingEventArgs e) 
     { 
      DataTable dtWhere = new DataTable(); 
      dtWhere.Clear(); 
      dtWhere.Columns.Add("FieldId"); 
      dtWhere.Columns.Add("FieldText"); 

      string sFilterId = ""; 
      string sFilterText = ""; 
      TextBox tbFilter = null; 
      foreach (DataRow dtrow in dtFilter.Rows) 
      { 
       sFilterId = dtrow["FieldId"].ToString(); 
       tbFilter = (TextBox)Master_Filter_ListView.Controls[0].FindControl(sFilterId); 
       if (tbFilter != null) 
       { 
        sFilterText = tbFilter.Text; 
        if (sFilterText != "") 
        { 
         DataRow dtWhereRow = dtWhere.NewRow(); 
         dtWhereRow["FieldId"] = sFilterId; 
         dtWhereRow["FieldText"] = sFilterText; 
         dtWhere.Rows.Add(dtWhereRow); 
        } 
       } 
      } 

      SqlParameter Param_Aplicacao = new SqlParameter("@ApplicationName", sApplication) 
      { 
       Direction = ParameterDirection.Input 
      }; 

      SqlParameter Param_dtwhere = new SqlParameter("@dtWhere", dtWhere) 
      { 
       Direction = ParameterDirection.Input 
      }; 

      e.Command.Parameters.Add(Param_Aplicacao); 
      e.Command.Parameters.Add(Param_dtwhere); 
     } 

     protected void ApplicationUsersSqlDataSource_Deleting(object sender, SqlDataSourceCommandEventArgs e) 
     { 
      SqlParameter Param_Aplicacao = new SqlParameter("@ApplicationName", sApplication) 
      { 
       Direction = ParameterDirection.Input 
      }; 

      e.Command.Parameters.Add(Param_Aplicacao); 
     } 
     #endregion 
    } 
} 

응용 프로그램은 호출기는 새로운 필터 버튼 (컨트롤에서 잘 작동 거의 준비 필터 ListView) 예상대로 작동합니다.

문제는 아이콘 (데이터 ListView ItemTemplate의 ImageButton 컨트롤)을 삭제하기 위해 아이콘을 클릭해도 삭제할 코드와 데이터 목록 뷰가 모두 다시로드되지 않습니다.

Sourcecode VisualStudio2010

SqlServer2012 backup

내가 ASP.NET에 새로 온 사람과 확실히 내가 (매우) 잘못 somethig에하고 있어요

. 이 문제를 해결하는 방법에 대한 아이디어가 없습니다. 누군가 도움을 줄 수 있습니까?

감사합니다,

마리오 Nunes 보낸

당신은 동적으로 ListView에서 컨트롤을 만드는
+0

이 질문은 약간 * 완료되었습니다. 질문과 관련이없는 코드를 제거 할 수 있습니까? 나는 모든 것을 스크롤하기가 어렵다. – lucrativelucas

답변

1

이 문제는 다시 게시에 이러한 컨트롤이 손실됩니다 (그래서 그 이벤트는) 데이터가 다시 바인더 제본되지 않았기 때문이다 .그게 해결되는지

참조 신속을 시도해보십시오

protected void Page_Load(object sender, EventArgs e) 
{ 
    //if (!this.Page.IsPostBack) 
    //{ 
     Master_Data_ListView.DataBind(); 
    //} 

편집 :

신경 끄시 고 내가 잘못된 태그를 찾고 있었어요! : o

+0

답장을 보내 주셔서 감사합니다. 이제 ListView가 다시로드되지만 SqlDataSource 삭제 이벤트는 발생하지 않습니다. –

+0

@MarioNunes 위의 편집을 참조하십시오. –

+0

다시 한번 감사드립니다. 나는 C# 코드를 되돌려 놓았다. ListView 필터에는 ListView 데이터와 다른 데이터 소스가 있습니다. 필터 ListView는 머리글 (제목 및 필터 텍스트 상자) 역할을하며 dataListView는 실제 데이터를 표시합니다. –

관련 문제