“删除行”按钮不会在动态表 Asp.net 上触发
本文关键字:删除行 net Asp 按钮 动态 | 更新日期: 2023-09-27 18:36:15
我创建了一个表,您可以在其中添加用户,并且每次在动态表中都会生成一个新行。每一行都应该有一个带有减号的按钮,允许您删除一行。由于某种原因,我无法通过"onclick"或事件处理程序单击来触发我的按钮。"减法按钮"按钮应该在服务器端调用"减法用户"方法。当我单击任何行上的减法按钮时,它只是由于回发而消除了整个表。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) { Cache["CurrentUsersTable"] = new Table(); }
if (IsPostBack)
{
}
}
protected void click_NewUser(object sender, EventArgs e)
{
}
protected void click_NewCompany(object sender, EventArgs e)
{
}
protected void click_ImportCSV(object sender, EventArgs e)
{
}
protected void click_AddCompanyUser(object sender, EventArgs e)
{
addUserToTable(sender, e);
}
protected void click_AddIndividualUser(object sender, EventArgs e)
{
addUserToTable(sender, e);
}
protected void subtractUser(object sender, EventArgs e)
{
string deletionName = "";
List<HtmlGenericControl> userrows = new List<HtmlGenericControl>();
if (Cache["CurrentUserRowList"] != null) { userrows = (List<HtmlGenericControl>)Cache["CurrentUserRowList"]; }
userrows.Find(x => x.InnerText == deletionName);
}
protected void addUserToTable(object sender, EventArgs e)
{
Table currentuserstable = (Table)Cache["CurrentUsersTable"];
//For each new user added, create a row
TableRow trow = new TableRow();
TableCell td2 = new TableCell();
td2.Attributes.Add("style", "width: 100px");
td2.Text = "First Last";
TableCell td3 = new TableCell();
td3.Attributes.Add("style", "width: 331px");
RadioButtonList group = new RadioButtonList();
group.Attributes.Add("ID", "GroupRadio");
group.Attributes.Add("runat", "server");
group.RepeatDirection = RepeatDirection.Horizontal;
group.Items.Add(new ListItem("Core", "Core"));
group.Items.Add(new ListItem("Extended", "Extended"));
group.Items.Add(new ListItem("Advisory", "Advisory"));
group.Items.Add(new ListItem("None", "None"));
td3.Controls.Add(group);
TableCell td4 = new TableCell();
CheckBoxList role = new CheckBoxList();
role.Attributes.Add("ID", "roleRadio");
role.Attributes.Add("runat", "server");
role.RepeatDirection = RepeatDirection.Horizontal;
role.Items.Add(new ListItem("Participant", "Participant"));
role.Items.Add(new ListItem("Admin", "Admin"));
role.Items.Add(new ListItem("Reviewer", "Reviewer"));
td4.Attributes.Add("style", "width: 331px");
td4.Controls.Add(role);
TableCell td5 = new TableCell();
RadioButtonList seniority = new RadioButtonList();
seniority.Attributes.Add("ID", "seniorityRadio");
seniority.Attributes.Add("runat", "server");
seniority.RepeatDirection = RepeatDirection.Horizontal;
seniority.Items.Add(new ListItem("Core", "Core"));
seniority.Items.Add(new ListItem("Extended", "Extended"));
seniority.Items.Add(new ListItem("Advisory", "Advisory"));
seniority.Items.Add(new ListItem("None", "None"));
td5.Controls.Add(seniority);
TableCell td = new TableCell();
td.Attributes.Add("style", "width: 47px");
Button subtractbutton = new Button();
subtractbutton.Text = "-";
subtractbutton.Attributes.Add("style", "btn");
subtractbutton.Attributes.Add("runat", "server");
subtractbutton.Click += new System.EventHandler(subtractUser);
td.Controls.Add(subtractbutton);
trow.Controls.Add(td);
trow.Controls.Add(td2);
trow.Controls.Add(td3);
trow.Controls.Add(td4);
trow.Controls.Add(td5);
//Add row to Cache list
trow.ID = td2.Text + "Row";
currentuserstable.Rows.Add(trow);
Cache["CurrentUsersTable"] = currentuserstable;
update_CurrentUsers.ContentTemplateContainer.Controls.Add(currentuserstable);
}
}
<div class="row">
<div class="col-md-12">
<h1>New Project</h1>
</div>
</div>
<div class="row">
<div class="col-md-12">
<h4>Project Name</h4>
</div>
<div class="col-md-3">
<input runat="server" id="edit_ProjectName" class="form-control">
</div>
</div>
<div class="row">
<div class="col-md-12">
<h4>Project Companies</h4>
</div>
<div class="col-md-3">
<input class="form-control" runat="server" id="edit_CompanyName" placeholder="Company Name">
</div>
<div class="col-md-12"><button type="button" runat="server" id="btn_NewCompany" onclick="click_NewCompany" class="btn btn-default" style="width: 252px">+ New Company </button></div>
</div>
<div class="row">
<div class="col-md-12">
<h3>Users</h3>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="col-md-3">
<h4>Add Company Users</h4>
</div>
<div class="col-md-3">
<h4>Add Individual Users</h4>
</div>
<div class="col-md-3"></div>
</div>
</div>
<asp:Panel Cssclass="row" runat="server">
<asp:Panel Cssclass="col-md-12" runat="server">
<div class="col-md-3" runat="server">
<div class="row" runat="server">
<asp:UpdatePanel runat="server"><ContentTemplate>
<asp:button type="submit" runat="server" onclick="click_AddCompanyUser" id="btn_AddCompanyUsers" Text="+" class="btn"/>
<input runat="server" id="edit_AddCompanyUsers" class="form-control-loose"> </ContentTemplate></asp:UpdatePanel>
</div>
</div>
<asp:UpdatePanel runat="server"><ContentTemplate>
<asp:panel runat="server" Cssclass="col-md-3">
<asp:button type="submit" runat="server" onclick="click_AddIndividualUser" UseSubmitBehavior="false" id="btn_AddIndividualUsers" Text="+" class="btn"/>
<input runat="server" id="edit_AddIndividualUsers" class="form-control-loose">
<asp:button runat="server" Text="+New User" class="btn btn-default" style="width: 244px" OnClick="click_NewUser"></asp:button>
</asp:panel></ContentTemplate></asp:UpdatePanel>
<div class="col-md-3"> <button type="button" onclick="click_ImportCSV" class="btn btn-default">Import from CSV</button></div>
</asp:Panel>
</asp:Panel>
<div class="row">
<div class="col-md-12">
<h3>Current Users</h3>
</div>
</div>
<div class="row">
<div class="panel-heading" data-brix_class="1454831583299">
<div class="col-sm-1">
<h4> User</h4>
</div>
<div class="col-md-4">
<h4> Group</h4>
</div>
<div class="col-md-3">
<h4> Role(s)</h4>
</div>
<div class="col-md-3">
<h4> Seniority</h4>
</div>
</div>
</div>
<div class="row">
<div class="col-md-14">
<div class="panel"><!-- Default panel contents --><!-- Table -->
<asp:UpdatePanel ID="update_CurrentUsers" runat="server"><ContentTemplate>
</ContentTemplate></asp:UpdatePanel>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<h3>Additional Attributes</h3>
</div>
<div class="col-md-12">To be Determined</div>
</div>
<br />
<div class="row">
<div class="col-md-12"><button type="button" class="btn btn-default">Save & Continue</button><asp:linkbutton runat="server" PostBackUrl="~/Admin/Projects.aspx" type="button" Text="Cancel" class="btn btn-link"/></div>
</div>
由于这是一个动态表,因此每次有回发时,您都需要将其从缓存添加回容器中。
if (IsPostBack)
{
Cache["CurrentUsersTable"] = currentuserstable;
update_CurrentUsers.ContentTemplateContainer.Controls.Add(currentuserstable);
}