Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 面板/数据列表-选中复选框时发送记录ID_C#_Asp.net - Fatal编程技术网

C# 面板/数据列表-选中复选框时发送记录ID

C# 面板/数据列表-选中复选框时发送记录ID,c#,asp.net,C#,Asp.net,我有一个包含DataList的面板,其中列出了带有复选框的公司。我想做的是,当用户单击特定公司的复选框时,它将发送CompanyID,这样我就可以对数据库进行一些更新。我不知道如何将值存储在复选框中,并在用户单击时发送它 这是数据表 <asp:Panel ID="pnlChildView" runat="server" style="padding-left:200px;"> <asp:DataList ID="childList" runat="server" Width="

我有一个包含DataList的面板,其中列出了带有复选框的公司。我想做的是,当用户单击特定公司的复选框时,它将发送CompanyID,这样我就可以对数据库进行一些更新。我不知道如何将值存储在复选框中,并在用户单击时发送它

这是数据表

<asp:Panel ID="pnlChildView" runat="server" style="padding-left:200px;">
<asp:DataList ID="childList" runat="server" Width="100%">
   <ItemTemplate>
       <div  class="div_hover">
        <table class="table1" width="80%">
       <tr>
          <td style="width: 60%; border-right:0px solid black; border-spacing:0;">&#8226; <%#Eval("CompanyName")%></td>
           <td style="width: 20%;text-align:right; "><a href="/Apps/ERP/Other/CompanyInfo.asp?CompanyID=<%#Eval("CompanyID")%>" ><%#Eval("CompanyID")%></a></td> 
           <td style="width: 20%;text-align:right;"><asp:CheckBox id="chkChildCompany" runat="server"  
                AutoPostBack="true"
               OnCheckedChanged="chkChildCompany_CheckedChanged"  /></td>                     
       </tr>
        </table>
        </div>
   </ItemTemplate>
</asp:DataList>
</asp:Panel>
知道如何隐藏公司ID并在复选框单击事件时捕获它吗


谢谢,

将此代码用于aspx文件

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Temp._Default" %>

<!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">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:TextBox runat="server" ID="compID" Text="" Style="display: none;" />
    <!-- HIDDEN Field for saving data in case posting form -->
    <asp:Panel ID="pnlChildView" runat="server" Style="padding-left: 200px;">
        <asp:DataList ID="childList" runat="server" Width="100%">
            <ItemTemplate>
                <div class="div_hover">
                    <table class="table1" width="80%">
                        <tr>
                            <td style="width: 60%; border-right: 0px solid black; border-spacing: 0;">
                                &#8226;<%#Eval("CompanyName")%>
                            </td>
                            <td style="width: 20%; text-align: right;">
                                <a href="/Apps/ERP/Other/CompanyInfo.asp?CompanyID=<%#Eval("CompanyID")%>">
                                    <%#Eval("CompanyID")%></a>
                            </td>
                            <td style="width: 20%; text-align: right;">
                                <input type="checkbox" class="chkChildCompany" value="<%#Eval("CompanyID")%>" />
                                <!-- Binding Company id to value -->
                                <%--<asp:CheckBox ID="chkChildCompany" runat="server" AutoPostBack="true" OnCheckedChanged="chkChildCompany_CheckedChanged" />--%>
                            </td>
                        </tr>
                    </table>
                </div>
            </ItemTemplate>
        </asp:DataList>
    </asp:Panel>
    </form>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $('.chkChildCompany').click(function () {
                var idarray = new Array();
                 //idarray used to store all checked id
                $("input[class='chkChildCompany']:checked").each(function () {
                  //This is a kind of for loop iterating to all check box 
                  //which have class name chkChildCompany
                  //and also check box is checked
                    idarray.push($(this).val());
                  //Each particular value that bind via DataGrid
                  //is add to id array
                });
                var listofid= idarray;
               //here id array is set to a new field to use as a string
                $('input#compID').val(listofid);
                console.log('listofid:- ' + listofid+ ' input#compID:- ' + $('input#compID').val());
                $.ajax({
                    url: "Default.aspx/saveComanyId", type: "POST", cache: "false",
                    dataType: "json", contentType: "application/json;charset=utf-8",
                    data: "{'idList':'" + airlinepref + "'}",
                   //here data must contains the var(idList) name defined in WebMethod
                   //saveComanyId(string idList)
                    success: function (data) {
                        console.log(data.d);
                    }, error: function () {
                    }
                });

            });
        });
    </script>
</body>
</html>

