LinkButton is not properly triggered - asp.net

I'm going crazy about making this code to work. I got a simple link button that I add to a panel. When I click on the link, it's like something is trigger (whole page refresh and my link button disappear) but when I debug it, I never enter my DownloadItems function.
LinkButton linkTest = new LinkButton();
linkTest.ID = "ID";
linkTest.Click += new EventHandler(DownloadItems);
linkTest.Text = "Test";
In another page, everything is working great but I can't put my finger on it :s

Related

ASP.NET Refresh Parent Page after Closing Modal Dialog Window

I have an ASP.NET page that launches a modal dialog window:
Dim sURL As String = System.Configuration.ConfigurationManager.AppSettings("PAYORS_Path") & "PayorCopy.aspx"
lnkCopy.Attributes.Add("onclick", "javascript:window.showModalDialog('" & sURL & "',null,'status:no;dialogWidth:375px;dialogHeight:550px;dialogHide:true;help:no;scroll:yes;center:yes');return false;")
The user can create new items in this modal window. When the user clicks the Close button the modal dialog window is successfully closed, but the new items created do not appear on the parent page. When the modal window is closed I would like the parent page to refresh to show the new items the user created. Currently:
Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnClose.Click
Dim strjscript As String = "<script language='javascript'>self.close();</script>"
LtClose.Text = strjscript
End Sub
I have tried adding to this script:
window.opener.location.reload(true);
But then when I test I get an error that says "Unable to get value of the property 'location': object is null or undefined."
Any help would be much appreciated!
Instead of using the old modal dialog window, try jQuery Dialog because it behaves just like a modal but keeps you in the same window. Therefore it's is easily refreshed with the following code:
location.reload(true);
For more information, check out this question here at SO.
Hope it helps.
That would be because there is no 'opener' :)
Location is method of the window object so you can even treat it as global is most cases. Any of those should work:
window.location.reload(true);
or:
location.reload(true);
EDIT Yeah I just noticed where you put that script.. if it's in the click handle inside the modal..then 'window' there is really a different one - it's modal after all! Try adding it directly after the code opening modal window - the script execution will be blocked until the modal returns, so you can so something like this:
var shouldReloadPage = window.showModalDialog('" & sURL & "',null,'status:no;dialogWidth:375px;dialogHeight:550px;dialogHide:true;help:no;scroll:yes;center:yes');
if(shouldReloadPage){
window.location.reload(true);
}
return false;
This is still inside the click handler ofc, just showing in code block for easier reading.

My button click is working only the first time

I have a classic ASP NET page on my SharePoint site with several buttons in it:
Button importZIPPOTBtn = new Button();
importZIPPOTBtn.Click += new EventHandler(importZIPPOTBtn_Click);
this.Controls.Add(new LiteralControl("<br/><br/>"));
this.Controls.Add(importZIPPOTBtn);
The first click on any button is perfectly firing the event, but any further click on any button doesn't fire, I can't understand why...
Consider aadding button to form1 or container element like panel inside page.
Button importZIPPOTBtn = new Button();
importZIPPOTBtn.Click += new EventHandler(importZIPPOTBtn_Click);
this.form1.Controls.Add(new LiteralControl("<br/><br/>"));
this.form1.Controls.Add(importZIPPOTBtn);
You can verfiy in html source, unless you add this way even literal control containing lines breaks will not be rendered.

MultiView & MaintainScrollPositionOnPostBack

