0

I have a repeater and i have made it click able using code from How to add an attribute to repeater item at runtime?

Now i want when i click on any item of repeater it will redirect to another page along with query string.. whose value comes from database like (userid). Actually what i want to do when i click on any item i want to redirect to user detail-page along with its user information.

Community
  • 1
  • 1
Ram Singh
  • 6,664
  • 35
  • 100
  • 166

1 Answers1

3

You may add another attribute to the itemRow ;):

row.Attributes["onclick"] = string.Format("window.location = '{0}';", ResolveClientUrl(string.Format("~/Default.aspx?userId={0}", e.Item.DataItem)) );

Instead to using e.Item.DataItem you may cast DataItem to concrete class and use that class property like this:

MyClass mc = e.Item.DataItem as MyClass;
row.Attributes["onclick"] = string.Format("window.location = '{0}';", ResolveClientUrl(string.Format("~/Default.aspx?userId={0}", mc.UserId)) );

Generally, you may add attributes on row in repeater's ItemTemplate without server-side code. All that you need it's just to bind appropriate fields from your datasource to attributes:

<ItemTemplate>
     <tr onmouseover="window.document.title = '<%# Eval("userId", "Click to navigate onto user {0} details page.") %>'"
         onclick='window.location = "<%# ResolveClientUrl( "~/Default.aspx?userId=" + Eval("userid") ) %>"'>
          <td>
               <%# Eval("UserId") %>
          </td>
     </tr>
</ItemTemplate>
Yuriy Rozhovetskiy
  • 22,270
  • 4
  • 37
  • 68