We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

link button inside a repeater

Medium Priority
708 Views
Last Modified: 2012-06-22
I have a linkbutton inside a repeater. in my aspx.cs page I need to write a event handler for the linkbutton. by this click event I need to redirect the link to a different location. I am not able to call the linkbutton id. could you just help me out here. I am using the link button because i need to update a table on the click of the button. That's the reason why i am not using the hyperlink.Could you please help me out...I have attached my aspx page where the linkbutton code is written
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound" DataSourceID ="ViewDocuments" OnItemCommand="Repeater1_ItemCommand">
                <HeaderTemplate><table></HeaderTemplate>
                <ItemTemplate>
                <tr><td><asp:LinkButton
                        ID="lnkFilename" runat="server" Text='<% # Eval("DocumentName")%>' ></asp:LinkButton>
                       
                        </td></tr>
                </ItemTemplate>
                <FooterTemplate>
                </table>
                </FooterTemplate>
                </asp:Repeater>

Open in new window

Comment
Watch Question

I think you can use something like:
<asp:LinkButton ID="lnkFilename" runat="server" Text='<% # Eval("DocumentName")%>' Commandname="MyMethod" CommandArgument="<% # Eval("DocumentName")%>" ></asp:LinkButton>
Then you should be able to pick up the DocumentName in te event handler
 

Author

Commented:
thank you..I had thought of it..I will try this and then let you know..
Actually it's not quite right.
You create an event handler in code behind fro the repeate like this:

MyRepeater.ItemCommand += new RepeaterCommandEventHandler(Test);
Then create an event handler like this:
 
public void Test(object sender, CommandEventArgs e)
{
     Response.Write(e.CommandArgument.ToString());
}
Then in the aspx you have:

Commandname="DoSomething" CommandArgument="22"
Or whatever you want in there then these will be available in the e object:
e.CommandArgument and e.CommandName
Ok?

Author

Commented:
I am sorry ..It is'nt clear here. Could you explain with an example..I'll give you my scenario.

I have a table called documents which has a list of filenames of the registered candidate. Now as he enters the page (i'm working on) he has to see his list of files. I am getting the files using the repeater.Now I need to link them to the prescribed location.As the button is clicked the table also needs to be updated.The table has a column called modified date. by clicking the link (for modification of the document) the date modified column must have the current date.
Yes I can so that for you.
I will give you complete working code but first can you look at another question I answered for you.  I thought my answer was good but you haven't assigned any points.  Can you have a look at this please:
http://www.experts-exchange.com/Programming/Languages/.NET/Q_24132294.html 
Ok thansk for the points on the other question.
Give me a little time to get this working for you - won't take too long...
What kind of files are these - ie how does the user get to them - by a url or an edit page with some kind of id on a querystring maybe?

Author

Commented:
these files can be any document..like word or excel and it is stored in a folder called uploads under the name of the logged in user? I hope its clear .If not do let me know.
Ok that's fine
Ok all done.
You will of cours ehave to tinker with it to meet your exact requirements but it's pretty much al done for you.,  See attached code.
I slightly misused the CommandName to store the url of teh document but I was in a rush and this is ok really.  Other wise you would need some other code to return a document url from a given documentID.

Database table script:
 
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Documents]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Documents]
GO
 
CREATE TABLE [dbo].[Documents] (
	[DocumentID] [int] IDENTITY (1, 1) NOT NULL ,
	[DocumentName] [varchar] (100) COLLATE Latin1_General_CI_AS NOT NULL ,
	[DocumentUrl] [varchar] (200) COLLATE Latin1_General_CI_AS NOT NULL ,
	[ModifiedDate] [datetime] NULL 
) ON [PRIMARY]
GO
 
Sample data:
 
	1 Dave CV		http://www.audacs.co.uk/cvs/DaveAmourCV.pdf	12/02/2009 14:28:35
	2 Info Behaviour	http://www.bl.uk/news/pdf/googlegen.pdf	12/02/2009 14:28:39
	3 Form W4		http://www.irs.gov/pub/irs-pdf/fw4.pdf	
 
