Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 19th, 2008, 09:40 AM
Authorized User
 
Join Date: Apr 2008
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
Default Changing the image button on PostBack

Hi,

I have a image button. I want that after I press that button, the image button to change. I don't know how to do that. Either from aspx file, or aspx.cs file it's ok ..

Any idea ?

Thanks

Thank you
__________________
Thank you
 
Old May 19th, 2008, 09:48 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I don't see what the problem is - you simply change the ImageUrl in the click event:

Code:
private void ImageButton1_Click(object sender, EventArgs e)
{
    ImageButton1.ImageUrl = "image2.gif";
}
/- Sam Judson : Wrox Technical Editor -/
 
Old May 19th, 2008, 10:05 AM
Authorized User
 
Join Date: Apr 2008
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Sorry, I'm just a beginner in C# and .NET technologies.

The problem is that I have a gridView ItemTemplate field. If I'll add a OnClick function and put the code which you wrote here, it will give me the following error...Object reference not set to an instance of an object.



Thank you
 
Old May 19th, 2008, 10:08 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

OK, that explains your problem a little better. Try this instead then:

Code:
private void ImageButton1_Click(object sender, EventArgs e)
{
    ImageButton button = (ImageButton)sender;
    button.ImageUrl = "image2.gif";
}
/- Sam Judson : Wrox Technical Editor -/
 
Old May 19th, 2008, 10:30 AM
Authorized User
 
Join Date: Apr 2008
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Well, I put that in the code but it's not working.

So I have one user list(grid View). After I'll press the ImageButton, and after he'll update the status of the user from active to inactive , I want the image button changed. the rest of the code(command name, argument and the rest) are working just fine (in sql tabel the user status is updated when I press the image button). The only thing is that the image doesn't change.

So ..some code :
aspx:
<asp:TemplateField>
<ItemTemplate>
 <asp:ImageButton ID="imgbtnSetInactiveUser" runat="server" ImageUrl="~/Images/interziss.jpg" CommandName="SetInactiveUser" OnClientClick="changeImageButton" />
  </ItemTemplate>
  </asp:TemplateField>


C# :
  protected void gvUserList_RowCommand(object sender, GridViewCommandEventArgs e)
        {

            if (e.CommandName.Equals("EditUser"))
            {
                Response.Redirect("~/AddUser.aspx?UserID="+e.CommandArgument);
            }

            else if (e.CommandName.Equals("SetInactiveUser"))
            {
                Logins objLogins = new Logins();
                objLogins.SetInactiveUser(int.Parse(e.CommandArgum ent.ToString()));



            }
        }

protected void gvUserList_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            LinkButton lnkUserName;
            ImageButton imgbtnSetInactiveUser;

            if (e.Row.RowType != DataControlRowType.DataRow)
            {

                return;

            }
            else
            {
                lnkUserName = (LinkButton)(e.Row.Cells[0].FindControl("lnkUserName"));
                imgbtnSetInactiveUser = (ImageButton)(e.Row.Cells[0].FindControl("imgbtnSetInactiveUser"));
                DataRowView objDRV = ((DataRowView)e.Row.DataItem);

                if (lnkUserName != null)
                {
                    lnkUserName.CommandArgument = objDRV["UserID"].ToString();
                }

                if (imgbtnSetInactiveUser != null)
                {
                    imgbtnSetInactiveUser.CommandArgument = objDRV["UserID"].ToString();
                }


            }

        }


        private void changeImageButton(object sender, EventArgs e)
        {
            ImageButton imgbtnSetInactiveUser = (ImageButton)sender;
            imgbtnSetInactiveUser.ImageUrl = "inter.jpg";
        }




Thank you
 
Old May 19th, 2008, 10:36 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

ClientClick is for javascript on the client side, and probably not what you want.

However you already have the code in front of you that you need.

ImageButton imgbtnSetInactiveUser;
imgbtnSetInactiveUser = (ImageButton)(e.Row.Cells[0].FindControl("imgbtnSetInactiveUser"));
imgbtnSetInactiveUser.ImageUrl = "inter.jpg";



/- Sam Judson : Wrox Technical Editor -/
 
Old May 19th, 2008, 11:20 AM
Authorized User
 
Join Date: Apr 2008
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thank you for your time and sorry for confusing you.

Thank you





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to disable PostBack for a button control? ashraf_gawdat .NET Framework 2.0 7 October 23rd, 2006 01:14 PM
Make a image button as default submit button toshi ASP.NET 1.0 and 1.1 Basics 1 June 1st, 2006 05:25 AM
Document Image Processing (DIP) - changing image f ALcom Access 0 March 27th, 2006 06:10 AM
button postback does not work v_ganeshraju VS.NET 2002/2003 1 September 8th, 2004 07:33 AM
Changing minimize button snowy0 VB.NET 3 July 29th, 2004 10:52 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.