I have a page with a MultiView control, and some of the views are long enough to scroll. Since may of the controls in the reviews require postback to function properly, MaintainScrollPositionOnPostBack is enabled on the page.
I have a problem when the user transitions from one view to another. If they were at the bottom of a long view, and transition to another long view, the new view loads and is scrolled all the way to the bottom. I need to jump to the top of the page when the user goes to a new view within the MultiView.
I've tried using the OnActiveViewChanged event to:
- call RegisterStartupScript to set window.location.hash to an anchor that I placed at the top of the page.
- call RegisterStartupScript to call window.scrollTo(0,0)
- set MaintainScrollPositionOnPostBack to false temporarily
The problem is that none of these seem to affect the actual transition postback, they take effect on the next postback, which actually causes a bigger issue.
Anybody have a proven method to have a MultiView page jump to top of page only on postbacks that transition to a new view?
This is exactly the same problem I've been having today with a multiview.. I found your question and went looking for answers. Seems we found the same article!
(Article code in C#)
private void ResetScrollPosition()
{
if (!ClientScript.IsClientScriptBlockRegistered(this.GetType(), "CreateResetScrollPosition"))
{
System.Text.StringBuilder script = new System.Text.StringBuilder();
script.Append("function ResetScrollPosition() {");
script.Append(" var scrollX = document.getElementById(\'__SCROLLPOSITIONX\');");
script.Append(" var scrollY = document.getElementById(\'__SCROLLPOSITIONY\');");
script.Append(" if (scrollX && scrollY) {");
script.Append(" scrollX.value = 0;");
script.Append(" scrollY.value = 0;");
script.Append(" }");
script.Append("}");
//Create the ResetScrollPosition() function
ClientScript.RegisterClientScriptBlock(this.GetType(), "CreateResetScrollPosition",
script.ToString(), true);
//Add the call to the ResetScrollPosition() function
ClientScript.RegisterStartupScript(this.GetType(), "CallResetScrollPosition", "ResetScrollPosition();", true);
}
}
Found an answer/workaround, finally: 4Guys
You have to trick ASP.Net into doing it for you by manipulating the hidden fields it uses for tracking the scroll position.

Need a postback handle from a clicked dynamic hyperlink control in ajax

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.
If you give your link an onclick attribute, those javascript actions should also be executed when the link is selected.

ImageClickEvenHandler doesn't call handler

I have a dynamically created table and in some of the cells i have an image button associated with the redBall_Click() handler
(here is the code behind)
TableCell cellOK = new TableCell();
cellOK.Style.Add(HtmlTextWriterStyle.TextAlign, "Center");
cellOK.Width = new Unit("3%");
ImageButton redBall = new ImageButton();
redBall.CausesValidation = false;
redBall.ID = id;
redBall.ImageUrl = "~/App_Themes/DotRed.png";
redBall.Click += new ImageClickEventHandler(redBall_Click);
cellOK.Controls.Add(redBall);
My problem is that the redBall_Click() method is never called (neither after the PostBack)
How can i solve this?
P.S. : I can't use a static link because every ImageButton is associated with a specific ID that i must pass to the page i call (for example as a Session object)
You have to set up the event handler within OnInt and do it every time the page is created, even during a postback. Make sure you don't have the code within a block like:
if(!this.IsPostback)
{
...
}
Have you tried putting a breakpoint in the page load method before clicking the image? I suspect this might show that the postback is happening, even if the image click handler isn't getting fired.
Is the code to create the image button also invoked during a postback? If not, the page will postback, but the image button won't exist to invoke the click handler. Sounds bizarre, but it's caught me out a few times.
I finally resolved (just yesterday).
I guess I was building my buttons too late: in the Page_PreRender (after the event was to be fired) maybe if i put the creation in Page_Load (yes, I create everything also on PostBack) it would handle the click, but I think I won't try since I found a workaround.
I'll explain for anyone who could have the same problem:
private TableCell cellOK(string id)
{
TableCell cellOK = new TableCell();
cellOK.Style.Add(HtmlTextWriterStyle.TextAlign, "Center");
Image redBall = new Image();
redBall.ID = id; // useless
redBall.ImageUrl = "redball.gif";
HyperLink hl = new HyperLink();
hl.Controls.Add(redBall);
hl.NavigateUrl = MyPage + "?id=" + id;
cellOK.Controls.Add(hl);
return cellOK;
}
and in Page_Init()
string m_QueryStringId = Request.QueryString.Get("id");
if (!string.IsNullOrEmpty(m_QueryStringId))
{
// Do something
}

Resources