Jonas Stawski

Everything .NET and More

More Control Over GridView Column

In response to the many comments/questions regarding the post on GridView, HyperLinkField, and JavaScript what the #%$@! I decided to extend it and show how you can have total control over a column with the GridView.

Whenever you need to do something that is not out of the box with the GridView, like adding RadioButtons, Adding JavaScript, etc you can use the <asp:TemplateField> In there you have total control as far as what you want to insert on that column. Following the issue with the JavaScript from my other post there is an example on how to do achieve the same results:
<asp:TemplateField>
  <ItemTemplate>
    <%#"<a href=""javascript:MM_openBrWindow('../images/elevations/" & Eval("Column1") & "','img" & Eval("Column2") & "','');"">" & Eval("Column1") & "</a>"%>
  </ItemTemplate>
</asp:TemplateField>

Comments (16) -

After a couple of hours searching for a solution, this finally did the trick! I really got stuck with passing two variables into a javascript function, but your solution worked (although I replaced the double double quotation marks with one single quotation mark)! Thanx!

Reply

Thank You Very MUCH!!!

Reply

Try this out. It gives you more flexibility to format the data to be displayed in the TemplateField.

=>  Declare a function in the page that handles the formatting:

    public string FmtLink(string prod_code, int prod_id)
    {
        return "javascript:window.opener.test_click('"
+ prod_code + "','"
+ prod_id.ToString() + "')";
    }

=>  Then, add a TemplateField to the grid and pass in the necessary fields.

<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" Text='<%# Eval("prod_code") %>'
NavigateUrl='<%# FmtLink( (string)Eval("prod_code"),(int)Eval("prod_id") ) %>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>

Reply

Hey I am using the GridView in the manner described by your "GridView, HyperLinkField, and JavaScript what the #%$@!" post, and I would like to pass a certain value - namely the projectt_id of the projectt name which is clicked on - into the child window in order to populate a bunch of fields to be edited in said window. I am relatively wet behind the ears using ASP.NET, so any help you may provide as well as suggestions on how to handle this type of request would be greatly appreciated. Here is the GridView in question:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="misProjects" DataKeyNames="mis_project_id" Height="512px" Width="1024px" HorizontalAlign="Center"
         BorderColor="Transparent" BorderStyle="None" ForeColor="Transparent" AllowSorting="True" CellPadding="10" HeaderStyle-Font-Size="Large" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" AutoGenerateEditButton="True"
          OnRowUpdating="GridView1_RowUpdating" OnRowDataBound="GridView1_RowDataBound">
            <Columns>
                <asp:BoundField DataFormatString="&lt;a href=javascript:MM_openBrWindow('editMisProjects.aspx?='+(mis_project_id),'projects','scrollbars=no,resizable=no,width=640,height=490');&gt;{0}&lt;/a&gt;"
                 DataField="mis_project_title_txt" HeaderImageUrl="~/images/projectID2.jpg" SortExpression="mis_project_title_txt" ReadOnly="True">
                    <ControlStyle Font-Bold="True" />
                    <ItemStyle HorizontalAlign="Left" Wrap="False" Font-Bold="True" />
                    <HeaderStyle HorizontalAlign="Center" />
                </asp:BoundField>
                <asp:BoundField DataField="mis_project_id" Visible="False" ShowHeader="False" />
</asp:BoundField>
                </Columns></asp:GridView>

Reply

Any idea on the syntax for making this work with a dynamically generate TenplateField?

Reply

Jerry,
    I don't know what you mean.

Reply

mwmartin,
     Why don't you try what is suggested in this article. You can see how to use multiple values using the eval function.

Reply

Thank you very much. I modify it a bit

<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID"
                             DataFormatString="&lt;a href='#' onclick=javascript:window.open('StatusDetail.aspx?id='+{0},'projects','scrollbars=no,resizable=yes,width=739,height=265',false); &gt;{0}&lt;/a&gt;">
                            <HeaderStyle CssClass="statusHeader" HorizontalAlign="Center"    />
                            <ItemStyle HorizontalAlign="Center" VerticalAlign="Top" Width="50px" />
                        </asp:BoundField>

Reply

Is this possible in codebehind page/ dynamically?

Reply

Yes, you can either call a function from the template field by doing something like this <%# FunctionName() %> and FunctionName returns a string. Or you can implement the RowDataBound event of the GridView and you will be able to modify your rows, cells, and or controls.

Reply

Thanks so much; I really needed this one!

Reply

great! thanks!

Reply

I can't seem to figure out how to pass the parameter WOID to the aspx page.  I get the literal {0} on the other side.  I've tried numerous different ways.
Thanks in advance.
Phil

           <asp:TemplateField HeaderText="Suppress">
                <ItemTemplate>
                    <%#"<a href=javascript:openPopup2('SuppressPopup.aspx?WOID={0}');>"+ DataBinder.Eval(Container.DataItem,"Suppress").ToString() + "" %>
                </ItemTemplate>
            </asp:TemplateField>
            

Reply

I think I"m close, but I just can't get the quotes right, I think.  One big problem, I think, is I didn't realize the examples above are VB.Net and I'm using C#,
which means change the amps to plus.

I get 3 compile errors....
) expected
; expected
Invalid expression term ','

<asp:TemplateField>
  <ItemTemplate>
    <%#"<a href=""javascript:openPopup2('SuppressPopup.aspx?WOID="+DataBinder.Eval(Container.DataItem, "Suppress").ToString()+"');"">"""%>
  </ItemTemplate>                                                                                                                        
</asp:TemplateField>

Well, one more hour and it's weekend for me.  Hopefully, the coding guru's will shine down on me before Monday.

Reply

my code as follows;
<asp:TemplateField>
                     <ItemTemplate>
    <%#"<a href=""javascript:get_id(" & Eval("this_id") & ");"">" & Eval("this_id") & ""%>
  </ItemTemplate>

it works fine except i don't want it to reload in a new page ... as my script returns false value.  Above function call will load a new page with 'False' text displayed.  How do I make the script runs without postback?

Reply

fryzjer warszawa
United States fryzjer warszawa

Great blog! i love it Smile !

Reply

Add comment

biuquote
Loading