views:

1186

answers:

1

The scenario...I have a list of dynamic hyperlink controls on an ajax panel that link directly to a WORD doc located on a share. Clicking any link opens WORD client side (I set the file association to do that so the IE browser doesn't try to open it instead).

LinkButtons won't work because it doesn't do a direct link to the doc from the browser and I don't want server automation or activex, since I want each client to open the doc using their own box as if they had simply clicked on the document itself.

But I need to change an image in an image control once they've clicked on the hyperlink. In other words, I need the link to sit on top of a control that does cause a postback so I can get at the posting controls ID and do my thing. I'm also trying to avoid client side scripting though I'm sure there may be a convoluted way to do that.

Here is the code in the loop that creates the dynamic link. I started with just the hyperlink control, then this code is messing with adding a hyperlink to a label, that's why it's showing as it is:

Label lblWordLink = new Label();
HyperLink hrefLetter = new HyperLink();
hrefLetter.Text = items.letterName;                
hrefLetter.NavigateUrl = folderForPackageLetters + items.letterName + wordDocExtension;
hrefLetter.ID = "standardLettersHref_" + items.letterName;
lblWordLink.Text = "<a href='" + hrefLetter.NavigateUrl.ToString() + "'>" + items.letterName + "</a>" ;                
tRow.Cells[1].Controls.Add(lblWordLink);

I'm looking for a way to let the link open the doc and at the same time postback. Is there a way to layer the link control on top of another control such that the link just links and the control beneath causes a postback?

32U

Fixed: the answer gave the clue. On the server, during dynamic control creation I did:

HyperLink hrefLetter = new HyperLink();
hrefLetter.ID = "standardLettersHref_" + items.letterName;
hrefLetter.Text = items.letterName;                
hrefLetter.NavigateUrl = folderForPackageLetters + items.letterName + wordDocExtension;                               
hrefLetter.Attributes.Add("OnClick", "letterHrefClick('" + items.letterName + wordDocExtension + "')");
tRow.Cells[1].Controls.Add(hrefLetter);

then client side I pushed a value into a hidden control inside the ajax panel when the hyperlink control was clicked and forced submit:

function letterHrefClick(link) {
  //alert(link);
  form1.hdnLetterClick.value = link;
  form1.submit();
}

After the submit, back on server side, I got the value in the Page_Load event:

 string x = hdnLetterClick.Value;

nice!

update... An even better way to do this... in the javascript postback use:

__doPostBack("hdnLetterClick", "somevalue");

then in the codebehind in the Page_Init you can do (not for button or imgbutton):

string postbackControlID = Request.Params.Get("__EVENTTARGET");
string postbackArgument = Request.Params.Get"__EVENTARGUMENT");

to get at what you need. This is better if dealing with dynamic controls so you can control state during control recreation.

+1  A: 

If you give your link an onclick attribute, those javascript actions should also be executed when the link is selected.

Eric Nguyen
I did that but need to get at the value set client side in server code.
32U
Yeah, you are right. I messed it up the first time. I'll put the code snips in my original question. Thanks.
32U