C#中.ascx文件的使用以及.ashx文件的使用

.ascx文件

ashx是用于写web handler的,一般用来处理生成动态图片、 生成动态文本等不需要回传处理的任务。

配合xmlhttp使用就可以构建一个简单的ajax效果了!

<%@ Register TagPrefix="uc1" TagName="WebCtl1" Src="../WebCtl1.ascx" %>

Register指令,在aspx页头引用ascx文件,TagPrefix 属性 获取指定控件的标记前缀,TagName 获取 HTML 标记的名称。
 
使用在html代码中使用:
 嵌入html的<body>中

<td><uc1:webctl1 id="WFC1" runat="server"></uc1:webctl1></td>

在后续的维护过程中,直接在ASCX文件和ASCX.cs文件里改代码就可以了,ASPX里面只要保留最基本的HTML元素就可以了,一个页面可以由多个用户控件(也就是ASCX)组成。


在ASCX里面写代码的意义就是可以更灵活开发,而且可以更好的代码复用。你可以开发几个通用的用户控件,就和.net提供的那些控件一样,灵活的放到你用到的各个页面里。而且以后维护的时候只要更改你的一处代码就可以了。


其实ascx你可以理解为Html里的一部分代码,只是嵌到aspx里而已,因为aspx内容多的时候实在是不太好管理,而且你把公共的Html部分写成ascx也可以公用在很多aspx里
比如Web页的下面注释部分ascx 也相当于ASP中的<!--#include file="Include/md5.asp" -->


.ashx文件

ashx是用于写web handler的,即为web应用程序中的一般事务处理程序文件。

通过前台的ajax方法传递值到.ashx文件后然后进行处理,可以返回text/html/script/xml/json这些类型值。

一般事务处理程序可以用来处理生成动态图片、 生成动态文本等不需要回传处理的任务。

示例:

namespace ComtecsolarManager.AjaxServer
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class PostHandlers : IHttpHandler, IReadOnlySessionState
    {
        HttpContext httpContext;
        static DataTable m_dtEntrustInfo = new DataTable();
        const string m_sConditionFormat = " AND {0} LIKE'%{1}%'";

        public void ProcessRequest(HttpContext context)
        {
            httpContext = context;
            context.Response.ContentType = "text/plain";

            switch (getFormString("method"))
            {
                case "GetCURR":
                    GetCURR();
                    break;
                case "GetUNIT":
                    GetUNIT();
                    break;
                case "GetCOUNTRY":
                    GetCOUNTRY();
                    break;
                case "GetGNoInfoByListNoType":
                    GetGNoInfoByListNoType();
                    break;
                case "GetGInfoByCopGNo":
                    GetGInfoByCopGNo();
                    break;
                case "GetNameModelByCopGNo":
                    GetNameModelByCopGNo();
                    break;
                case "GetCrystalInfo":
                    GetCrystalInfo();
                    break;                  
                case "GetSquareInfo":
                    GetSquareInfo();
                    break;             
                case "GetCircleInfo":
                    GetCircleInfo();
                    break;                 
                case "GetNameModelQtyByCopGNo":
                    GetNameModelQtyByCopGNo();
                    break;

                case "CheckStockQty":
                    CheckStockQty();
                    break;
            }
        }

        ///返回json值
        public string CreateJsonParameters(DataTable dataList)
        {
            StringBuilder JsonString = new StringBuilder();
            //Exception Handling       
            if (dataList != null && dataList.Rows.Count > 0)
            {
                // JsonString.Append("{ ");
                // JsonString.Append("\"TableInfo\":[ ");
                JsonString.Append("[ ");
                for (int i = 0; i < dataList.Rows.Count; i++)
                {
                    JsonString.Append("{ ");
                    for (int j = 0; j < dataList.Columns.Count; j++)
                    {
                        if (j < dataList.Columns.Count - 1)
                        {
                            JsonString.Append("\"" + dataList.Columns[j].ColumnName.ToString() + "\":" + "\"" + dataList.Rows[i][j].ToString() + "\",");
                        }
                        else if (j == dataList.Columns.Count - 1)
                        {
                            JsonString.Append("\"" + dataList.Columns[j].ColumnName.ToString() + "\":" + "\"" + dataList.Rows[i][j].ToString() + "\"");
                        }
                    }


                    if (i == dataList.Rows.Count - 1)
                    {
                        JsonString.Append("} ");
                    }
                    else
                    {
                        JsonString.Append("}, ");
                    }
                }
                //  JsonString.Append("]}");
                JsonString.Append("]");
                return JsonString.ToString();
            }
            else
            {
                return null;
            }
        }

        private void GetCircleInfo()
        {
            string listNoValue = getFormString("inputListNoValue");
            string copGNoValue = getFormString("inputCopGNoValue");
            string querySql = @"SELECT 
                                       KPR.SC_G_NO,KPR.RC_G_NO,KPR.OE_RESISTIVITY,KPR.LE_RESISTIVITY
                                      ,T.G_NAME,T.G_MODEL,T.CRRYSTALL,KPR.LENGTH,KPR.WEIGHT,T.REMARK
                                 FROM
                                       KMDK_AXCBT_ROUND KPR INNER JOIN 
                                      (SELECT 
                                               KRM.G_NAME,KRM.G_MODEL,KRM.COP_G_NO,KPC.CRRYSTALL,KPC.REMARK,KPC.SC_G_NO
                                       FROM 
                                               KMDK_RAW_WCT KRM
                                              ,KMDK_WSTB_CRYSTAL KPC 
                                       WHERE  KRM.COP_G_NO=KPC.SC_G_NO) T ON KPR.SC_G_NO=T.SC_G_NO
                                LEFT JOIN (SELECT LIST_NO_USAGE, RC_G_NO FROM KMDK_PROCESSED_SQUARE_HEAD WHERE SEL_FLAG!='6' AND LIST_NO_USAGE='" + SqlRunner.GetSqlPara(listNoValue)
                + "' AND RC_G_NO='" + SqlRunner.GetSqlPara(copGNoValue)
                + "') T3 ON KPR.RC_G_NO=T3.RC_G_NO WHERE KPR.RC_G_NO=(SELECT COP_G_NO FROM KMDK_RAW_MATERIAL WHERE LIST_NO='" + SqlRunner.GetSqlPara(listNoValue)
                + "' AND COP_G_NO='" + SqlRunner.GetSqlPara(copGNoValue)
                + "' AND ROWNUM <=1) and T3.LIST_NO_USAGE IS NULL ";
            DataTable data = SqlRunner.Instance.ExecuteDataTable(querySql);
            if (data != null && data.Rows.Count > 0)
            {
                StringBuilder result = new StringBuilder();
                for (int i = 0; i < data.Columns.Count; i++)
                {
                    result.Append("|" + data.Rows[0][i]);
                }
                httpContext.Response.Write(result.ToString().Substring(1));
                return;
            }
            httpContext.Response.Write("");
        }

        private void GetGInfoByCopGNo()
        {
            string bigType = getFormString("bigType");
            string listNo = getFormString("listNo");
            string copGNo = getFormString("inputValue");            
            if (bigType.Equals("3") || bigType.Equals("4") || bigType.Equals("5"))
            {                
                string querySql = "";
                if (bigType.Equals("4")) 
                {
                    querySql = @"SELECT GUID,G_NAME, G_MODEL FROM KMDK_PROCESSED_SQUARE T
                        INNER JOIN (SELECT LIST_NO FROM KMDK_PROCESSED_SQUARE_HEAD WHERE SEL_FLAG='5') H ON T.LIST_NO=H.LIST_NO WHERE T.LIST_NO='"
                        + SqlRunner.GetSqlPara(listNo) + "' AND T.SB_G_NO='" + SqlRunner.GetSqlPara(copGNo) + "'";
                }
                else if (bigType.Equals("5"))
                {
                    querySql = @"SELECT GUID,W_NAME, W_MODEL FROM KMDK_FINISHED_GOODS T
                        INNER JOIN (SELECT LIST_NO FROM KMDK_FINISHED_GOODS_HEAD WHERE SEL_FLAG='5') H ON T.LIST_NO=H.LIST_NO WHERE T.LIST_NO='"
                        + SqlRunner.GetSqlPara(listNo) + "' AND W_G_NO='" + SqlRunner.GetSqlPara(copGNo) + "'";
                }
                else if (bigType.Equals("3"))
                {
                    querySql = @"SELECT GUID,G_NAME, G_MODEL FROM KMDK_PROCESSED_ROUND T 
                        INNER JOIN (SELECT LIST_NO FROM KMDK_PROCESSED_ROUND_HEAD WHERE SEL_FLAG='5') H ON T.LIST_NO=H.LIST_NO WHERE T.LIST_NO='"
                        + SqlRunner.GetSqlPara(listNo) + "' AND RC_G_NO='" + SqlRunner.GetSqlPara(copGNo) + "'";
                }
                DataTable data = SqlRunner.Instance.ExecuteDataTable(querySql);
                if (data != null && data.Rows.Count > 0)
                {
                    string returnValue = data.Rows[0][0] + "," + data.Rows[0][1] + "," + data.Rows[0][2];
                    httpContext.Response.Write(returnValue);
                    return;
                }
            }

            httpContext.Response.Write("");
        }








已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页