Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用jquery标记和设置div id';在列表视图中动态显示_C#_Jquery_Asp.net_Listview_Html Injections - Fatal编程技术网

C# 如何使用jquery标记和设置div id';在列表视图中动态显示

C# 如何使用jquery标记和设置div id';在列表视图中动态显示,c#,jquery,asp.net,listview,html-injections,C#,Jquery,Asp.net,Listview,Html Injections,大家好,我想动态设置jquery id和listview div id 我在网上查了一下,没有找到任何好的例子 <script> $(function () { // I need to set the #accLstViewInner to #accLstViewInner + itemID $("#accLstViewInner").accordion({ collapsible: true,

大家好,我想动态设置jquery id和listview div id 我在网上查了一下,没有找到任何好的例子

    <script>
        $(function () {
    // I need to set the #accLstViewInner to #accLstViewInner + itemID
            $("#accLstViewInner").accordion({
                collapsible: true, active: false
            });
        });
    </script>

$(函数(){
//我需要将#accLstViewInner设置为#accLstViewInner+itemID
$(“accLstViewInner”).手风琴({
可折叠:真,活动:假
});
});
设定

     <asp:ListView ID="lstAccordionViewInner" Runat="server" >
        <LayoutTemplate>
            // set the div below to #accLstViewInner + itemID as well
            <div id="accLstViewInner">

//将下面的div也设置为#accLstViewInner+itemID
有什么好的资源或想法链接吗


谢谢

您提到的代码中没有动态的东西。通常的做法是使用两个ListView(或Repeater)生成代码,或者直接在选择器或js变量中输出对象的结果

我想如果你从代码隐藏处输入

lstAccordionViewInner.DataSource = someListObject;
lstAccordionViewInner.DataBind();
例如

class someList
{
    public int Id { get; set; }
    public string Name { get; set; }
}

// and an example instance, should be put in a property if you want to access it from your aspx-code
var someListObject = new List<someList>()
{
    new someList() {Id = 1, Name = "#accLstViewInner"},
    new someList() {Id = 2, Name = "#accLstViewInner"},
    new someList() {Id = 3, Name = "#accLstViewInner"},
    new someList() {Id = 4, Name = "#accLstViewInner"},
    new someList() {Id = 5, Name = "#accLstViewInner"}
};

string jqueryResult = someListObject.Aggregate(string.Empty, (current, item) => current + string.Concat(item.Name, "_", item.Id, item == someListObject.Last() ? string.Empty : ", "));
// #accLstViewInner_1, #accLstViewInner_2, #accLstViewInner_3, #accLstViewInner_4, #accLstViewInner_5
但是,既然你有这种模式,我想说,除非你想要极端的控制,否则做那么多工作只是为了构建一个选择器是愚蠢的。改用通配符

$("[id^=accLstViewInner]").accordion({
    collapsible: true, active: false
});
要处理listview

 <asp:ListView ID="lstAccordionViewInner" Runat="server" >
    <LayoutTemplate>
        // set the div below to #accLstViewInner + itemID as well
        <div id='accLstViewInner_<%#Eval("Id") %>' />'>
        // or possibly
        <div id='accLstViewInner_<%#DataBinder.Eval(Container.DataItem, "Id")%>' />'>
        // ...
        <div id='accLstViewInner_<% DataBinder.Eval(Container.DataItem("Id"))%>' />'>

//将下面的div也设置为#accLstViewInner+itemID
'>
//或者可能
'>
// ...
'>

为什么不改为不使用id?@KevinB,因为user2817020想要生成类似于accLstViewInner\u 1、accLstViewInner\u 2、accLstViewInner的代码_3@EricHerlitz显然,但没有什么理由这么做。如果您需要枚举它,它甚至已经没有id了,每个id在DOM中都有一个索引。否则,你会得到像
$('accLstViewInner'u 1,'accLstViewInner'u 2,'accLstViewInner'u 3,'accLstViewInner'u 4,'accLstViewInner'u 5')
$([id^=accLstViewInner])
而不是
$(.acclstviewiner”)
@KevinB,总的来说,我同意你的看法,但有些情况下,你需要的是比课堂更大的控制权selection@Kevin我确实让它与嵌套类一起工作,但正如kevin对嵌套ListView所说的那样,我进入了一些场景,这些场景将证明添加的控件是正确的,因此我确信id是唯一的,并且可以调整的,包含了任何奇怪的css依赖项发生在不同模块的后端和后端开发之间,等等。感谢回复。我一定会尝试一下。Eric…实际上,我通过将id交换为类来实现这一点,但我会运行您的解决方案并让您知道。再次感谢
$("[id^=accLstViewInner]").accordion({
    collapsible: true, active: false
});
 <asp:ListView ID="lstAccordionViewInner" Runat="server" >
    <LayoutTemplate>
        // set the div below to #accLstViewInner + itemID as well
        <div id='accLstViewInner_<%#Eval("Id") %>' />'>
        // or possibly
        <div id='accLstViewInner_<%#DataBinder.Eval(Container.DataItem, "Id")%>' />'>
        // ...
        <div id='accLstViewInner_<% DataBinder.Eval(Container.DataItem("Id"))%>' />'>