如何从代码后面访问父转发器ID

时间:2015-01-30 11:11:35

标签: c# asp.net repeater asprepeater

我的页面上有一个嵌套的转发器,如:

enter image description here

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>

代码背后:

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

在我的孩子中继器中,我有一个链接按钮,我需要做一些计算,我需要知道哪些问题答案被点击。即在我的LinkButton1_Command中我想与QuestionId一起获取AnsId。

如何在按钮点击事件中获得父转发器ID?

1 个答案:

答案 0 :(得分:0)

试试这个,

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

如果这不起作用,请尝试

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

如果您使用ItemDataBound方法进行代码隐藏:

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