gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:58:24 24 4
gpt4 key购买 nike

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

代码文件

    Dim repeatTimes((TotalAdInsured - 1)) As Integer

myRepeater.DataSource = repeatTimes
myRepeater.DataBind()

ASP文件

<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>

返回此错误

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

最佳答案

I want to distinguish dropdown controls inside a repeater control.

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

标记:

<%@ 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);
}
}

关于ASP.NET 动态更改中继器项内的下拉控件 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/932186/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com