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="gvwTreatmentPlanDetail s" runat="server" DataKeyNames="intTreatment ID" />
<Columns>
<asp:CommandField EditImageUrl=??? ButtonType="Link" EditText="Edit" DeleteText="Delete" CancelText="Cancel" UpdateText="Update" ShowEditButton="true" ShowDeleteButton=true />
<asp:GridView ID="gvwTreatmentPlanDetail
<Columns>
<asp:CommandField EditImageUrl=??? ButtonType="Link" EditText="Edit" DeleteText="Delete" CancelText="Cancel" UpdateText="Update" ShowEditButton="true" ShowDeleteButton=true />
ASKER
Thanks, but how do I specify the EditImageUrl value at runtime?
ASKER
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(Co ntainer.Da taItem, "strTreatmentType")%>>
</asp:Label>
</ItemTemplate>
<asp:TemplateField HeaderText="Treatment Type">
<ItemTemplate>
<asp:Label ID="lblTreatmentType" runat="server" Text=<%#DataBinder.Eval(Co
</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)
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.
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";
}
}
ASKER
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
Let me know.
Open in new window