 |
| 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
|
|
|
|

May 19th, 2008, 09:40 AM
|
|
Authorized User
|
|
Join Date: Apr 2008
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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
|
|

May 19th, 2008, 09:48 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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 -/
|
|

May 19th, 2008, 10:05 AM
|
|
Authorized User
|
|
Join Date: Apr 2008
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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
|
|

May 19th, 2008, 10:08 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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 -/
|
|

May 19th, 2008, 10:30 AM
|
|
Authorized User
|
|
Join Date: Apr 2008
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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
|
|

May 19th, 2008, 10:36 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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 -/
|
|

May 19th, 2008, 11:20 AM
|
|
Authorized User
|
|
Join Date: Apr 2008
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Thank you for your time and sorry for confusing you.
Thank you
|
|
 |