1

I'm having a problem trying to get the value of the ID item that has been clicked in an asp.net Repeater. My Repeater has an image that opens a dialog, and from that dialog opened, when I click Approve, I would like to get this value in the .cs file when I'm redirected because the ApproveChange_Click event. I'm not doing any DataBinder.Eval to the Id I want to retrieve in the Repeater. How can I accomplish that? If I use session variables, where I can set up the value in the .aspx page and how. Thanks in advance!

This is the Repeater:

<tr class="<%# Container.ItemIndex % 2 != 0 ? "" : "odd" %>">
     <td class ="approval-img"><a class ="approvalDialog" href='#'><img src="/Images/Approve.png" alt ="Approve"/></td></a>
     <td class ="approval-img"><a class ="declineDialog" href='#'><img src="/Images/Decline.png" alt ="Decline"/></td></a>
     ...
</tr>

And this the dialog:

<div id="approval-form" style="display: none; cursor: default">
    <div class="approve-change">
        <ul>
        <li>
            <p><label>Reason</label></p>
            <textarea id="txtReason" runat="server" cols="1" rows="1" class="required"></textarea><br />
        </li>
        <li>
            <span>
            <asp:Button ID="btnApprove" runat="server" CssClass="blue" Text="Approve" ToolTip = "Approve" OnClick="ApproveChange_Click" />
            <button id="btnCancelApprove" class="blue">Cancel</button>
            </span>
        </li>
        </ul>
    </div>
</div>
Jorge
  • 1,178
  • 2
  • 13
  • 33

2 Answers2

2

You can use the button's NamingContainer property to get the RepeaterItem. But it's not clear where you have stored the ID if you are "not doing any DataBinder.Eval to the Id".

So i would recommend to use for example a HiddenField for this, use Eval to apply the ID to it's Value property.

Somewhere in the repeater:

<asp:HiddenField ID="HiddenID" runat="server" Value='<%# Eval("ID") %>' />

Now you can get it in following way:

protected void ApproveChange_Click(Object sender, EventArgs e)
{
    Button btn = (Button) sender;
    RepeaterItem item = (RepeaterItem)  btn.NamingContainer;
    HiddenField idField = (HiddenField) item.FindControl("HiddenID");
    int ID = int.Parse(idField.Value);
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Hi Tim! thanks for the reply. But I'm getting an exception in the cast to Repetear: Unable to cast object of type 'System.Web.UI.WebControls.ContentPlaceHolder' to type 'System.Web.UI.WebControls.RepeaterItem'. I have tried with the Eval in and outside the . I don't show the ID in the Repetear that's why I told thta I'm nnot doing any DataBinder.Eval to the Id – Jorge Dec 04 '12 at 16:38
  • @Jorge: I understand, then your dialog is outside of the repeater. The problem is that you seem to do all at clientside. So there is no point where you could use the session if you open the dialog immediately when the user clicks on the image. How do you open that dialog? Maybe you could pass the ID to the javascript function or read the hiddenfield from there. – Tim Schmelter Dec 04 '12 at 16:44
  • Yes, it is outside the Repeater. I open the dialog with: $('.approvalDialog').click(function () { $.blockUI({ message: $('#approval-form'), baseZ: 10000 }); $('#approval-form').parent().appendTo($("form:first")); return false;}); I'm going to search information of how to do what you are saying, thanks! – Jorge Dec 04 '12 at 16:49
  • Hi! With this: http://stackoverflow.com/a/11825092/1528483 I could get the id of the rows in the .cs file, but how do I know which row has been selected? – Jorge Dec 05 '12 at 10:49
0

Here is the solution that I have found: First: change the tag in the repeater for ImageButton, for have the chance to send a command argument to the event

<td class ="approval-img"><asp:ImageButton runat="server" CommandArgument = '<%# Eval("aux_approvalId")%>'  OnClick="getApprovalID_approve" ToolTip="Approve" ImageUrl="/Images/Approve.png" /></td>

So, when I'm redirected to OnClick event, with this, I get the ID and after the dialog is opened.

ImageButton btn = (ImageButton)(sender);
Session["ApprovalID"] = btn.CommandArgument;
string script = "OpenApprovalDialog();";
Page.ClientScript.RegisterStartupScript(typeof(Page), "", script, true);
Jorge
  • 1,178
  • 2
  • 13
  • 33