2

I am stuck on something: I am creating a hyperlink at runtime that has a navigation URL. I need to define its click event so that I can save a few values to the database. I did something like below but without success.

Could you please suggest an alternative?

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) {
  if (e.Item is GridDataItem) {
    HyperLink link = (HyperLink)gridDataItem["ContentTitle"].Controls[0];
    link.ForeColor = System.Drawing.Color.Navy;
    link.ToolTip = Common.grdTextCell(gridDataItem["ContentSummaryDescr"].Text);
    link.NavigateUrl = "~/SomePath/" + gridDataItem["ContentName"].Text;
    link.Target = "_blank";
    link.Attributes.Add("onclick", "document.getElementById('" +
      dummyBtn.ClientID + "').click();");
  }
}

protected void dummyBtn_Click(object sender, EventArgs e) {
}

But the button click event is not firing, it simply navigates to the URL. What to do please?

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
NoviceToDotNet
  • 10,387
  • 36
  • 112
  • 166

3 Answers3

3

For a server side event to fire you would need a LinkButton and not a HyperLink

LinkButton has a Click event handler which you can use.

HyperLink only redirects and has no corresponding Click event handler associated for server side code

V4Vendetta
  • 37,194
  • 9
  • 78
  • 82
  • yes that's y i can't use LinkBUtton..the above code is implemented and the click event is firing now, but now i am unable to fix on which row is called so that i can insert the values....and unfortunately my RadGrid1_ItemDataBound also not firing, i am so stuck..........help me god.. – NoviceToDotNet Jul 13 '12 at 11:07
1

You want a LinkButton, not a HyperLink.

Here's some sample code to get you started (not tested)

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        LinkButton link = (LinkButton)gridDataItem["ContentTitle"].Controls[0];
        link.Click += dummyBtn_Click;
    }
}

protected void dummyBtn_Click(object sender, EventArgs e)
{
    Response.Write("dummyBtn_Click");
}
royse41
  • 2,310
  • 4
  • 22
  • 29
1

You should be using Link Button. Just replace your Hyperlink with LinkButton in your code.It should work.

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) {
  if (e.Item is GridDataItem) {
    LinkButton link = (LinkButton )gridDataItem["ContentTitle"].Controls[0];
    link.ForeColor = System.Drawing.Color.Navy;
    link.ToolTip = Common.grdTextCell(gridDataItem["ContentSummaryDescr"].Text);
    link.NavigateUrl = "~/SomePath/" + gridDataItem["ContentName"].Text;
    link.Target = "_blank";
    link.Click += dummyBtn_Click;

  }
}

protected void dummyBtn_Click(object sender, EventArgs e) {
}
Peru
  • 2,871
  • 5
  • 37
  • 66