【C#编程最佳实践 十】控件使用及Ajax通信

简介: 【C#编程最佳实践 十】控件使用及Ajax通信

控件隐藏

控件前端显示,选择框

<div class="form-group">
 <label class="col-sm-3 control-label">API类型</label>
  <div class="col-sm-8">
       <input class="apiType" type="radio" name="apiType" value="0" title="基础数据" />基础数据
       <input class="apiType" type="radio" name="apiType" value="1" title="单实体" checked="checked" />单实体
       <input class="apiType" type="radio" name="apiType" value="3" title="多实体" />多实体
       <input class="apiType" type="radio" name="apiType" value="2" title="业务数据接口" />业务数据
       <span class="help-block"><span class="required">*</span>说明:选择业务对接类型</span>
   </div>
 </div>

依据class对应的apitype如果发生改变,则触发函数,对应确定哪些控件显示,哪些隐藏。

注意:这里无论是隐藏还是显示,四种条件各自涉及到的控件要取并集,也就是说即使不隐藏也要写。

<script type="text/javascript">
        $(function () {
            var item = document.getElementById("IsDependsBasic")
            item.parentNode.parentNode.style = "";
            item = document.getElementById("whiteList")
            item.parentNode.parentNode.style = "display:none";
            item = document.getElementById("showFields")
            item.parentNode.parentNode.style = "";  //可以显示
            item = document.getElementById("getApiListUrl")
            item.parentNode.parentNode.style = "display:none";   //设置不显示
            item = document.getElementById("importApiUrl")
            item.parentNode.parentNode.style = "display:none";
            item = document.getElementById("isEnable")
            item.parentNode.parentNode.style = "display:none";
            $(".apiType").change(function () {
                // var item = document.getElementById("getApiListUrl")
                // item.parentNode.parentNode.style = "display:none";
                //document.getElementById("importApiUrl")
                console.info(item)
                var apiType = $("input[name='apiType']:checked").val();
                if (apiType == "0") {
                    var item = document.getElementById("IsDependsBasic")
                    item.parentNode.parentNode.style = "display:none";
                    item = document.getElementById("whiteList")
                    item.parentNode.parentNode.style = "display:none";
                    item = document.getElementById("getApiListUrl")
                    item.parentNode.parentNode.style = "display:none";
                    item = document.getElementById("showFields")
                    item.parentNode.parentNode.style = "";
                    item = document.getElementById("importApiUrl")
                    item.parentNode.parentNode.style = "display:none";
                    item = document.getElementById("isEnable")
                    item.parentNode.parentNode.style = "display:none";
                }
                if (apiType == "1") {
                    var item = document.getElementById("IsDependsBasic")
                    item.parentNode.parentNode.style = "";
                    item = document.getElementById("whiteList")
                    item.parentNode.parentNode.style = "display:none";
                    item = document.getElementById("showFields")
                    item.parentNode.parentNode.style = "";
                    item = document.getElementById("getApiListUrl")
                    item.parentNode.parentNode.style = "display:none";
                    item = document.getElementById("importApiUrl")
                    item.parentNode.parentNode.style = "display:none";
                    item = document.getElementById("isEnable")
                    item.parentNode.parentNode.style = "display:none";
                }
                if (apiType == "3") {
                    var item = document.getElementById("whiteList")
                    item.parentNode.parentNode.style = "";
                    item = document.getElementById("IsDependsBasic")
                    item.parentNode.parentNode.style = "";
                    item = document.getElementById("getApiListUrl")
                    item.parentNode.parentNode.style = "display:none";
                    item = document.getElementById("showFields")
                    item.parentNode.parentNode.style = "";
                    item = document.getElementById("importApiUrl")
                    item.parentNode.parentNode.style = "display:none";
                    item = document.getElementById("isEnable")
                    item.parentNode.parentNode.style = "display:none";
                }
                if (apiType == "2") {
                    var item = document.getElementById("IsDependsBasic")
                    item.parentNode.parentNode.style = "display:none";
                    item = document.getElementById("whiteList")
                    item.parentNode.parentNode.style = "display:none";
                    item = document.getElementById("showFields")
                    item.parentNode.parentNode.style = "display:none";
                    item = document.getElementById("getApiListUrl")
                    item.parentNode.parentNode.style = "";
                    item = document.getElementById("importApiUrl")
                    item.parentNode.parentNode.style = "";
                    item = document.getElementById("isEnable")
                    item.parentNode.parentNode.style = "display:none";
                }
                if (apiType == "2") {
                    $("#getApiListUrl").val("");
                    $("#importApiUrl").val("");
                    $("#getApiListUrl").attr("readonly", false);
                    $("#importApiUrl").attr("readonly", false);
                }
                else {
                    $("#getApiListUrl").val("/Biz/GetObjectDataList");
                    $("#importApiUrl").val("/Biz/ImportObjectData");
                    $("#getApiListUrl").attr("readonly", "readonly");
                    $("#importApiUrl").attr("readonly", "readonly");
                    //禁用不可以修改
                }
            });
        });
    </script>

控件不可点

获取输入控件中的name,如果为以下这几种直接禁用

$("input[name='apiType']").attr("disabled", true);
$("input[name='appName']").attr("disabled", true);
$("input[name='resourceCode']").attr("disabled", true);

当然也可以启用

$("input[name='resourceCode']").attr("disabled", false);

Ajax通信标准写法

控制器将需要访问js函数封装到链接里返回给前端

