I am generating a linkbutton dynamically and setting its postbackurl by iterating over a collection the linkbutton shows the correct url when i hover mouse over it but when i click the linkbutton only three of intended five querystring parameters are passed.
I,m using Firefox
here's my code
foreach(var item in collection)
{
linkbutton b = new linkbutton();
linkbutton.postbackurl="somepage.aspx?" + "param1="+ item.val1 + "¶m2=" + item.val2
+ "¶m3=" + item.val3+ "¶m4=" + item.val4+ "¶m5="
+item.val5;
PlaceHolder1.Controls.Add(b);
}
however when i click the linkbutton browser opens
somepage.aspx?param1=val1¶m2=val2¶m5=val5
all values are present in the collection item
plus the browser ignores param3,4 in the link itself
:<
once again HELP!!!
For this scenario you just need an asp:HyperLink not need of a asp:LinkButton. There are some errors in your code. A more optimised / readable version will be this
foreach(var item in collection)
{
var btn = new HyperLink();
btn.NavigateUrl=String.Format("somepage.aspx?param1={0}¶m2={1}¶m3={2}¶m4={3}¶m5={4}",
item.val1,
item.val2,
item.val3,
item.val4,
item.val5);
PlaceHolder1.Controls.Add(btn);
}
Hope this helps.
Related
I have some ASP.NET code that has an SSRS report control on page. The web application is hosted on a different server than where SSRS is hosted. The problem I'm having is when I pull up the report in Internet Explorer the date picker control is not showing the calendar and if I try to pull up the report in Chrome the date picker control doesn't show up at all. If I type dates into the text boxes, the report works just fine but, we would really like to be able to use the date picker control.
Any ideas as to what could be wrong?
I believe this question to be different from those asked before because I am not only asking about non-IE browsers, but also asking about an issue with IE.
The date picker control does not display the calendar in IE when the user clicks on the control.
Wayne E. Pfeffer
------ Edit to add code example ------
The aspx code is:
<rsweb:ReportViewer ID="ReportViewer1" runat="server" ProcessingMode="Remote" Width="100%" Height="100%" AsyncRendering="False">
</rsweb:ReportViewer>
<asp:ScriptManager ID="ScriptManager1" runat="server">
There is a drop down list where a report is chosen and this is the code for loading the report into the report viewer:
protected void loadViewer(string report) {
ReportViewer1.ProcessingMode = ProcessingMode.Remote;
IReportServerCredentials irsc = new CustomReportCredentials(
ConfigurationManager.AppSettings["ReportUser"],
ConfigurationManager.AppSettings["ReportPswd"],
ConfigurationManager.AppSettings["ReportDomain"]);
ReportViewer1.ServerReport.ReportServerCredentials = irsc;
ReportViewer1.ServerReport.ReportServerUrl = new Uri(ConfigurationManager.AppSettings["ReportURL"]);
ReportViewer1.ServerReport.ReportPath = ConfigurationManager.AppSettings["ReportPath"] + report;
ReportViewer1.SizeToReportContent = true;
//Get the list of account IDs that the user has viewothertransactions at
List<string> vaIds = new List<string>();
string votAccts = (String)Session["votAccounts"];
string[] aIds = votAccts.Split(',');
foreach (var aId in aIds)
{
vaIds.Add(aId);
}
//Create the list of account ids where the user can only see its orders
List<DropdownOption> acclist = (List<DropdownOption>)Session["searchAccounts"];
string acctIds = "";
if (null != acclist)
{
for (int i = 0; i < acclist.Count; i++)
{
if (!vaIds.Contains(acclist[i].Id))
{
acctIds += acclist[i].Id + ",";
}
}
if (acctIds.Length > 0)
{
acctIds = acctIds.Substring(0, acctIds.Length - 1);
}
}
Users user = (Users) Session["userObject"];
ReportParameter userid = new ReportParameter("Userid", user.Id.ToString());
ReportParameter votAccounts = new ReportParameter("VotAccounts", votAccts);
ReportParameter accounts = new ReportParameter("Accounts", acctIds);
log.Debug("Requesting report '" + report + "'. Parameters - Userid=" + user.Id + " VotAccounts=" + votAccts + " Accounts=" + acctIds);
ReportViewer1.ServerReport.SetParameters(new ReportParameter[] { userid, votAccounts, accounts });
ReportViewer1.ServerReport.Refresh();
}
When using Internet Explorer 11 you could try Compatibility Mode .
Try pressing F12 whilst on the page to see what browser emulation is used.
I recently encountered this problem on IE and tracked down the issue to a javascript function registered by the report control. The javascript function adds a prototype method "Show" to the "DropDownParamClass". Inside the function is a line of code that checks for the presence of a "FORM" tag and if it's not there abandons the function...
// do not show the drop down if the frame has not yet been aded to the form tag
if (floatingIFrame.parentNode.tagName.toUpperCase() != "FORM")
return;
In my case the floatingIFrame.parentNode.tagName was returning a "DIV"... As a fix I've simply overwritten the DropDownParamClass.prototype.Show function with a slightly revised version (not the new explaining variable "readyToShowCalendar" which checks for either a DIV or a FROM tag), see below...
<script language="javascript" type="text/javascript">
if (typeof (window.DropDownParamClass) !== "undefined") {
//Fix for bug in report viewer control resulting in the Calendar Control not displaying when the calendar icon is clicked.
window.DropDownParamClass.prototype.Show = function () {
var floatingIFrame = document.getElementById(this.m_floatingIframeID);
// do not show the drop down if the frame has not yet been added to the form tag
var readyToShowCalendar = "FORM,DIV".indexOf(floatingIFrame.parentNode.tagName.toUpperCase()) > -1;
if (!readyToShowCalendar) return;
// position the drop down. This must be done before calling show base. Otherwise,
// a scroll bar is likely to appear as a result of showBase which would make the
// position invalid.
var newDropDownPosition = this.GetDropDownPosition();
floatingIFrame.style.left = newDropDownPosition.Left + "px";
floatingIFrame.style.top = newDropDownPosition.Top + "px";
this.ShowBase();
// poll for changes in screen position
this.StartPolling();
};
}
</script>
This script was then simply placed just underneath the initial tag on the page. After this the calendar control then seems to open as expected when clicking the calendar icon.
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.
I am building a dynamic user control (ascx) which contains a placeholder. Inside the placeholder, there will be n tables. Inside each table, in one of the cells i have the link button as below.
HyperLink lnkButton = new HyperLink();
lnkButton.ID = "lnkButton_" + ID.ToString();
lnkButton.Text = tstText;
lnkButton.NavigateUrl = "javascript:JS_Click();";
I have to call a JS function on the link button click (no postback).
In the JS function, i have to get the id of the control that invoked that JS function. In this case, it will be link button. I am writing the below code in the JS Function:
var ctrl = event.srcElement.id;
but i am getting 'Object Required' error in javaScript.
Few other things: This user control will be loaded in default.aspx page and the JS Function i am writing in default.aspx.
I am not sure why this is not working. I am using IE6/7, VS 2005. Is there any other function or way available to get the srcElement.id. Please help.
Try passing this along when you invoke the method
lnkButton.NavigateUrl = "javascript:JS_Click(this);";
then you can write this in your JS_Click event
function JS_Click(obj)
{
alert("Now I have my object " + obj.id);
}
Another option would be to use jQuery and wire up to the click event on your link buttons.
Add a CssClass attribute to the LinkButton's you are creating:
HyperLink lnkButton = new HyperLink();
lnkButton.ID = "lnkButton_" + ID.ToString();
lnkButton.Text = tstText;
lnkButton.CssClass = "linkButton";
Then write the follwoing JS/ JQuery in your ASPX page:
$(document).ready(function()
{
$('.linkButton', '#userControlID').click(function(e)
{
alert('You clicked linkbutton with ID ' + e.target.id);
});
});
In the above example each link button has class="linkButton" and they are contained within your usercontrol with ID="userControlID".
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.
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
}