2

I have a nested repeater on my page like:

enter image description here

Aspx page:

    <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. i.e in my LinkButton1_Command I want to fetch the AnsId along with QuestionId.

How will I get parent repeaters Id in button click event?

Community
  • 1
  • 1
Arti
  • 2,993
  • 11
  • 68
  • 121

1 Answers1

1

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:

 ((Repeater)e.Item.NamingContainer.NamingContainer).DataItem 
Mahesh
  • 8,694
  • 2
  • 32
  • 53
  • You can set it as the` CommandArgument ` for the `LinkButton` and then access it. – Mahesh Jan 30 '15 at 11:23
  • but i also want to access ansId along with questionId. I have already set ` CommandArgument='<%# DataBinder.Eval(Container.DataItem, "[\"AnsId\"]")%>'>` – Arti Jan 30 '15 at 11:26
  • 1
    You can take a look at this answer http://stackoverflow.com/questions/6939009/how-to-send-multiple-command-arguments-through-command-button-rowcommand-event this approach will work – Mahesh Jan 30 '15 at 11:30
  • There's no other way of doing that other then appending id in command argument? – Arti Jan 30 '15 at 11:42
  • As far as i know (Not sure.) this is the only way I came across. – Mahesh Jan 30 '15 at 11:44
  • :-) that link helped. – Arti Jan 30 '15 at 11:49
  • There is another way of doing it as add the hidden field in inner repeater and set its value to the your parent value and then access that. – Mahesh Jan 30 '15 at 11:50
  • How can that hidden filed be accesed in my linkbutton command event? – Arti Jan 30 '15 at 12:04
  • You can use `findcontrol` – Mahesh Jan 30 '15 at 12:08