English 中文(简体)
在小组所加的隐蔽领域周围建立html的最佳方式是什么?
原标题:What is the best way to create html around hidden fields added to a panel?
  • 时间:2012-05-07 13:43:11
  •  标签:
  • c#
  • .net

我将这些隐蔽的输入小组:

foreach (Object obj in Objects)
{
    HiddenField hf = new HiddenField();
    hf.Value = "";
    hf.ID = "categoria" + obj.ID;
    panelCategorieGuida.Controls.Add(hf);
}

但是,在每一个隐蔽的领域,我需要补充这一法典:

<div class="option-box"><a href="javascript:void(0);" class="option-box-item">&nbsp;</a>
    <span class="option-box-testo">Hello</span>
    <!-- HERE I NEED TO PUT THE HIDDEN FIELD i, so think to this whole option-box item into a cycle -->
</div>

我愿避免在专题上写<代码>Literal,并将其添加到小组。

你可以向我建议什么?

最佳回答

您可以使用雷娜特控制,标志:

<asp:Repeater ID="catRep" runat="server" onitemcreated="catRep_ItemCreated">
  <ItemTemplate>
    <div class="option-box"><a href="javascript:void(0);" class="option-box-item">&nbsp;</a>
      <span class="option-box-testo">Hello</span>
      <asp:PlaceHolder ID="hiddenPlaceHolder" runat="server"></asp:PlaceHolder>          
    </div>
  </ItemTemplate>
</asp:Repeater>

后面的编码:

protected void catRep_ItemCreated(object sender, RepeaterItemEventArgs e)
{
  Control placeHolder = e.Item.FindControl("hiddenPlaceHolder");
  if (placeHolder != null)
  {
    MyItemClass my = (MyItemClass)e.Item.DataItem;
    HiddenField hf = new HiddenField();
    hf.Value = "";
    hf.ID = "categoria" + my.ID;
    placeHolder.Controls.Add(hf);
  }
}

当然,你必须对物体清单重复使用。

问题回答

你们也可以使用HtmlGenericControl。

HtmlGenericControl div = new HtmlGenericControl("div");
div.Attributes.Add("class", "option-box");

HtmlGenericControl a = new HtmlGenericControl("a") { InnerText = "&nbsp;" };
a.Attributes.Add("href", "javascript:void(0);");
a.Attributes.Add("class", "option-box-item");

HtmlGenericControl span = new HtmlGenericControl("span") { InnerText = "Hello" };
a.Attributes.Add("class", "option-box-testo");

div.Controls.Add(a);
div.Controls.Add(span);
div.Controls.Add(hf);

如果你需要这种东西,那么你或许应当研究把它变成一个,这样你就可以简单地说明你需要什么,然后再使用,而不必担心需要周围什么。 届时,你可以把隐蔽的领域确定为你所需要的一切领域,并在方案上稍后加以利用。

或者,更糟的是,是否宜考虑Microsoft MVC,并作此等工作,作为可替代的观点。

Edit based on comment: You re not terribly clear on precisely how this needs to be used, so this is a rough go at things where I m making a lot of assumptions on your usage of the hidden field.

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="test.ascx.cs" Inherits="stuff.test" %>
<div class="option-box"><a href="javascript:void(0);" class="option-box-item">&nbsp;</a>
    <span class="option-box-testo">Hello</span>
    <asp:label id="label1" runat="server" visible="false"/>
</div>

其后,在法典中,下列数字有所下降:

    public Label HiddenField
    {
        get { return (Label)FindControl("label1"); }
    }

从那儿,你可以加上许多这样的控制,即请你在你使用时使用类似的“FindControl”职业(如果你需要多种职业的话,请见,以了解如何这样做。 在你对用户进行控制后,你才必须进行用户调查。 隐蔽的现场。 查阅标签的案文。

最大的假设是,这些隐蔽的田地都必须完全孤立地处理——这就是你的印象。 各位都给我说了,因为你似乎彼此处理,但彼此独立。 如果所有这些事情都必须以彼此的价值观为基础处理,那么你很可能更能采纳“雷阿”建议。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签