Aspx page:
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RepeaterDocuments.aspx.cs" Inherits="RepeaterDocuments" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Repeater ID="DocumentsRepeater" runat="server">
            <ItemTemplate>
                <asp:LinkButton ID="DocumentLinkButton" runat="server" Text=<%# DataBinder.Eval(Container.DataItem, "DocumentName") %> CommandName=<%# DataBinder.Eval(Container.DataItem, "DocumentUrl") %> CommandArgument=<%# DataBinder.Eval(Container.DataItem, "DocumentID") %>></asp:LinkButton><br />
            </ItemTemplate>
        </asp:Repeater>
    </form>
</body>
</html>
 
 
C# Code behind:
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
 
public partial class RepeaterDocuments : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DocumentsRepeater.DataSource = GetDocuments();
            DocumentsRepeater.DataBind();
        }
    }
 
    protected override void OnInit(EventArgs e)
    {
        DocumentsRepeater.ItemCommand += new RepeaterCommandEventHandler(DocumentsRepeater_ItemCommand);
        
        base.OnInit(e);
    }
 
    private void DocumentsRepeater_ItemCommand(object sender, CommandEventArgs e)
    {
        UpdateDocumentDateTimeStamp(Convert.ToInt32(e.CommandArgument));
 
        Response.Redirect(e.CommandName);
    }
 
    private void UpdateDocumentDateTimeStamp(int documentID)
    {
        using (SqlConnection dbConnection = new SqlConnection("Data Source=MyServer;Initial Catalog=MyDatabase;User Id=Bob;Password=MyPassword;"))
        {
            using (SqlCommand dbCommand = new SqlCommand("Update Documents Set ModifiedDate = GetDate() Where DocumentID = " + documentID))
            {
                dbCommand.Connection = dbConnection;
 
                dbCommand.CommandType = CommandType.Text;
 
                dbConnection.Open();
 
                dbCommand.ExecuteNonQuery();
            }
        }
    }
 
    private List<Document> GetDocuments()
    {
        List<Document> docs = new List<Document>();
 
        using (SqlConnection dbConnection = new SqlConnection("Data Source=MyServer;Initial Catalog=MyDatabase;User Id=Bob;Password=MyPassword;"))
        {
            using (SqlCommand dbCommand = new SqlCommand("Select DocumentID, DocumentName, DocumentUrl From Documents"))
            {
                dbCommand.Connection = dbConnection;
 
                dbCommand.CommandType = CommandType.Text;
 
                dbConnection.Open();
 
                SqlDataReader reader = dbCommand.ExecuteReader();
 
                while (reader.Read())
                {
                    int docID = reader.GetInt32(0);
                    string docName = reader.GetString(1);
                    string docUrl = reader.GetString(2);
 
                    docs.Add(new Document(docID, docName, docUrl));
                }
            }
        }
 
        return docs;
    }
}
 
public class Document
{
    public Document()
    {
 
    }
    
    public Document( string documentName, string documentUrl)
    {
        this.documentName = documentName;
        this.documentUrl = documentUrl;
    }
    
    public Document(int documentID, string documentName, string documentUrl)
    {
        this.documentID = documentID;
        this.documentName = documentName;
        this.documentUrl = documentUrl;
    }
    
    private int documentID;
    private string documentName;
    private string documentUrl;
 
    public int DocumentID
    {
        get
        {
            return documentID;
        }
 
        set
        {
            documentID = value;
        }
    }
 
    public string DocumentName
    {
        get
        {
            return documentName;
        }
 
        set
        {
            documentName = value;
        }
    }
 
    public string DocumentUrl
    {
        get
        {
            return documentUrl;
        }
 
        set
        {
            documentUrl = value;
        }
    }
}

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Thankyou so muuch . The code reallly worked
Of course it works, don't sound so surprised!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.