【发布时间】:2016-10-19 09:45:45
【问题描述】:
我有以下代码
namespace WebApplication3
{
public partial class WebForm2 : System.Web.UI.Page
{
class Customer
{
public Customer(int id, string fName)
{
ID = id;
FirstName = fName;
}
public int ID { get; set; }
public string FirstName { get; set; }
}
private List<Customer> GetCustomerNames()
{
// Add Customers to list - this is essentially coming from a database
List<Customer> Names = new List<Customer>();
Names.Add(new Customer(1, "Name 1"));
Names.Add(new Customer(2, "Name 2"));
Names.Add(new Customer(3, "Name 3"));
Names.Add(new Customer(4, "Name 4"));
return Names;
}
private void DropdownNames()
{
// Set the dropdown
ddlName.DataSource = GetCustomerNames();
ddlName.DataTextField = "FirstName";
ddlName.DataValueField = "ID";
ddlName.DataBind();
}
private void SetNameHTML(List<Customer> Names)
{
// The below code would set the literal control with the values coming from the list along with custom span so the design would be correct
Int32 Count = 0;
lit1.Text = string.Empty;
foreach (Customer c in Names)
{
if (Count == 0)
{
// IF first element then set active class so correct style is applied
lit1.Text += string.Format("<span class='square active' ID='spName' runat='server' data-value='{0}'>{1}</span> <br />", c.FirstName, c.ID);
spName.InnerText = c.FirstName;
}
else
{
// Other non selected items - Note no active listed in the class
lit1.Text += string.Format("<span class='square' ID='spName' runat='server' data-value='{0}'>{1}</span> <br />", c.FirstName, c.ID);
}
Count += 1;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DropdownNames();
SetNameHTML(GetCustomerNames());
}
}
protected void ddlName_SelectedIndexChanged(object sender, EventArgs e)
{
// Get customer names again as the list would be empty if the SetNameHTML is removed
var CustomerNames = GetCustomerNames();
SetNameHTML(CustomerNames);
// Get a single customer to set the correct class (square active)
var CustName = CustomerNames.Where(cid => cid.ID == Convert.ToInt32(ddlName.SelectedValue)).FirstOrDefault();
spName.InnerText = ddlName.SelectedItem.Text;
// += on lit1.Text so that the span list is not empty.
// If i remove the lin SetNameHTML(CustomerNames); above then the list is empty
lit1.Text = string.Format("<span class='square active' ID='spName' runat='server' data-value='{0}'>{1}</span> <br />", CustName.FirstName, CustName.ID);
}
}
}
ASPX 代码
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<span class="info" runat="server" id="spName"></span>
<asp:DropDownList ID="ddlName" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlName_SelectedIndexChanged"></asp:DropDownList>
<br />
<div class="squares">
<asp:Literal ID="lit1" runat="server"></asp:Literal>
</div>
</div>
</form>
</body>
</html>
我正在使用文字控件,因此我可以根据找到的名称数量添加相关跨度。请注意,我删除了大部分 CSS JS,这就是为什么它看起来很普通。
当我从下拉列表中选择一个项目时,它会再次添加该项目(如果你运行上面的代码,你就会明白我的意思)
注意我的 cmets 以及为什么我做了我所拥有的。
这是页面加载时的视图源,然后我选择了一个项目
加载
<div class="squares">
<span class='square active' ID='spName' runat='server' data-value='Name 1'>1</span> <br />
<span class='square' ID='spName' runat='server' data-value='Name 2'>2</span> <br />
<span class='square' ID='spName' runat='server' data-value='Name 3'>3</span> <br />
<span class='square' ID='spName' runat='server' data-value='Name 4'>4</span> <br />
</div>
选择的第一个项目
<div class="squares">
<span class='square active' ID='spName' runat='server' data-value='Name 1'>1</span> <br />
<span class='square' ID='spName' runat='server' data-value='Name 2'>2</span> <br />
<span class='square' ID='spName' runat='server' data-value='Name 3'>3</span> <br />
<span class='square' ID='spName' runat='server' data-value='Name 4'>4</span> <br />
<span class='square active' ID='spName' runat='server' data-value='Name 2'>2</span> <br />
</div>
我知道为什么会这样,但我不知道该怎么做。我想要做的不是再次将所选项目添加到列表中,但由于我使用文字控件,我不知道如何删除它,或者是否有更好的方法来解决这个问题?
【问题讨论】:
-
您可以尝试将跨度添加为实际控件,而不仅仅是在您的代码中添加一个 html 字符串,您可能会有更好的运气。像这样:
var span = new System.Web.UI.HtmlControls.HtmlGenericControl("span");然后添加您的属性:span.Attributes.Add("data-value", c.ID);然后只需将新的跨度控件添加到文字:lit1.Controls.Add(span); -
将文字改为占位符
-
正如@pho3nix 所说,您可能应该使用占位符控件。
<asp:PlaceHolder />呈现为<div />,因此您应该将“正方形”div 替换为占位符,然后将跨度添加到其中。像这样:<asp:PlaceHolder ID="phSquares" runat="server" CssClass="squares"> -
@zgood - 正如你所看到的,我已经发布了一个建议 PlaceHolder 的答案(事实上,在 pho3nix 的评论之前)。顺便说一句,PlaceHolder 不会呈现为 div。其内容在页面中呈现,无需额外的容器 (stackoverflow.com/questions/2743404/…)。
-
@ConnorsFan 是的,你是对的......我在想
<asp:Panel />。对不起,我没有看到你回答。在我上面的评论中,只需将“PlaceHolder”替换为“Panel”就可以了。