windowsgm
windowsgm

Reputation: 1616

Passing Params From Aspx

So I had have a table populating with data but I was wondering how I could pass two bits of data from a row depending on which link at the end of the row is clicked.

<%WebReceiptSummary[] receipts = GetReceipts();
          if (receipts != null)
          {
              for (int i = 0; i < receipts.Length; i++)
              {%>
        <tr>
            <td><%= receipts[i].Type%></td>
            <td><%= receipts[i].PolicyNo%></td>
            <td><%= receipts[i].Date%></td>
            <td class="c"><%= receipts[i].Amount%></td>
            <td class="r"><asp:LinkButton OnCommand="PDFLinkClick" 
CommandArgument="<%= receipts[i].PDF %>&<% receipts[i].PolicyNo %>" runat="server">View PDF</asp:LinkButton></td>
        </tr>
        <% }
        }
        %>

Obviously my CommandArgument just passes back the string <%= receipts[i].PDF %>&<% receipts[i].PolicyNo %> not the values. What would be the best way of doing this? I was also thinking of using;

<asp:HiddenField ID="hiddenIsCaptchaReadyValidate" runat="server" Value=false/>

But I have the same problem here where the value is placed within quotes and also it means I need to create two hiddenfields for ever row which isn't the most efficient way of doing this. Thoughts?

Upvotes: 0

Views: 111

Answers (1)

freefaller
freefaller

Reputation: 19953

It is not possible to have <%= %> commands as part of an attribute when added via the mark-up.

Can I recommend that instead of using the for loop in the ASPX, you instead use the <asp:Repeater> control? This will also allow you to set the CommandAttribute value from the code-behind.

An example...

<asp:Repeater runat="server" id="receipts" OnItemDataBound="receipts_ItemDataBound">
  <ItemTemplate>
    <tr>
        <td><%#((WebReceiptSummary)Container.DataItem).Type%></td>
        <td><%#((WebReceiptSummary)Container.DataItem).PolicyNo%></td>
        <td><%#((WebReceiptSummary)Container.DataItem).Date%></td>
        <td class="c"><%#((WebReceiptSummary)Container.DataItem).Amount%></td>
        <td class="r"><asp:LinkButton ID="pdfLink" OnCommand="PDFLinkClick" runat="server">View PDF</asp:LinkButton></td>
    </tr>
  </ItemTemplate>
</asp:Repeater>

In your Init or Load in code behind...

receipts.DataSource = GetReceipts();
receipts.DataBind();

Then...

protected void receipts_ItemDataBound(Object sender, RepeaterItemEventArgs e)
{
  ((LinkButton)e.Item.FindControl("pdfLink")).CommandArgument = 
    ((WebReceiptSummary)e.DataItem).PDF + ((WebReceiptSummary)e.DataItem).PolicyNo;
}

UPDATE

Thinking about it, rather than using the code-behind setting of the CommandArgument, I think (I haven't tested this yet) you could actually do the following without needing the receipts_ItemDataBound function...

 <td class="r"><asp:LinkButton ID="pdfLink" OnCommand="PDFLinkClick" runat="server"
   CommandArgument="<%#((WebReceiptSummary)Container.DataItem).PDF + ((WebReceiptSummary)Container.DataItem).PolicyNo%>"
   >View PDF</asp:LinkButton></td>

UPDATE 2

All instances of Container.DataItem in the above examples, have been changed into the tight-bound ((WebReceiptSummary)Container.DataItem)

Upvotes: 1

Related Questions