Link to home
Start Free TrialLog in
Avatar of Drizzt95
Drizzt95

asked on

Update EditImageUrl at Runtime in Datagrid

I have a datagrid with a commandfield for editing or deleting rows.  I'd like to be able to change the EditImageUrl at runtime but can't figure out how:

 <asp:GridView ID="gvwTreatmentPlanDetails" runat="server" DataKeyNames="intTreatmentID" />
                   <Columns>
                         <asp:CommandField EditImageUrl=??? ButtonType="Link" EditText="Edit" DeleteText="Delete" CancelText="Cancel" UpdateText="Update" ShowEditButton="true" ShowDeleteButton=true />
Avatar of jabcoco
jabcoco
Flag of Canada image

First if you want a "image type button" for edit action and a "link type button" for a delete action you cannot use the 1 CommandField  statement. You will have to split in 2 column just like my example. Also you have to specify the "ButtonType" to be "Image" in order the be able using the "EditImageUrl".

Let me know.
<asp:CommandField EditImageUrl="img.jpg" ButtonType="Image" EditText="Edit" ShowEditButton="true" />
<asp:CommandField ButtonType="Link" EditText="Delete" ShowDeleteButton="true"/>

Open in new window

Avatar of Drizzt95
Drizzt95

ASKER

Thanks, but how do I specify the EditImageUrl value at runtime?
I've come up with the code before, which adds the image at runtime nicely, except it removes any text from  any columns that are setup as a template field:

<asp:TemplateField HeaderText="Treatment Type">
                            <ItemTemplate>
                            <asp:Label ID="lblTreatmentType" runat="server" Text=<%#DataBinder.Eval(Container.DataItem, "strTreatmentType")%>>
                                                        </asp:Label>
                            </ItemTemplate>
Dim ctrlfld As New CommandField
                ctrlfld.DeleteText = "<image src='" & arrImgDelBtnAttrs(0) & "' alt='" & arrImgDelBtnAttrs(1) & "'/>"
                ctrlfld.ShowDeleteButton = True
                ctrlfld.EditText = "<image src='" & arrImgEditBtnAttrs(0) & "' alt='" & arrImgEditBtnAttrs(1) & "'/>"
                ctrlfld.ShowEditButton = True
                ctrlfld.CancelText = "<image src='" & arrCancelBtnAttrs(0) & "' alt='" & arrCancelBtnAttrs(1) & "'/>"
                gvwTreatmentPlanDetails.Columns.Insert(0, ctrlfld)

Open in new window

Sorry don't know nothing about VB.NET, but here is how you can do it with C#.
You will have to handle the RowDataBound event and from there get the column index "cast" has a "ImageButton" and et the "ImageUrl" property.

Let me know.
//ASPX
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
        <Columns>
            <asp:BoundField DataField="Plan" />
            <asp:CommandField ButtonType="Image" EditText="Edit" ShowEditButton="true" />
            <asp:CommandField ButtonType="Link" EditText="Delete" ShowDeleteButton="true"/>
        </Columns>
        </asp:GridView>
 
//Code behind
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{    
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
    ImageButton oFld = (ImageButton)e.Row.Cells[1].Controls[0];
    oFld.ImageUrl =  "img.jpg";
  }   
}

Open in new window

That's no probs, I can convert it ok.

I'm not sure if I'm missing something, but how do I specify a different Image for the delete button, the edit button, and the cancel when it is all a part of the one commandfield?
ASKER CERTIFIED SOLUTION
Avatar of jabcoco
jabcoco
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That's brilliant, thanks a lot for your help, it works perfectly.  I thought it had something to do with Cells.Controls but I never managed to get anywhere.