Manage a GridView's HyperLinkField column's width? [modified]
-
Scenario: I was using Template columns with ImageButtons with no problem controlling the columns' width. However template columns are not allowed when 'EnableSortingAndPagingCallbacks="True"'. Original html code:
<asp:TemplateField ItemStyle-Width="22px">
<ItemTemplate>
<asp:ImageButton ID="imbView" runat="server" ImageUrl="~/Images/ViewSearch.gif" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="22px">
<ItemTemplate>
<asp:ImageButton ID="imgEdit" runat="server" ImageUrl="~/Images/Edit.gif" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="22px">
<ItemTemplate>
<asp:ImageButton ID="imgDelete" runat="server" ImageUrl="~/Images/Delete.gif" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="22px">
<ItemTemplate>
<asp:ImageButton ID="imgPrint" runat="server" ImageUrl="~/Images/Print.gif" />
</ItemTemplate>
</asp:TemplateField>So I replaced the template columns with HyperLinkField. Then in the onRowDataBound event in the code-behind, I set the images to the respective links.
</asp:HyperLinkField DataNavigateUrlFormatString="~/Invoices/Invoice.aspx?id={0}&Readonly" DataNavigateUrlFields="InvoiceID" ItemStyle-Width="20px" />
</asp:HyperLinkField DataNavigateUrlFormatString="~/Invoices/Invoice.aspx?id={0}&Edit" DataNavigateUrlFields="InvoiceID" />
</asp:HyperLinkField DataNavigateUrlFormatString="~/Reports/Reports.aspx?DeleteFromSomeWhere" DataNavigateUrlFields="InvoiceID" />
</asp:HyperLinkField DataNavigateUrlFormatString="~/Reports/Reports.aspx?id={0}" DataNavigateUrlFields="InvoiceID" />public void dgData\_onRowDataBound(Object src, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { DataRowView row = (DataRowView)e.Row.DataItem; HyperLink link = (HyperLink)e.Row.Cells\[0\].Controls\[0\]; if (!(link == null)) { link.ImageUrl = "~/Images/ViewSearch.gif"; link.ToolTip = "Print"; } link = (HyperLink)e.Row.Cells\[1\].Controls\[0\]; if (!(link == null)) { link.ImageUrl = "~/Images/Edit.gif"; link.ToolTip = "Print"; } link = (HyperLink)e.Row.Cells\[2\].Controls\[0\]; if (!(link == null)) { link.ImageUrl = "~/Images/Delete.gif"; link.ToolTip = "Delete Invoice
-
Scenario: I was using Template columns with ImageButtons with no problem controlling the columns' width. However template columns are not allowed when 'EnableSortingAndPagingCallbacks="True"'. Original html code:
<asp:TemplateField ItemStyle-Width="22px">
<ItemTemplate>
<asp:ImageButton ID="imbView" runat="server" ImageUrl="~/Images/ViewSearch.gif" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="22px">
<ItemTemplate>
<asp:ImageButton ID="imgEdit" runat="server" ImageUrl="~/Images/Edit.gif" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="22px">
<ItemTemplate>
<asp:ImageButton ID="imgDelete" runat="server" ImageUrl="~/Images/Delete.gif" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="22px">
<ItemTemplate>
<asp:ImageButton ID="imgPrint" runat="server" ImageUrl="~/Images/Print.gif" />
</ItemTemplate>
</asp:TemplateField>So I replaced the template columns with HyperLinkField. Then in the onRowDataBound event in the code-behind, I set the images to the respective links.
</asp:HyperLinkField DataNavigateUrlFormatString="~/Invoices/Invoice.aspx?id={0}&Readonly" DataNavigateUrlFields="InvoiceID" ItemStyle-Width="20px" />
</asp:HyperLinkField DataNavigateUrlFormatString="~/Invoices/Invoice.aspx?id={0}&Edit" DataNavigateUrlFields="InvoiceID" />
</asp:HyperLinkField DataNavigateUrlFormatString="~/Reports/Reports.aspx?DeleteFromSomeWhere" DataNavigateUrlFields="InvoiceID" />
</asp:HyperLinkField DataNavigateUrlFormatString="~/Reports/Reports.aspx?id={0}" DataNavigateUrlFields="InvoiceID" />public void dgData\_onRowDataBound(Object src, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { DataRowView row = (DataRowView)e.Row.DataItem; HyperLink link = (HyperLink)e.Row.Cells\[0\].Controls\[0\]; if (!(link == null)) { link.ImageUrl = "~/Images/ViewSearch.gif"; link.ToolTip = "Print"; } link = (HyperLink)e.Row.Cells\[1\].Controls\[0\]; if (!(link == null)) { link.ImageUrl = "~/Images/Edit.gif"; link.ToolTip = "Print"; } link = (HyperLink)e.Row.Cells\[2\].Controls\[0\]; if (!(link == null)) { link.ImageUrl = "~/Images/Delete.gif"; link.ToolTip = "Delete Invoice
Hi Michael, There are a couple of ways which you can try: + Use Atlas with your GridView control, you can keep using the TemplateField and simply place the GridView control inside the
UpdatePanel
to support client callback with paging and sorting. + Use the ButtonField withButtonType="Image"
:<asp:ButtonField ItemStyle-Width="22px" ButtonType="Image" ImageUrl="..." />
+ Use the BoundField with adding the ImageButton to the column in the RowDataBound event handler:
<asp:BoundField ItemStyle-Width="22px" />
-
Hi Michael, There are a couple of ways which you can try: + Use Atlas with your GridView control, you can keep using the TemplateField and simply place the GridView control inside the
UpdatePanel
to support client callback with paging and sorting. + Use the ButtonField withButtonType="Image"
:<asp:ButtonField ItemStyle-Width="22px" ButtonType="Image" ImageUrl="..." />
+ Use the BoundField with adding the ImageButton to the column in the RowDataBound event handler:
<asp:BoundField ItemStyle-Width="22px" />
I ended up going with the Atlas:UpdatePanel. Thanks! I firmly believe that any man's finest hour, the greatest fulfillment of all that he holds dear, is that moment when he has worked his heart out in a good cause and lies exhausted on the field of battle - victorious. Vince Lombardi (1913-1970)