ASP.NET 动态更改中继器项内的下拉控件 ID

发布于 2024-07-21 13:29:00 字数 1104 浏览 4 评论 0原文

有人可以告诉我如何让它发挥作用吗? 我想区分中继器控件内的下拉控件。 我现在了解了生命周期以及缓冲区是如何写入的,但是我的选择是什么? 这是发生的情况

代码文件

    Dim repeatTimes((TotalAdInsured - 1)) As Integer

    myRepeater.DataSource = repeatTimes
    myRepeater.DataBind()

Aspfile

<asp:Repeater ID="myRepeater" runat="server">
    <ItemTemplate>
         <asp:DropDownList ID="AdTitle<%# Container.ItemIndex %>" runat="server">
             <asp:ListItem Selected="True" Value="" Text=""/>
             <asp:ListItem Selected="False" Value="Miss" Text="Miss"/>
             <asp:ListItem Selected="False" Value="Ms" Text="Ms"/>
             <asp:ListItem Selected="False" Value="Mrs" Text="Mrs"/>
             <asp:ListItem Selected="False" Value="Mr" Text="Mr"/>
             <asp:ListItem Selected="False" Value="Other" Text="Other"/>
         </asp:DropDownList>
    </ItemTemplate>
</asp:Repeater>

返回此错误

解析器错误消息:“AdTitle<%# Container.ItemIndex %>” 不是有效的标识符。

Can someone tell me how I can get this to work. I want to distinguish dropdown controls inside a repeater control. I understand now about the lifecyle and how the buffer is already writen, but what are my alternatives? Here is what happens

Code File

    Dim repeatTimes((TotalAdInsured - 1)) As Integer

    myRepeater.DataSource = repeatTimes
    myRepeater.DataBind()

Aspfile

<asp:Repeater ID="myRepeater" runat="server">
    <ItemTemplate>
         <asp:DropDownList ID="AdTitle<%# Container.ItemIndex %>" runat="server">
             <asp:ListItem Selected="True" Value="" Text=""/>
             <asp:ListItem Selected="False" Value="Miss" Text="Miss"/>
             <asp:ListItem Selected="False" Value="Ms" Text="Ms"/>
             <asp:ListItem Selected="False" Value="Mrs" Text="Mrs"/>
             <asp:ListItem Selected="False" Value="Mr" Text="Mr"/>
             <asp:ListItem Selected="False" Value="Other" Text="Other"/>
         </asp:DropDownList>
    </ItemTemplate>
</asp:Repeater>

Returns this error

Parser Error Message: 'AdTitle<%# Container.ItemIndex %>' is not a valid identifier.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

沒落の蓅哖 2024-07-28 13:29:00

我想区分下拉菜单
中继器控件内的控件。

你不需要。 以下是一些可能对您有所帮助的示例代码:

标记:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label runat="server" ID="lblMsg" Text="Click a button" />
        <hr />
        <asp:Repeater runat="server" ID="rptAwesome" onitemcommand="rptAwesome_ItemCommand"
            >
            <ItemTemplate>
                <asp:Button runat="server" ID="btnAwesome"
                    Text='<%# "Button #" + Container.ItemIndex %>'
                    CommandArgument='<%# Container.DataItem %>'/><br />
            </ItemTemplate>
        </asp:Repeater>
    </div>
    </form>
</body>
</html>

代码隐藏:

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            var x = new int[] { 1, 2, 3, 4, 5 };
            rptAwesome.DataSource = x;
            rptAwesome.DataBind();
        }
    }
    protected void rptAwesome_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        Button btnAwesome = (Button)e.CommandSource;
        lblMsg.Text = string.Format("btnAwesome.ID = {0}, e.CommandArgument = {1}", btnAwesome.ID, e.CommandArgument);
    }
}

I want to distinguish dropdown
controls inside a repeater control.

You don't need to. Here's some sample code that might help you out:

Markup:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label runat="server" ID="lblMsg" Text="Click a button" />
        <hr />
        <asp:Repeater runat="server" ID="rptAwesome" onitemcommand="rptAwesome_ItemCommand"
            >
            <ItemTemplate>
                <asp:Button runat="server" ID="btnAwesome"
                    Text='<%# "Button #" + Container.ItemIndex %>'
                    CommandArgument='<%# Container.DataItem %>'/><br />
            </ItemTemplate>
        </asp:Repeater>
    </div>
    </form>
</body>
</html>

Codebehind:

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            var x = new int[] { 1, 2, 3, 4, 5 };
            rptAwesome.DataSource = x;
            rptAwesome.DataBind();
        }
    }
    protected void rptAwesome_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        Button btnAwesome = (Button)e.CommandSource;
        lblMsg.Text = string.Format("btnAwesome.ID = {0}, e.CommandArgument = {1}", btnAwesome.ID, e.CommandArgument);
    }
}
树深时见影 2024-07-28 13:29:00

您无法通过这样的标记在转发器内创建唯一 ID。 但是,您可以通过使用 FindControl 来检索代码隐藏中的下拉菜单复读机。

Dim adTitle As DropDownList = MyRepeater.FindControl("AdTitle")
If (Not adTitle Is Nothing) Then
  ''Do something here
End If

