Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 7th, 2006, 04:49 AM
vbmazza
Guest
 
Posts: n/a
Default Image Button as Template Field in GridView control

Hi,

I have a GridView control in which I have a template column with an image button.

When I disable the GridView is doesn't provide a disabled image, which I expected and I know it doesn't support a disabled state.

Basically, when I click a button, it fires that button's event and disables the GridView control but not the image of the imagebutton, what I would ideally like to do at that stage is change the imageURL for the disabled imageURL but I am not sure how to refer to or reference the control in order to set the imageURL propery.

The ASP.Net source is:-
<ItemTemplate>
     <asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="false" CommandName="" ImageUrl="~/Images/pdf-icon.gif" Text="" OnClick="PDFButton_Click" ToolTip="Download PDF" />
</ItemTemplate>

As you can see it has an ID name, but I cannot use this name using FindControl, as it is not visable to my VB.Net code.

If anyone could help me with this that would be great, as this is beginning to get me down. Also, if there is a completley different way to achieve the same results, I am open to suggestions.

Cheers

Chris


VBMazza
  #2 (permalink)  
Old August 7th, 2006, 05:13 AM
Imar's Avatar
Wrox Author
Points: 72,022, Level: 100
Points: 72,022, Level: 100 Points: 72,022, Level: 100 Points: 72,022, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,076
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Quote:
quote:As you can see it has an ID name, but I cannot use this name using FindControl, as it is not visable to my VB.Net code.
Why not??

Can't you loop through the Items collection of the control when the button that disables the control is clicked? Then you can use FindControl and change the image....

Imar
  #3 (permalink)  
Old August 7th, 2006, 08:57 AM
vbmazza
Guest
 
Posts: n/a
Default

Hi,

I have no problem referencing the column if it is just a ButtonField column, the problem is I want to trap the click event of the button to run my own click event code so I converted the ButtonField into a TemplateField which made this an ImageButton control within a TemplateField which provided me with the 'onclick' property.

I know the column that the control is in, but this column is classed as a template field and I am not sure how to get at the Image Button to replace the ImageURL property, this is easy to do with just a ButtonField. If I could use a ButtonField this would be ideal, how on earth do you trap the click event of a ButtonField as it doesn't have a property for this, only the usual edit, delete in CommandName.

Thanks

Chris.



VBMazza
  #4 (permalink)  
Old August 7th, 2006, 02:13 PM
Imar's Avatar
Wrox Author
Points: 72,022, Level: 100
Points: 72,022, Level: 100 Points: 72,022, Level: 100 Points: 72,022, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,076
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Assuming I understand what you want, I think the code below does exactly that.

Create a new ASPX page, and add the following GridView and button to it:
Code:
<asp:GridView ID="GridView1" runat="server">
  <Columns>
    <asp:TemplateField ShowHeader="True">
      <ItemTemplate>
        <asp:ImageButton runat="server" ID="ImageButton1" 
            ImageUrl="Enabled.gif" />
      </ItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="Button" 
            OnClick="Button1_Click" />
            Each button in the GridView has its source set to Enabled.gif.

Next, add this code to load some items in the Grid:
Code:
protected void Page_Load(object sender, EventArgs e)
{
  string[] movies = new string[] {"Reservoir Dogs", "Pulp 
              Fiction", "Natural Born Killers", "12 Monkeys"};
  // Bind the datasource to the DataGrid
  GridView1.DataSource = movies;
  GridView1.DataBind();
}
This adds a few items to the gird on Page_Load.

Finally, you need some code that runs when the button is clicked:
Code:
protected void Button1_Click(object sender, EventArgs e)
{
  GridView1.Enabled = false;
  // Find the button
  foreach(GridViewRow myRow in GridView1.Rows)
  {
    ImageButton myButton = myRow.Cells[0].Controls[1] as ImageButton;
    if (myButton != null)
    {
      myButton.ImageUrl = "disabled.gif";
    }
  }
}
This code loops through each row in the grid, finds and casts the ImageButton and changes the ImageUrl.

Was this what you were looking for??

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
  #5 (permalink)  
Old August 8th, 2006, 03:13 AM
vbmazza
Guest
 
Posts: n/a
Default

Thanks, this was very helpful. I had already got the click events and had set up the gridview in the same way.

I did use the GridViewRow property, but I was going at it from the wrong angle, choosing the template column and then trying to find the imagebutton within that. I didn't think about using cells, and then choose the control as I would do with a regular HTML table with controls in it.

Thanks for your help, it was only a small part of my project but it was becoming the biggest headache.

Chris.:D



VBMazza
  #6 (permalink)  
Old August 8th, 2006, 03:28 AM
Imar's Avatar
Wrox Author
Points: 72,022, Level: 100
Points: 72,022, Level: 100 Points: 72,022, Level: 100 Points: 72,022, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,076
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Quote:
quote:I had already got the click events and had set up the gridview in the same way.
Yeah, I figured you had.

I posted a code complete example, because I am sure others will find this thread in the future... ;)

