• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 926
  • Last Modified:

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 />
0
Drizzt95
Asked:
Drizzt95
  • 4
  • 3
1 Solution
 
jabcocoCommented:
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

0
 
Drizzt95Author Commented:
Thanks, but how do I specify the EditImageUrl value at runtime?
0
 
Drizzt95Author Commented:
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

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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

0
 
Drizzt95Author Commented:
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?
0
 
jabcocoCommented:
Well you can use "(ImageButton)e.Row.Cells[1].Controls[ctrlIndex]" and change the controls index.

You will also have to check condition with "e.row.RowState" to check what ever is a "Insert/Edit/" row, because when using the commandfield column only the available column will be generate... (so in read mode the "update button" will not show.
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ImageButton oFld = (ImageButton)e.Row.Cells[1].Controls[0];
                oFld.ImageUrl = "img.jpg";
                oFld = (ImageButton)e.Row.Cells[1].Controls[2];//Literal control separate the 2 button
                oFld.ImageUrl = "img.jpg";
            }
            else if (e.Row.RowState == DataControlRowState.Edit)
            {
 .....
            }

Open in new window

0
 
Drizzt95Author Commented:
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.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now