C# Hit hyperlink and send a column value to the code behind.

  • Thread starter Thread starter Pat
  • Start date Start date
P

Pat

All,

What I want to do:
*******************
Click on a hyperlink in the last column in a datagrid, have it grab a
value in the fourth column in the same row and send it to the codehind
into a function that will then put the value into a query and send it
off the database.... and do some stuff with the return.

What I have so far:
********************
I have the first part of the program done, the datagrid is populated
and the last column has the hyperlink.

What code I have:
******************
In the .ASPX
=============
<asp:TemplateColumn>
<ItemTemplate>
<asp:HyperLink id="HyperLink2" Runat="server"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateColumn>



In the code behind - In the Page Load
======================================
if(Page.Request["Report"]!=null)
ShowReport();



In the code behind
====================
private void grdReceipts_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if((e.Item.ItemType == ListItemType.Item)|| (e.Item.ItemType ==
ListItemType.AlternatingItem))
{
HyperLink c=(HyperLink)e.Item.FindControl ("HyperLink2");
c.Text="Report" ;
c.NavigateUrl='Javascript:document.getElementById("hidCurrentTab").value
= "Detail"; window.open('ItemAudit.aspx?Report=True', 'Report', '')'
}
}


Any help is appreciated.

Thanks,
Pat G.
 
Pat,
How about using an asp:LinkButton instead of the hyperlink.

You could do something like this:

protected void dgIrResources_OnItemCreated(Object sender,
DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
LinkButton button = (LinkButton ) e.Item.Cells[0].Controls[0];
button.Attributes.Add("onclick",
"return confirm(\"Are you sure you want to delete this document? You
cannot submit an Invoice Request to Accounting without an attached
Reconciliation Document\");");
}
}

What I'm doing is using .Attributes.Add to add a javascript confirm to the
onclick event of the LinkButton.
You could use a stringbuilding to build your javascript

StringBuilder sb = new StringBuilder();

sb.Append ( blah blah);

then do:

button.Attributes.Add("onclick", sb.ToString());
 
Back
Top