Glad it's working now.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
  #7 (permalink)  
Old October 31st, 2006, 05:41 AM
Registered User
 
Join Date: Oct 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Nice solution :-)

But I have same problem with repeater. How can I find a ImageButton control to set "disabled" image?

Thanks

  #8 (permalink)  
Old October 31st, 2006, 06:10 AM
Registered User
 
Join Date: Oct 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by t800t8
 Nice solution :-)

But I have same problem with repeater. How can I find a ImageButton control to set "disabled" image?

Thanks

More exactly, I want to change only one ImageButton which I've just clicked.

  #9 (permalink)  
Old October 31st, 2006, 01:56 PM
Imar's Avatar
Wrox Author
Points: 72,022, Level: 100
Points: 72,022, Level: 100 Points: 72,022, Level: 100 Points: 72,022, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,076
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

You can use similar code. However, a Repeater has an Items collection that you can loop through instead of the Rows collection. Each item (a RepeaterItem object) has a method called FindControl that you can use to find the LinkButton.

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
  #10 (permalink)  
Old May 24th, 2007, 04:41 PM
Registered User
 
Join Date: May 2007
Location: Mangalia, cta, Romania.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by Imar
 Assuming I understand what you want, I think the code below does exactly that.

Create a new ASPX page, and add the following GridView and button to it:
Code:
<asp:GridView ID="GridView1" runat="server">
  <Columns>
    <asp:TemplateField ShowHeader="True">
      <ItemTemplate>
        <asp:ImageButton runat="server" ID="ImageButton1" 
            ImageUrl="Enabled.gif" />
      </ItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="Button" 
            OnClick="Button1_Click" />
            Each button in the GridView has its source set to Enabled.gif.

Next, add this code to load some items in the Grid:
Code:
protected void Page_Load(object sender, EventArgs e)
{
  string[] movies = new string[] {"Reservoir Dogs", "Pulp 
              Fiction", "Natural Born Killers", "12 Monkeys"};
  // Bind the datasource to the DataGrid
  GridView1.DataSource = movies;
  GridView1.DataBind();
}
This adds a few items to the gird on Page_Load.

Finally, you need some code that runs when the button is clicked:
Code:
protected void Button1_Click(object sender, EventArgs e)
{
  GridView1.Enabled = false;
  // Find the button
  foreach(GridViewRow myRow in GridView1.Rows)
  {
    ImageButton myButton = myRow.Cells[0].Controls[1] as ImageButton;
    if (myButton != null)
    {
      myButton.ImageUrl = "disabled.gif";
    }
  }
}
This code loops through each row in the grid, finds and casts the ImageButton and changes the ImageUrl.

Was this what you were looking for??

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.



10x m8....i need it this one! I signed up just to say thanks! :D:D

 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to put a button control in a GridView control ryan.webb ASP.NET 2.0 Basics 2 December 28th, 2007 05:26 AM
Replace Gridview field if null with new field Indo77 ASP.NET 2.0 Basics 1 June 18th, 2007 06:22 AM
Need to get value of gridview template columns for kmh030 ASP.NET 2.0 Professional 1 January 6th, 2007 11:10 PM
To get text of Button Field column in GridView nitinp ASP.NET 2.0 Basics 1 November 3rd, 2006 10:57 AM
"Indent" field in gridview template. wirerider ASP.NET 2.0 Basics 0 March 1st, 2006 12:15 AM



All times are GMT -4. The time now is 04:42 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.