views:

1599

answers:

5

I use a Repeater Control for showing the data in my WebSite. I use a HyperLink in the Repeater Control for showing one field of my table in the database.

When you click, I want that link go to another page and send a string to that [page] with a QueryString. I don't know how I do that - can you explain how to do it?

I am using Linq in ASP.net.

+1  A: 

Here's an example from within an ItemTemplate in a Repeater to give you an idea. Latitude and Longitude come from a database

<a target="_blank" class="newwindow" href="http://maps.google.com/maps?saddr=&lt;%=addressTextBox.Text%&gt;&amp;daddr=&lt;%#Eval("Latitude")%&gt;,&lt;%#Eval("Longitude") %>">
David Stratton
I can't Understand please explain more for me please
mohammad reza
+3  A: 

Let's start with the format of the QueryString. The QueryString looks like the following:

http://www.mysite.com/somepage.aspx?id=?

Right!

Of course you can pass multiple parameters in QueryString using the "&" symbol as shown below:

http://www.mysite.com/somepage.aspx?id=?&amp;foo=?

Now, you need to do the same thing but inside the Repeater control and using the values from the database.

<asp:Repeater>

<ItemTemplate>
<a href="http://www.mysite.com/somepage.aspx?id=&lt;%# Eval("Id") #>"><Eval("Title")</a>
</ItemTemplate>
</asp:Repeater>

The Eval("Id") is the property from your data source which can be DataSet, DataTable, Entity classes etc.

The <%# Eval("Id") #> will be called when you bind the Repeater control. You bind the Repeater control using the Repeater.DataBind() method.

Getting the id on the other page:

if(Request.QueryString["id"] != null) 
{
   string id = Request.QueryString["id"] as String; 
}
azamsharp
I would change ?id=? to something like ?id=0, since using ? as a placeholder might confuse someone with no idea how a query string works.
Jason Berkan
Thanks Jason for pointing that out :)
azamsharp
when I send this ID , How I get that on that page
mohammad reza
I have edited my answer please check it out above!
azamsharp
A: 

Use Eval method to evaluate a field into your rendered page. For example you have a Products table, and you want a link that passes a product's id to showproduct.aspx. Check the example below :

<a href='showproduct.aspx?productid=<%#Eval("ProductID")%>'>View Details</a>

At showproduct.aspx use Page.Request.QueryString collection to get product :

string productId = Page.Request.QueryString["productid"].ToString();
Canavar
when I send this ID , How I get that on that page(showproduct)
mohammad reza
+1  A: 
<asp:HyperLink ID="HyperLink3" NavigateUrl='<%#Eval("ID_Message","~/ADMIN/Reply.aspx?ID={0}") %>' runat="server">OK</asp:HyperLink>
mohammad reza
A: 

Since everyone has posted code with all the evaluation embedded in the aspx page I will post one with all the code required in the code behind (where I prefer all this code to be).

First in your repeater you will need a control:

<asp:Repeater>
    <ItemTemplate>
        <asp:HyperLink ID="hrefLink"
            href="http://www.mysite.com/somepage.aspx?id={0}&amp;more={1}"
            OnDataBinding="hrefLink_DataBinding">
        </asp:HyperLink>
    </ItemTemplate>
</asp:Repeater>

Then in your code behind you implement the databinding to fill in your links details:

protected void hrefLink_DataBinding(object sender, System.EventArgs e)
{
    HyperLink link = (HyperLink)(sender);
    // Fill in your links details
    link.NavigateUrl = string.Format(link.NavigateUrl,
        Eval("ID").ToString(), Eval("More").ToString());
    link.Text = Eval("LinkTitle").ToString();        
}

The advantage to this is that you can easily add more logic when needed without cluttering your aspx page with tons of code. I prefer this method to inline but they are both valid solutions and it's more of a preference.

If you don't want to predefine where the link would go you could change the above databinding code to rewrite the entire NavigateUrl to whatever you want. So based on some evalulated value you could redirect to different pages. It's the most customizeable solution.

Side note: Make sure you turn off ViewState on repeaters if you don't need it as it causes a ton of clutter.

Kelsey