Solved

Embedding an Image within a user control

Posted on 2012-04-11
4
265 Views
Last Modified: 2012-06-05
Hello Experts,

I found the following code on the website http://www.codeproject.com. The author Manny Salazar designed this code to be a user control that provides a custom pager for the GridView control.

This code is compiled into a DLL and placed in the bin folder for the website.

I want to modify the code to use images in place of the"First", "Next", "Previous", and "Last" LinksButtons.



protected override void InitializePager
    (GridViewRow row, int columnSpan, PagedDataSource pagedDataSource)
{
    if (PagerType == ThisPagerType.Regular)
    {
        // if PagerType is not DropDownList
        // render the regular GridView Pager
        base.InitializePager(row, columnSpan, pagedDataSource);
    }
    else
    {
        TableCell cell_1;
        // if we are going to use dropdownlist
        if (PagerType == ThisPagerType.DropDownList)
        {
            // our Pager with DropDownList control

            // create our DropDownList control
            DropDownList ddl = new DropDownList();
            // populate it with the number of Pages of our GridView
            for (int i = 0; i < PageCount; i++)
            {
                ddl.Items.Add(new ListItem(Convert.ToString(i + 1), i.ToString()));
            }
            ddl.AutoPostBack = true;
            // assign an Event Handler when its Selected Index Changed
            ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
            // synchronize its selected index to GridView's current PageIndex
            ddl.SelectedIndex = PageIndex;

            // add our first TableCell which will contain the DropDownList
            cell_1 = new TableCell();

            // we just add a Label with 'Page ' Text
            cell_1.Controls.Add(PageOf());

            // our DropDownList control here.
            cell_1.Controls.Add(ddl);

            // and our Total number of Pages
            cell_1.Controls.Add(PageTotal());
        }
        else
        {
            // if we are going to use the FirstPrevNextLast buttons.
            LinkButton first = new LinkButton();
            first.Text = "First ";
            // first button will always have a value of zero (0)
            first.CommandArgument = "0";
            first.Enabled = PageIndex > 0;
            // add click event handler
            first.Click += new EventHandler(navigate_Click);

            LinkButton prev = new LinkButton();
            prev.Text = "Prev ";
            // set Prev button arguments to current PageIndex minus 1
            prev.CommandArgument = string.Format("{0}", (PageIndex - 1));
            prev.Enabled = (PageIndex > 0);
            prev.Click += new EventHandler(navigate_Click);

            LinkButton next = new LinkButton();
            next.Text = "Next ";
            // set Next button arguments to current PageIndex plus 1
            next.CommandArgument = string.Format("{0}", (PageIndex + 1));
            next.Enabled = (PageIndex < (PageCount - 1));
            next.Click += new EventHandler(navigate_Click);

            LinkButton last = new LinkButton();
            last.Text = "Last";
            // Last button will always have a value equal to PageCount minus 1
            last.CommandArgument = string.Format("{0}", (PageCount - 1));
            last.Enabled = (PageIndex < (PageCount - 1));
            last.Click += new EventHandler(navigate_Click);

            cell_1 = new TableCell();

            // add the First button to cell controls collection
            cell_1.Controls.Add(first);
            // add the Prev button
            cell_1.Controls.Add(prev);
            // add the Next button
            cell_1.Controls.Add(next);
            // add the Last button
            cell_1.Controls.Add(last);
        }

        // create a Table that will replace entirely our GridView's Pager section
        Table tbl = new Table();
        tbl.BorderWidth = 0;
        tbl.Width = Unit.Percentage(100);
        // add one TableRow to our Table
        tbl.Rows.Add(new TableRow());

        // the 2nd TableCell will display the Record number you are currently in.
        TableCell cell_2 = new TableCell();
        cell_2.Controls.Add(PageInfo(pagedDataSource.DataSourceCount));

        // add now the 2 cells to our created row
        tbl.Rows[0].Cells.Add(cell_1);
        tbl.Rows[0].Cells.Add(cell_2);
        tbl.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Left;
        tbl.Rows[0].Cells[1].HorizontalAlign = HorizontalAlign.Right;

        // in Pager's Row of our GridView add a TableCell
        row.Controls.AddAt(0, new TableCell());
        // sets it span to GridView's number of columns
        row.Cells[0].ColumnSpan = Columns.Count;
        // finally add our created Table
        row.Cells[0].Controls.AddAt(0, tbl);
    }
}

protected virtual void navigate_Click(object sender, EventArgs e)
{
    OnPageIndexChanging(new GridViewPageEventArgs
        (int.Parse(((LinkButton)sender).CommandArgument)));
}

protected virtual void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
    // on our DropDownList SelectedIndexChanged event
    // call the GridView's OnPageIndexChanging method
    // to raise the PageIndexChanging event.
    // pass the DropDownList SelectedIndex as its argument.
    OnPageIndexChanging(new GridViewPageEventArgs
            (((DropDownList)sender).SelectedIndex));
}

private Label PageOf()
{
    // it is just a label
    Label lbl = new Label();
    lbl.Text = "Page ";
    return lbl;
}

private  Label PageTotal()
{
    // a label of GridView's Page Count
    Label lbl = new Label();
    lbl.Text = string.Format(" of {0}", PageCount);
    return lbl;
}

