Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 668
  • Last Modified:

link button inside a repeater

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

0
boneythomas
Asked:
boneythomas
  • 8
  • 4
1 Solution
 
daveamourCommented:
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
 
0
 
boneythomasAuthor Commented:
thank you..I had thought of it..I will try this and then let you know..
0
 
daveamourCommented:
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?
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
boneythomasAuthor 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.
0
 
daveamourCommented:
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 
0
 
daveamourCommented:
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...
0
 
daveamourCommented:
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?
0
 
boneythomasAuthor 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.
0
 
daveamourCommented:
Ok that's fine
0
 
daveamourCommented:
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

0
 
boneythomasAuthor Commented:
Thankyou so muuch . The code reallly worked
0
 
daveamourCommented:
Of course it works, don't sound so surprised!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 8
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now