|
-
Sep 16th, 2014, 09:18 AM
#1
[RESOLVED] Setting Image Button URL in code behind
I thought this would be a simple issue, but I've been struggling. I created an image button in my header template. When clicked, it calls the Sorting method. When I change the image url in code behind, it does not render. By setting a break point, I can see the property being updated, but I assume it is getting overridden by the default I set in the HTML page.
Code:
<HeaderTemplate>
<span>Claim Id</span>
<asp:ImageButton ID="btnClaimIdSort" runat="server" ImageUrl="~/Styles/UpArrow.ico" OnClick="Sorting" ToolTip="ClaimId" />
</HeaderTemplate>
Code:
protected void Sorting(object sender, EventArgs e)
{
var sortButton = (ImageButton)sender;
// parse new sort
var newField = sortButton.ToolTip;
// parse existing sort
var existingSortArgs = this.ClaimSort.Split(' ');
var existingField = existingSortArgs[0];
var existingDirection = existingSortArgs[1];
if (string.Compare(newField, existingField,true) == 0)
{
// swap direction
var newDirection = (existingDirection == "Asc" ? "Desc" : "Asc");
sortButton.ImageUrl = (newDirection == "Asc" ? "~/Styles/DownArrow.ico" : "~/Styles/UpArrow.ico");
this.ClaimSort = string.Format("{0} {1}", existingField, newDirection);
}
else
{
sortButton.ImageUrl = "~/Styles/UpArrow.ico";
this.ClaimSort = string.Format("{0} Asc", newField);
}
// rebind data
this.BindResultsGrid();
}
That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma
Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney
-
Sep 16th, 2014, 11:33 AM
#2
Re: Setting Image Button URL in code behind
Hi,
Normally, the image doesn't get updated on your sorting event because you rebind your Gridview control with new datasource.
Hence, the control has been re-initialized.
Try updating the image url through your GridView RowDataBound event.
Code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
ImageButton sortButton = (ImageButton)e.Row.FindControl("btnClaimIdSort");
//Modify code below with your logic.
if (sortButton != null)
{
if (sortdescription =="ASC")
{
sortButton.ImageUrl = "~/Styles/UpArrow.ico";
}
else
sortButton.ImageUrl = "~/Styles/DownArrow.ico";
}
}
}
KGC
-
Sep 16th, 2014, 12:24 PM
#3
Re: Setting Image Button URL in code behind
That makes total sense, and your solution works like a charm. Thank you.
That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma
Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|