private Label PageInfo(int rowCount)
{
    // create a label that will display the current Record you're in
    Label label = new Label();
    int currentPageFirstRow = ((PageIndex * PageSize) + 1);
    int currentPageLastRow = 0;
    int lastPageRemainder = rowCount % PageSize;
    currentPageLastRow = (PageCount == PageIndex + 1) ?
        (currentPageFirstRow + lastPageRemainder - 1) :
        (currentPageFirstRow + PageSize - 1);
    label.Text = String.Format("Record {0} to {1} of {2}",
        currentPageFirstRow, currentPageLastRow, rowCount);
    return label;
}

[Category("Paging")]
[DefaultValue("1")]
public ThisPagerType PagerType
{
    get
    {
        return (ViewState["PagerType"] != null ?
            (ThisPagerType)ViewState["PagerType"] : ThisPagerType.DropDownList);
    }
    set
    {
        ViewState["PagerType"] = value;
    }
}

public enum ThisPagerType
{
    Regular = 0,
    DropDownList = 1,
    LinkButton = 2
}

if (PagerType != ThisPagerType.DropDownList)
{
    // if PagerType is not DropDownList
    // render the regular GridView Pager
    base.InitializePager(row, columnSpan, pagedDataSource);
}
else
{
    if (PagerType == ThisPagerType.DropDownList)
    {
        // our Pager with DropDownList control
    }
    else
    {
        // our Pager with LinkButton controls
    }
}

Open in new window


I have four .gif(s) that I want to use in the following part of the code:

     else
        {
            // if we are going to use the FirstPrevNextLast buttons.
            LinkButton first = new LinkButton();
            first.Text = "First ";
            // first button will always have a value of zero (0)
            first.CommandArgument = "0";
            first.Enabled = PageIndex > 0;
            // add click event handler
            first.Click += new EventHandler(navigate_Click);

            LinkButton prev = new LinkButton();
            prev.Text = "Prev ";
            // set Prev button arguments to current PageIndex minus 1
            prev.CommandArgument = string.Format("{0}", (PageIndex - 1));
            prev.Enabled = (PageIndex > 0);
            prev.Click += new EventHandler(navigate_Click);

            LinkButton next = new LinkButton();
            next.Text = "Next ";
            // set Next button arguments to current PageIndex plus 1
            next.CommandArgument = string.Format("{0}", (PageIndex + 1));
            next.Enabled = (PageIndex < (PageCount - 1));
            next.Click += new EventHandler(navigate_Click);

            LinkButton last = new LinkButton();
            last.Text = "Last";
            // Last button will always have a value equal to PageCount minus 1
            last.CommandArgument = string.Format("{0}", (PageCount - 1));
            last.Enabled = (PageIndex < (PageCount - 1));
            last.Click += new EventHandler(navigate_Click);

            cell_1 = new TableCell();

            // add the First button to cell controls collection
            cell_1.Controls.Add(first);
            // add the Prev button
            cell_1.Controls.Add(prev);
            // add the Next button
            cell_1.Controls.Add(next);
            // add the Last button
            cell_1.Controls.Add(last);
        }

Open in new window


I am assuming that's where I would substitute the images for the text. If anyone could help me figure this out that would be great.

I want to embed the images in the dll so that the user doesn't have to worry about where the images are.

As a further enhancement I wanted to embed a css file. Is that possible and if so how?

Thanks.
0
Comment
Question by:Twardone45
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 

Author Comment

by:Twardone45
ID: 37832477
I did try this:

   // if we are going to use the FirstPrevNextLast buttons.
            ImageButton first = new ImageButton();
            first.ImageUrl="\\webserver\\wwwroot\\folder\firstpage.gif;
            // first button will always have a value of zero (0)
            first.CommandArgument = "0";
            first.Enabled = PageIndex > 0;
            // add click event handler
            first.Click += new ImageClickEventHandler(navigate_Click);

Open in new window


The page loaded without errors but the image was not visible. I am assuming that the reference is wrong for the line:

first.ImageUrl="\\webserver\\wwwroot\\folder\firstpage.gif;

Open in new window


Again, any help is appreciated.
0
 
LVL 24

Expert Comment

by:alexey_gusev
ID: 37836258
first of all, you're missing a quote mark at the end of the string, but I assume that's just a typo :)

re. images - why not to add them to the resources in VS and then simply refer from the code? that's exactly what you wanted as far as I understand it
0
 

Author Comment

by:Twardone45
ID: 37837421
Thank you. I did add them as a "Resource" but I still couldn't get the images to load. I am using an ImageButton and it asks for an ImageUrl but well...I'm not hitting the "mark".

Can you give me some sample code that will do what I am trying to do?

Thanks.

P.S.

I've been on the Internet for 2 days researching this and I'm a little frustrated. I know the answer is out ther but I'm not sure where.
0
 
LVL 24

Accepted Solution

by:
alexey_gusev earned 300 total points
ID: 37839805
oh, I misread your OP slightly and now see it's a web project, so you should be able to put your images to the subfolder of your wwwroot just like shown here.

NB: the url is a relative path, ie in above example it's "images/image1.jpg" i.e. your web root directory for your particular webserver contains "images" folder and so on.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Before we dive into the marketing strategies involved with creating an effective homepage, it’s crucial that EE members know what a homepage is. In essence, a homepage is the introductory, or default page, of a website that typically highlights the …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question