describe = "停用";
hrefEnabled = $"<a style='cursor: pointer; ' onclick=EnabledAPI('" + item.ID + "','" + setEnabled + "','" + describe + "',this)> <button class ='btn btn-primary' style ='padding:2px 5px'>停用 </button></ a >";

前端通过点击js函数,得到参数id,enabled

//启用按钮
        function EnabledAPI(id, enabled, describe, tr) {
            var currentTr = $(tr).parent().parent().find("td");
            resourceName = currentTr[0].innerHTML;
            //提示信息赋值,弹框
            $("#hidId").val(id);
            $("#hidEnabled").val(enabled);
            $("#sureMessage").html("是否" + describe + "API:" + resourceName + "  的状态?");
            $('#ConfirmEnabledModel').modal('show');
        }

将值传到确认启用或停用的模态框id,enabled

// <!--设置是否启用的确认模态框-->
                    <div class="modal fade" id="ConfirmEnabledModel" tabindex="-1" role="dialog" data-backdrop="static">
                        <div class="modal-dialog" role="document">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                    <h4 class="modal-title" id="gridSystemModalLabel">API注册</h4>
                                </div>
                                <input type="hidden" value="" id="hidId" />
                                <input type="hidden" value="" id="hidEnabled" />
                                <div class="modal-body">
                                    <blockquote>
                                        <p id="sureMessage"></p>
                                    </blockquote>
                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                                        <button id="ConfirmEnabled" type="button" class="btn btn-primary">确定</button>
                                    </div>
                                </div>
                            </div><!-- /.modal-content -->
                        </div><!-- /.modal-dialog -->
                    </div>

点击确认模态框后,将参数值传进来id,enabled

```
       //确定启用
        $("#ConfirmEnabled").click(function () {
            var id = $("#hidId").val();
            var enabled = $("#hidEnabled").val();
            if (id == "")
                return false;
            if (enabled == "")
                return false;
            $('#ConfirmEnabledModel').modal('hide');
            $.ajax({
                type: "post",
                dataType: "json",
                url: "/API/EnabledAPI",
                data: {
                    id: id,
                    enabled: enabled
                },
                success: function (data) {
                    $('#APIModel').modal('hide');
                    alert(data.message ? data.message : "success");
                    window.location.reload();
                },
                error: function (data) {
                    alert(data.message ? data.message : "error");
                }
            });
        });


相关文章
|
1月前
|
数据处理 C# C++
如何使用C#和C++结构体实现Socket通信
如何使用C#和C++结构体实现Socket通信
|
1月前
|
C# 数据库 开发者
44.c#:combobox控件
44.c#:combobox控件
19 1
|
1月前
|
C# 数据库
40.c#:TreeView 控件
40.c#:TreeView 控件
19 1
|
1月前
|
C#
24. C# 编程:用户设定敌人初始血值的实现
24. C# 编程:用户设定敌人初始血值的实现
22 0
|
7天前
|
存储 安全 网络安全
C#编程的安全性与加密技术
【4月更文挑战第21天】C#在.NET框架支持下,以其面向对象和高级特性成为安全软件开发的利器。本文探讨C#在安全加密领域的应用,包括使用System.Security.Cryptography库实现加密算法,利用SSL/TLS保障网络传输安全,进行身份验证,并强调编写安全代码的重要性。实际案例涵盖在线支付、企业应用和文件加密,展示了C#在应对安全挑战的同时,不断拓展其在该领域的潜力和未来前景。
|
7天前
|
程序员 C#
C#编程中的面向对象编程思想
【4月更文挑战第21天】本文探讨了C#中的面向对象编程,包括类、对象、封装、继承和多态。类是对象的抽象,定义属性和行为;对象是类的实例。封装隐藏内部细节,只暴露必要接口。继承允许类复用和扩展属性与行为,而多态使不同类的对象能通过相同接口调用方法。C#通过访问修饰符实现封装,使用虚方法和抽象方法实现多态。理解并应用这些概念,能提升代码的清晰度和可扩展性,助你成为更好的C#程序员。
|
8天前
|
IDE 程序员 C#
C#编程入门:从零开始的旅程
【4月更文挑战第20天】本文引导初学者入门C#编程,从环境搭建开始,推荐使用Visual Studio Community版作为IDE。接着,通过编写&quot;Hello, World!&quot;程序,介绍基本语法,包括数据类型、运算符和表达式。文章还涉及控制结构、函数和方法,以及面向对象编程概念。通过学习,读者将对C#有初步了解,并激发进一步探索编程世界的兴趣。
|
8天前
|
开发框架 .NET Java
探索 C#编程的奥秘与魅力
【4月更文挑战第20天】C#是微软开发的现代、面向对象的编程语言,以其简洁语法、强大功能和跨平台支持脱颖而出。它支持自动垃圾回收、泛型、委托、LINQ,并广泛应用于桌面、Web、移动和游戏开发。C#拥有活跃的开发者社区和丰富的资源,是Unity游戏开发的首选语言。随着.NET Core,C#可在多个操作系统上运行,持续创新,未来发展潜力巨大。
|
8天前
|
缓存 算法 测试技术
优化 C#编程性能的策略
【4月更文挑战第20天】优化C#性能策略包括:选择合适算法和数据结构,避免频繁对象创建,缓存常用数据,减少内存分配,使用异步编程,优化数据库操作(如合理查询和使用索引),利用多线程并行处理,精简代码,使用性能分析工具,硬件升级,以及进行性能测试。综合应用这些策略可提升程序性能和响应性。
|
1月前
|
C# Windows
49.c#:StatusStrip 控件
49.c#:StatusStrip 控件
26 1
49.c#:StatusStrip 控件