$(函数(){
$('.chkChildCompany')。单击(函数(){
var idarray=新数组();
//用于存储所有已检查id的idarray
$(“输入[class='chkChildCompany']:选中”)。每个(函数(){
//这是一种for循环迭代到all复选框
//类名为chkChildCompany的
//并且复选框也被选中
推送($(this.val());
//通过DataGrid绑定的每个特定值
//是否添加到id数组
});
var listofid=idarray;
//此处,id数组被设置为用作字符串的新字段
$('input#compID').val(listofid);
log('listofid:-'+listofid+'input#compID:-'+$('input#compID').val());
$.ajax({
url:“Default.aspx/saveComanyId”,类型:“POST”,缓存:“false”,
数据类型:“json”,内容类型:“application/json;charset=utf-8”,
数据:“{'idList':'”+airlinepref+“}”,
//此处数据必须包含WebMethod中定义的var(idList)名称
//saveComanyId(字符串idList)
成功:功能(数据){
console.log(data.d);
},错误:函数(){
}
});
});
});
这一个用于代码隐藏,即aspx.cs文件

namespace Temp
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            List<Company> companyList = new List<Company>{
                new Company{CompanyID=1,CompanyName="AAAA"},
                new Company{CompanyID=2,CompanyName="BBBB"},
                new Company{CompanyID=3,CompanyName="CCCC"},
                new Company{CompanyID=4,CompanyName="DDDD"},
            };
            childList.DataSource = companyList;
            childList.DataBind();
        }
        [System.Web.Services.WebMethod]
        public static string saveComanyId(string idList)
        {
            string response = "false";
            try
            {
                var ids = idList.Split(',');
                foreach (var id in ids)
                {
                    //Code for saving id in DATABASE
                }
                response ="true";
            }
            catch (Exception)
            {
                response="false";
            }
            return response;
            //returing response true for success and false for failure
        }
        /*protected void chkChildCompany_CheckedChanged(object sender, EventArgs e)
        {
            Response.Write("Child Checkbox is checked, CompanyID: ");

        }*/
    }
    public class Company
    {
        public string CompanyName { get; set; }
        public int CompanyID { get; set; }
    }
}
namespace-Temp
{
公共部分类\u默认值:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
列表公司列表=新列表{
新公司{CompanyID=1,CompanyName=“AAAA”},
新公司{CompanyID=2,CompanyName=“BBBB”},
新公司{CompanyID=3,CompanyName=“CCCC”},
新公司{CompanyID=4,CompanyName=“DDDD”},
};
childList.DataSource=公司列表;
childList.DataBind();
}
[System.Web.Services.WebMethod]
公共静态字符串saveComanyId(字符串idList)
{
字符串响应=“false”;
尝试
{
var id=idList.Split(',');
foreach(id中的变量id)
{
//在数据库中保存id的代码
}
response=“true”;
}
捕获(例外)
{
response=“false”;
}
返回响应;
//重复回答成功为true,失败为false
}
/*受保护的无效chkChildCompany\u CheckedChanged(对象发送方,事件参数e)
{
写(“选中子复选框,公司ID:”);
}*/
}
公营公司
{
公共字符串CompanyName{get;set;}
public int CompanyID{get;set;}
}
}

哇,看起来很复杂,但我会按照你的建议来做。感谢you@milacay这是jQuery和c#的组合。如果您使用复选框更改事件,它将回发不适合网页的页面,因此更喜欢jQuery,如果您想在复选框更改事件中使用,我也会提供一个,这需要很多时间来帮助。如果您不介意的话,请同时向我提供复选框更改事件方法。我是新来的,正在努力学习asp.net。我在上面运行的“airlinepref”错误是什么?请告知。
namespace Temp
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            List<Company> companyList = new List<Company>{
                new Company{CompanyID=1,CompanyName="AAAA"},
                new Company{CompanyID=2,CompanyName="BBBB"},
                new Company{CompanyID=3,CompanyName="CCCC"},
                new Company{CompanyID=4,CompanyName="DDDD"},
            };
            childList.DataSource = companyList;
            childList.DataBind();
        }
        [System.Web.Services.WebMethod]
        public static string saveComanyId(string idList)
        {
            string response = "false";
            try
            {
                var ids = idList.Split(',');
                foreach (var id in ids)
                {
                    //Code for saving id in DATABASE
                }
                response ="true";
            }
            catch (Exception)
            {
                response="false";
            }
            return response;
            //returing response true for success and false for failure
        }
        /*protected void chkChildCompany_CheckedChanged(object sender, EventArgs e)
        {
            Response.Write("Child Checkbox is checked, CompanyID: ");

        }*/
    }
    public class Company
    {
        public string CompanyName { get; set; }
        public int CompanyID { get; set; }
    }
}