如何解决如何在asp.net中的ascx中设置表格单元格数据?
//ascs.cs page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace Assignment_2
{
public partial class Structure : System.Web.UI.UserControl
{
public string tit1
{
get { }
set { }
}
protected void Page_Load(object sender,EventArgs e)
{
}
}
}
//ascs.cs page
// ascx page
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Structure.ascx.cs" Inherits="Assignment_2.Structure" %>
<style type="text/css">
.auto-style1 {
width: 273px;
}
.auto-style2 {
width: 879px;
}
.auto-style3 {
width: 273px;
height: 31px;
}
.auto-style4 {
width: 879px;
height: 31px;
}
.auto-style5 {
height: 31px;
width: 55px;
}
.auto-style6 {
width: 55px;
}
.auto-style7 {
width: 100%;
}
</style>
<table border="1" style="background-color: #FFFFFF;" class="auto-style7" id="tb1" runat="server">
<tr>
<td class="auto-style3" style="background-color: #003399; font-weight: bold; color: #FFFFFF;"> Book Title</td>
<td class="auto-style4" style="background-color: #003399; font-weight: bold; color: #FFFFFF;"> Book Description</td>
<td class="auto-style5" style="background-color: #003399; font-weight: bold; color: #FFFFFF;"> Add to<br />
cart </td>
</tr>
<tr>
<td class="auto-style1" style="background-color: #C0C0C0" id="cl1" runat="server" > </td>
<td class="auto-style2" style="background-color: #C0C0C0" id="cl1.1" runat="server"> </td>
<td class="auto-style6" style="background-color: #C0C0C0">
<input id="Checkbox1" type="checkbox" runat="server"/></td>
</tr>
<tr>
<td class="auto-style1" style="background-color: #999999" id="cl2" runat="server"> </td>
<td class="auto-style2" style="background-color: #999999" id="cl2.1" runat="server"> </td>
<td class="auto-style6" style="background-color: #999999">
<input id="Checkbox2" type="checkbox" runat="server"/></td>
</tr>
<tr>
<td class="auto-style1" style="background-color: #C0C0C0" id="cl3" runat="server"> </td>
<td class="auto-style2" style="background-color: #C0C0C0" id="cl3.1" runat="server"> </td>
<td class="auto-style6" style="background-color: #C0C0C0">
<input id="Checkbox3" type="checkbox" runat="server"/></td>
</tr>
<tr>
<td class="auto-style1" style="background-color: #999999" id="cl4" runat="server"> </td>
<td class="auto-style2" style="background-color: #999999" id="cl4.1" runat="server"> </td>
<td class="auto-style6" style="background-color: #999999">
<input id="Checkbox4" type="checkbox" runat="server"/></td>
</tr>
<tr>
<td class="auto-style1" style="background-color: #C0C0C0" id="cl5" runat="server"> </td>
<td class="auto-style2" style="background-color: #C0C0C0" id="cl5.1" runat="server"> </td>
<td class="auto-style6" style="background-color: #C0C0C0">
<input id="Checkbox5" type="checkbox" runat="server"/></td>
</tr>
</table>
// ascx page
如果我通过id调用它显示错误,该如何在ascs.cs页中获取和设置单元格值。任何人都可以建议我如何实现它。我想获取并设置usercontrol中每个单元格的值,以便在aspx页面中可以操作laod方法上的aspx页面中的数据。请帮忙。
解决方法
如果要访问ascx文件中的表数据,则可以使用javascript或jquery获取和设置值。 var tb1 = document.getElementById('tb1');
第一行第一列获取值: var myValue = tb1.rows [0] .columns [0] .innerHTML;
设置值第一行,第一列: tb1.rows [0] .columns [0] .innerHTML ='newvalue';
,在aspx.cs Page_load
函数中尝试以下代码以将数据填充到ascx中的表中:
protected void Page_Load(object sender,EventArgs e)
{
if (!IsPostBack)
{
HtmlTable table = Structure.FindControl("tb1") as HtmlTable;
for (int i = 1; i < table.Rows.Count; i++)
{
((HtmlTableCell)table.Rows[i].FindControl($"cl{i}")).InnerHtml += $"BookTitle{i}";
((HtmlTableCell)table.Rows[i].FindControl($"Td{i}")).InnerHtml += $"BookDescription{i}";
((HtmlInputCheckBox)table.Rows[i].FindControl($"Checkbox{i}")).Checked = true;
}
}
}
aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication_webform.Pages.WebForm1" %>
<%@ Register Src="~/Structure.ascx" TagPrefix="uc1" TagName="Structure" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body>
<uc1:Structure runat="server" ID="Structure" />
</body>
</html>
我正在使用的是动态填充每个单元格的方法,如果要将硬编码数据添加到每个单元格,则需要将循环更改为要分配的无效数据。
这是我的代码的结果:
要在ascx.cs中获取数据,可以使用以下代码:
public partial class Structure : System.Web.UI.UserControl
{
protected void Page_Load(object sender,EventArgs e)
{
for (int i = 1; i < tb1.Rows.Count; i++)
{
var cl = ((HtmlTableCell)tb1.Rows[i].FindControl($"cl{i}")).InnerText;
var td = ((HtmlTableCell)tb1.Rows[i].FindControl($"Td{i}")).InnerText;
var cb = ((HtmlInputCheckBox)tb1.Rows[i].FindControl($"Checkbox{i}")).Checked;
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。