我有一个包含DataList的面板,它列出了带有复选框的公司。我想要做的是当用户单击特定公司的复选框时,它将发送CompanyID,因此我可以对数据库进行一些更新。我不确定如何将值存储在复选框中,并在用户点击时发送。
这是DataList
<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;">• <%#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>
以下是活动:
protected void chkChildCompany_CheckedChanged(object sender, EventArgs e)
{
Response.Write("Child Checkbox is checked, CompanyID: " + CompanyID (sending from the checkbox event);
}
任何想法如何隐藏CompanyID并在复选框点击事件时捕获它?
谢谢,
答案 0 :(得分:0)
将此代码用于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;">
•<%#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>
这个代码隐藏在后面,即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; }
}
}