You cannot create a Unique ID inside a repeater through the markup like that. You can however retrieve the dropdown in the codebehind by using FindControl on the Repeater.

Dim adTitle As DropDownList = MyRepeater.FindControl("AdTitle")
If (Not adTitle Is Nothing) Then
  ''Do something here
End If
流星番茄 2024-07-28 13:29:00

好吧......简而言之,您无法以当前正在做的方式做您想做的事情。

控件的 ID 属性只能使用标记中的 ID 属性和简单值来设置。 示例:

我可以看到你想要做什么,但我不太明白为什么......

中继器控件数据源中的每个项目将包含 1 个项目,因此只需调用 DropDownList ID="AdTitle" 就可以了,因为这将是与下排的一个。

为了让它们返回服务器端,您只需迭代中继器和中继器中的项目即可。 FindControl("AdTitle") 获取该项目的特定 DropDownList。

如果绝对有必要让 ID 递增,则需要以编程方式执行此操作(可能在 ItemDataBound 事件上创建 DropDownList 并将其添加到 ItemTemplate 中。

Ok... in a nutshell you can't do what you're trying to do in the manner you are currently doing it.

The ID property of a control can only be set using the ID attribute in the tag and a simple value. Example: <asp:Button runat="server" id="Button1" />

I can see what your trying to do, but I don't really understand Why...

A repeater control will contain 1 item, per item in your datasource, so it's perfectly fine to just call your DropDownList ID="AdTitle" as that will be a different 'AdTitle' from the one in the next row.

To get them back server side, you would just iterate through the Items in your Repeater & FindControl("AdTitle") to get the specific DropDownList for that item.

If it's absolutely necessary to have the IDs incrementing, you'll need to do this programmatically (probably on the ItemDataBound event to create a DropDownList and add it to your ItemTemplate.

空心空情空意 2024-07-28 13:29:00

扩展 BC 的答案:我最近遇到了同样的问题,尽管有一个复选框,但我需要设置 ID,因为我在保留选择时使用了该 ID。 有点笨重,但我是在项目数据绑定时做到的。

这是标记:

 
  标题
>
>
<项目模板> > ...为简洁起见,删除了其他控件 <页脚模板>

我设置并绑定了此类的集合,

public class mst
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Number { get; set; }
    public bool NumberRequired { get; set; }
}

在 Page_Load 中执行必要的操作

        Repeater1.ItemDataBound += new RepeaterItemEventHandler(Repeater1_ItemDataBound);
        Repeater1.DataSource = listOfMsts;
        Repeater1.DataBind();

,然后在此处设置 ID:

 void Repeater1_ItemDataBound(对象发送者,RepeaterItemEventArgs e) 
      { 
          if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
          { 
              var mst = (mst)e.Item.DataItem; 
              ((CheckBox)e.Item.FindControl("cb")).ID = mst.Id.ToString(); 
          } 
      } 
  

因此,当点击提交按钮时,我可以循环并获取已检查的 ID 并保存:

 protected void Submit(对象发送者,EventArgs e) 
      { 
      foreach(Repeater1.Items 中的 var item) 
          { 
              var checkboxes = ((RepeaterItem)item).Controls.OfType().ToList(); 
              if (复选框[0].选中) 
              { 
                  保存(复选框[0].ID); 
                  ....其他的东西 
              } 
          } 
      } 
  

Expanding on BC's answer: I recently had the same issue, albeit with a checkbox, I needed to set the ID as I used that when persisting choices. A bit clunky but I did it when on Item Data Bound.

Here's the markup:

<asp:Repeater ID="Repeater1" runat="server" >
<HeaderTemplate>Header<br /><br /></HeaderTemplate>
<ItemTemplate>
    <asp:CheckBox ID="cb" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Name") %>' />
     ...other controls removed for brevity
</ItemTemplate>
<FooterTemplate>
    <asp:Button ID="Button1" runat="server" OnClick="Submit" Text="Submit" />
</FooterTemplate>
</asp:Repeater>

I set up and bound a collection of this class

public class mst
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Number { get; set; }
    public bool NumberRequired { get; set; }
}

Do the necessary in Page_Load

        Repeater1.ItemDataBound += new RepeaterItemEventHandler(Repeater1_ItemDataBound);
        Repeater1.DataSource = listOfMsts;
        Repeater1.DataBind();

Followed by, this where I set ID:

    void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            var mst = (mst)e.Item.DataItem;
            ((CheckBox)e.Item.FindControl("cb")).ID = mst.Id.ToString();
        }
    }

So when the submit button is hit I can loop thru and grab the Id of those checked and save:

  protected void Submit(object sender, EventArgs e)
    {
    foreach (var item in Repeater1.Items)
        {
            var checkboxes = ((RepeaterItem)item).Controls.OfType<CheckBox>().ToList();
            if (checkboxes[0].Checked)
            {
                Save(checkboxes[0].ID);
                ....other stuff
            }
        }
    }
柒夜笙歌凉 2024-07-28 13:29:00

您可以向 Repeater.ItemDataBound 添​​加事件处理程序,并使用对项容器的引用在处理程序内创建控件。

You could add an event handler to Repeater.ItemDataBound and create the control within the handler using the reference to the item container.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文