简体   繁体   English

如何从后面的代码访问父级中继器ID

[英]How to access Parent repeater id from code behind

I have a nested repeater on my page like: 我的页面上有一个嵌套的中继器,例如:

在此处输入图片说明

Aspx page: Aspx页面:

    <asp:Repeater ID="parentRepeater" runat="server">
        <ItemTemplate>
            <br />
            <b><%# DataBinder.Eval(Container.DataItem,"question") %></b><br>

            <!-- start child repeater -->
            <asp:Repeater ID="childRepeater" DataSource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("relation") %>'
                runat="server">

                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" OnCommand="LinkButton1_Command" CommandName="MyUpdate" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "[\"AnsId\"]")%>'><%# DataBinder.Eval(Container.DataItem, "[\"Ans\"]")%></asp:LinkButton>

                    <br>
                </ItemTemplate>
            </asp:Repeater>
            <!-- end child repeater -->

        </ItemTemplate>
    </asp:Repeater>

Code behind: 后面的代码:

SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["star_report_con"].ConnectionString);
SqlDataAdapter cmd1 = new SqlDataAdapter("select questionId, question from questions", cnn);

//Create and fill the DataSet.
DataSet ds = new DataSet();
cmd1.Fill(ds, "questions");

//Create a second DataAdapter for the Titles table.
SqlDataAdapter cmd2 = new SqlDataAdapter("SELECT AnsId, Ans, questionId FROM answers", cnn);
cmd2.Fill(ds, "answers");

//Create the relation bewtween the Authors and Titles tables.
ds.Relations.Add("relation",
ds.Tables["questions"].Columns["questionId"],
ds.Tables["answers"].Columns["questionId"]);

//Bind the Authors table to the parent Repeater control, and call DataBind.
parentRepeater.DataSource = ds.Tables["questions"];
Page.DataBind();

//Close the connection.
cnn.Close();

protected void LinkButton1_Command(object sender, CommandEventArgs e)
{
    if (e.CommandName == "MyUpdate")
    {

        //e.CommandArgument --> contain the Ansid value
        //I want to also find which questions ans is clicked i.e the questionId
    }
}

In my child repeater I have a linkbutton on click of which i need to do some computaion for which I need to know which questions answers wa being clicked. 在我的儿童直放站中,我有一个链接按钮,单击它时我需要做一些计算,我需要知道要单击哪个问题答案。 ie in my LinkButton1_Command I want to fetch the AnsId along with QuestionId. 即在我的LinkButton1_Command我想获取AnsId以及QuestionId。

How will I get parent repeaters Id in button click event? 如何在按钮单击事件中获取父转发器ID?

Try this , 尝试这个 ,

  <%# ((RepeaterItem)Container.Parent.Parent).DataItem %>

If this does not work then try 如果这不起作用,请尝试

  <%# DataBinder.Eval(Container.Parent.Parent, "DataItem.YourProperty")%> 

if you're in code-behind in the ItemDataBound method: 如果您位于ItemDataBound方法的代码隐藏中:

 ((Repeater)e.Item.NamingContainer.NamingContainer).DataItem 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM