Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGrid formatting question

Status
Not open for further replies.

Naoise

Programmer
Dec 23, 2004
318
IE
I have a datagrid which uses an TemplateColumn due to formatting restrictions. Inside this I wish to have
an image link that when clicked fires a sub() and passes in the particular MyID for that record. Usually
I can achieve this with the <asp:EditCommandColumn> and setting the OnEditCommand of the DataGrid but
I cannot here as I need the particular image link where it is. Can anyone suggest how I might accomplish
this?

<asp:DataGrid id="dgDataGrid" DataKeyField="MyID">
<asp:TemplateColumn>
<ItemTemplate>
<table>
...
<tr>
<td>
<a href="y.aspx" id="lnkClickMe" onserverclick="lnkClickMe_Click"><img src="images/x.gif" /> click me</a>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
 
are you using .net 1.1?

I use the RowDataBound method on gridviews ( same can be used for the datagrid (i think!))

On the designer, select the grid and in the properties hit enter on the rowdatabound method, and this will create the method in the code behind file.

If you put an asp:image , you can search for the control in the row and then assign the url to it, using the roweventargs variable to find the control.
 
No RowDataBound for DataGrids unfortunately.
 
I don't see how ItemDataBound can help me add the DataKeyField to the onserverclick event of this line?

<a href="y.aspx" id="lnkClickMe" onserverclick="lnkClickMe_Click"><img src="images/x.gif" /> click me</a>

If I set the OnItemDataBound attribute of the DataGrid to SetIDs ike this...

Code:
Sub SetIDs(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)

        CType(e.Item.FindControl("lnkClickMe"), System.Web.UI.HtmlControls.HtmlAnchor).Attributes.Add("OnServerClick", e.Item.DataItem("MyID"))

End Sub

How do I retrieve the value??

Code:
Sub lnkClickMe_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Response.Write(???)
    End Sub
 
I can achieve this with the <asp:EditCommandColumn> and setting the OnEditCommand of the DataGrid but
I cannot here as I need the particular image link where it is
.
Can you explain what you mean by this further? I think you may still be able to use this method and simply place the link to the image in the EditText property unless I've mis-understood what you are trying to do.


------------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
 
I meant that usually I can pass the ID's I need to a code behind function using <asp:EditCommandColumn> and setting the OnEditCommand of the DataGrid but here I cannot nest <asp:EditCommandColumn> within this <asp:TemplateColumn> tag and there are strict display instructions with the design. How and ever I have achieved what I need to by nesting a <asp:ImageButton> tag within the TemplateColumn.

Code:
<asp:ImageButton id="btnClickMe" CommandArgument=<%#DataBinder.Eval(Container.DataItem, "MyID")%> OnCommand="btnClickMe_Click" runat="server" />

Sub btnClickMe_Click(ByVal sender As System.Object, ByVal e As CommandEventArgs)
     Response.Write(e.CommandArgument)        
End Sub
 
this is prob the same/similar in VB.net but here is what i use in c#. Note this is for a gridview, as datagrids are obselete in .net 2.0

here goes..

Code:
// for each row or item that gets bound you wil
    protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
Hyperlink HpYourLink = (HyperLink)e.Row.findcontrol("lnkClickMe");
//now you can add all the properties you want

HpYourLink.NavigateUrl = "[URL unfurl="true"]http://www.tek-tips.com";[/URL]
ETC

}
it this you can search for any control that is rendered by the object. etc.

you can then use the eventargs that are passed from the button to find the that you are in to determine what you want to do etc. e.RowIndex.
 
Hi,

I'd use an ASP:Hyperlink in the template column with the text set to be your image.

<asp:Hyperlink id="lnkClickMe" Runat="Server" />

Code:
Sub dgDataGrid_ItemDataBound(ByVal sources As Object, ByVal e As DataGridItemEventArgs) Handles dgDataGrid.ItemDataBound

        If (e.Item.ItemType = ListItemType.Item Or _
 e.Item.ItemType = ListItemType.AlternatingItem) Then


            Dim keyValue As Integer = (dgDataGrid.DataKeys(e.Item.ItemIndex)) ‘Brings back the ID

           
                Dim _lnkClickMe As HyperLink

                _lnkClickMe = e.Item.FindControl("lnkClickMe")

		_lnkClickMe.Text="images/x.gif"

                _lnkClickMe.NavigateUrl = "y.aspx?&ID=" & keyValue & ""

           
                End If

        End Sub

HTH

Jon


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top