I am trying to fetch data from a website in my application through a WebBrowserControl. I can successfully log in, but when I call btnsearch.Invokemember("click") programmatically I receive a JavaScript error. If I click the button interactively on the WebBrowserControl I receive no error and everything works fine.
In the aspx page I have one JavaScript function for calling an Ajax Panel:
<script type="text/javascript">
/*<![CDATA[*/
window["ctl00_WorkSpace_ctlCustAVIHistory_ajxResultPanel"] = new RadAjaxPanelNamespace.RadAjaxPanel({ClientID:"ctl00_WorkSpace_ctlCustAVIHistory_ajxResultPanel",EnableOutsideScripts:false,EnablePageHeadUpdate:true,Url:"/app/ModuleCustomer/ViewAVITransactions.aspx",LoadingPanelID:"ctl00_WorkSpace_ctlCustAVIHistory_LoadingPanel1",ActiveElementID:"",ClientEvents:{OnRequestStart:"",OnRequestSent:"",OnResponseReceived:"",OnResponseEnd:""},FormID:"aspnetForm",UniqueID:"ctl00$WorkSpace$ctlCustAVIHistory$ajxResultPanel"});/*]]>*/
</script>
It produces a UniqueID. If we click on btnsearch through code this unique id is null.
function onClickButton(rName)
{
if(validateData())
{
document.getElementById("ctl00_WorkSpace_ctlCustAVIHistory_hdnReset").value = "0";
ctl00_WorkSpace_ctlCustAVIHistory_ajxResultPanel.AjaxRequest("NewSearch"); ///these caling down function
}
}
RadAjaxPanelNamespace.RadAjaxPanel.prototype.AjaxRequest = function (eventArgument) {
this.AjaxRequestWithTarget(this.UniqueID, eventArgument);//this.UniqueID giving null from program click mouse click it is giving correct id
};
Any idea why this happens?
private const string STR_Field_userid = "field_userid";
private const string STR_Field_password = "field_password";
private const string STR_Submit = "submit";
document.GetElementById(STR_Field_userid).InnerText = UserId;
document.GetElementById(STR_Field_password).InnerText = Password;
document.Forms[0].InvokeMember(STR_Submit);
Related
I am trying to get an asp.net chart and it's legend to allow me to open up another page in another tab passing the values of the piece of the chart I clicked on with it. I have been able to get it to open up another tab when clicking on the chart by doing the following but it does not pass the data.
Chart2.Series[0].LegendUrl = "chartdetails.aspx";
Chart2.Series[0].Url = "chartdetails.aspx";
Chart2.Series[0].LegendMapAreaAttributes="target=\"_blank\"";
Chart2.Series[0].LegendPostBackValue = "#VALY-#VALX";
Chart2.Series[0].MapAreaAttributes="target=\"_blank\"";
Chart2.Series[0].PostBackValue = "#VALY-#VALX";
If I leave out the urls and mapareaattributes I can then get it to go to the onclick where I am able to get the data, put it in a session variable and use Reponse.Redirect to open the new page where it sees the session variable data,however it doesn't open in another tab, it opens in the same tab.
Chart2.Series[0].LegendPostBackValue = "#VALY-#VALX";
Chart2.Series[0].PostBackValue = "#VALY-#VALX";
protected void Chart2_Click(object sender, ImageMapEventArgs e)
{
HttpContext.Current.Session["VAL"] = e.PostBackValue;
Response.Redirect("chartdetails.aspx", false);
}
How can I get it to do both? Does Response.Redirect have a way to open a new tab? Some research leads me to believe it does not. Is there a way to get both the server side onclick event to run, so I can set the session variable and the chart.series.url to fire after the server side click runs so the session variable would be set before I open the new tab?
I'm feeling like this may be a case of "I can't have my cake and eat it too."
As it turns out I can have my cake and eat it too. If I set the url, postbackvalues, and legendmapareaattributes in my Page_Load and set up the click for the chart to put the PostBackValue in the session variable when you click on the chart it saves the value in the session variable that is listed in the PostBackValue of the Series of the chart. It then opens in a new tab chartdetails.aspx where I can access the information from the session variable.
Chart2.Series[0].LegendUrl = "chartdetails.aspx";
Chart2.Series[0].LabelUrl = "chartdetails.aspx";
Chart2.Series[0].Url = "chartdetails.aspx";
Chart2.Series[0].LegendPostBackValue = "#VALY-#VALX";
Chart2.Series[0].LabelPostBackValue = "#VALY-#VALX";
Chart2.Series[0].PostBackValue = "#VALY-#VALX";
Chart2.Series[0].LegendMapAreaAttributes = "target=\"_blank\"";
Chart2.Series[0].LabelMapAreaAttributes = "target=\"_blank\"";
Chart2.Series[0].MapAreaAttributes="target=\"_blank\"";
protected void Chart2_Click(object sender, ImageMapEventArgs e)
{
HttpContext.Current.Session["VAL"] = e.PostBackValue;
}
I can't use postback to get the value on the series for some reason. So, I want to share my way inspired by #Adam that is loop over the series points after data binding the chart and set URL.
GET:
Series s=new Series("Test");
/*
* After Data bind to the series s
*/
for (int p = 0; p < s.Points.Count; p++)
{
s.Points[p].Url ="test.aspx?name1=value1&name2=value2";
s.Points[p].MapAreaAttributes = "target=\"_blank\"";
}
POST:
(I put javascript function in url. So, It will execute the javascript for me to send a form I created in the function to the text.aspx.)
Series s=new Series("Test");
/*
* After Data bind to the series s
*/
for (int p = 0; p < s.Points.Count; p++)
{
s.Points[p].Url ="javascript:(function(){" +
"var mapForm = document.createElement('form');mapForm.target = '_blank';" +
"mapForm.method = 'POST';mapForm.action = 'test.aspx';" +
"var mapInput = document.createElement('input');mapInput.type = 'hidden';" +
"mapInput.name = 'partID';mapInput.value = 'put any value you need';" +
"mapForm.appendChild(mapInput);document.body.appendChild(mapForm);" +
"mapForm.submit();document.body.removeChild(mapForm);})();";
}
Reference:
Javascript pass values using POST
I have the following code -
protected string Term
{
get { return this.ViewState["Term"] != null ? (string)this.ViewState["Term"] : ""; }
set { this.ViewState["Term"] = value; }
}
Then set Term -
public void populateTerm()
{
Term = "Test";
ScriptManager.RegisterStartupScript(this, GetType(), "term","useTerm()", true);
}
Then in Javascript can use Term -
function useTerm() {
var Term = <%= Term %>;
// use Term
}
This works fine when the page is getting reloaded on the button click that was firing populateTerm(). However I have since moved the button into an updatePanel, so because the entire page is not getting reloaded on button click (only the update panel), this means that useTerm() is not called and <%= Term %> is null.
If I remove the updatePanel code it works fine. How can I get this to work with the updatePanel?
Assuming that the function useTerm is in you aspx page, you can move that script block within the UpdatePanel ContentTemplate.
An alternative would be to change the JavaScript function so term is passed in as a parameter:
function useTerm(term) {
// use term
}
and then to call it with the latest value of Term:
public void populateTerm()
{
Term = "Test";
string script = string.Format("useTerm('{0}');", Term);
ScriptManager.RegisterStartupScript(this, GetType(), "term", script, true);
}
I have a main page for customer in which i need to select location for the customer . to pick a location we have desined a popup page which has a grid which displays all locations . once user pics the loction that particular location should be returned back to the main page .
Location object contains feilds - LocId,LocName,LocState,LocCountry,PinCode .
The entire location object should be returned to the main page not a single value .
My code for opening location is
<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="../Content/Images/search.png" Height="21px" ToolTip="Search Location" Width="21px"
OnClientClick="ShowLocation();" />
function ShowLocation() {
window.showModalDialog('../StandardLookups/Location.aspx', '_blank', 'dialogWidth:820px; dialogHeight:400px; dialogLeft:250px; dialogTop:250px;status:no; scroll:no; help:no; resizable:no,edge: Raised,doPostBackAfterCloseCallback: false,postBackElementId: null');
}
Code in popup window once the row is selected by the user
protected void btnSelect_Click(object sender, EventArgs e)
{
List<object> locationValues = gvLocationLookup.GetSelectedFieldValues(new string[] { "LocId", "LocName", "LocState","LocCountry","PinCode" });
var locationValue = (object[])locationValues[0];
var location= new Location
{
LocId = (int?)locationValue[0],
LocName = (string)locationValue[1],
LocState = (string)locationValue[2]
LocCountry = (string)locationValue[3]
PinCode = (string)locationValue[4]
};
Session["SELECTED_LOCATION"] = location;
Response.Write("<script> window.opener.location.reload(false); window.close();</" + "script>");
Response.End();
}
Currently i use sessions values to move values . Is there any better approaches ?
You can check here what method suits to your needs.
http://msdn.microsoft.com/en-us/library/6c3yckfw%28v=vs.100%29.aspx
I've created a system like this before, using only client side code (javascript, no C#).
The requirement was that, when filling in a form, a specific value (e.g. a location) could be selected in a popup.
Once the value was clicked in the popup, the parent page receives this information through javascript, and the popup closes itself.
Note that the parent page must have the needed fields to be filled. In my case it is an autocomplete textbox (=FieldName) and a linked HiddenField (=FieldName_key), which holds the unique key to the text in the textbox.
function confirmSelection(code, key)
{
try {
window.opener.document.getElementById('<%= FieldName %>').value = code;
window.opener.document.getElementById('<%= FieldName %>_key').value = key;
window.close();
}
catch (e) {window.close();}
}
An additional benefit of this client-side script (opposed to the C# script) is that you don't have to reload the parent page, which can disturb your user in his/her work.
I currently use a hidden input field that is assigned the value of the tab that has just been selected, via javascript, like so:
function onTabSelecting(sender, args) {
var tab = args.get_tab(); //get selected tab
document.getElementById("MainContent_hdnPreviousTab").value = tab.get_text(); //assign value to hidden field
if (tab.get_pageViewID()) { //ignore
tab.set_postBack(false);
}
}
I then use this assigned value when the page is returned to, on client-side (ajax) PageLoad() event:
<script type="text/javascript" language="javascript">
var runOnce = false;
function pageLoad() {
if (!runOnce) {
var lastTab = document.getElementById("<%= hdnPreviousTab.ClientID %>");
if (lastTab.value) {
if (tabStrip) {
var tab = tabStrip.findTabByText(lastTab.value);
if (tab) {
tab.click();
}
}
}
runOnce = true;
}
}
</script>
Currently in IE this works fine (I know right?), the value that was previously set in javascript is still there and i am able to lcoate the tab that the user left the page on. However in FF, Chrome, etc. i have no such luck. The hidden field is returned to it's empty state (value = "") regardless of utilising viewstate or not.
Very curious as to whether anyone has an alternative method that would be appropriate in this situation. Please let me know if this is unclear.
Many thanks.
You could use localstorage.
localStorage.setItem('tab', value);
I am registering java script to my Asp.net code behind file, which is working fine. Now, I have some update panels on the same page and problem is whenever there is any change in any of the update panel, this script is automatically getting called. Is there any way that I can stop this happening. I can't remove update panels from my page and this script is also a very essential part of the application. In this situation I am just calling a alert (rad alert with set time out functionality) when Save Button is clicked or an Update routine is called while I have few other buttons in update panels and whenver any of the button which is registered to the update panels clicked, the following script is called un-willingly. Anyone's help will really be appreciated.
following is my Page.ClientScript
string radalertscript = "<script language='javascript'> Sys.Application.add_load(function(sender, e) {var oWnd = radalert('dialogMessage', 400, 140, 'Saved');window.setTimeout(function () { oWnd.Close(); }, 3000);});</script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "radalert", radalertscript);
You can assign empty string to same key radalert to remove the script.
if(some_condition)
Page.ClientScript.RegisterStartupScript(this.GetType(), "radalert", "");
Edit: Based on comments, you can make it simple without using RegisterStartupScript
In code behind
btnSave.Attributes.Add("", "saveButtonFunction();");
In Javascript
<script language='javascript'>
Sys.Application.add_load(function(sender, e) {
if(btnSaveClicked){
var oWnd = radalert('dialogMessage', 400,140, 'Saved');
window.setTimeout(function () { oWnd.Close(); }, 3000);
btnSaveClicked = false;
}
});
btnSaveClicked = false;
function saveButtonFunction(){
btnSaveClicked = true;
};
</script>
Thank you very much for your answer Adil. I already have followed the same approach with little difference. I have taken JavaScript out from my code behind file and have registered Sys.Application.add_load event as follow
Sys.Application.add_load(DisplayRadAlertHandler);
function DisplayRadAlertHandler() {
var getMessage = document.getElementById('<%=radAlertDialogHidden.ClientID%>').value;
if (getMessage != "") {
document.getElementById('<%=radAlertDialogHidden.ClientID%>').value = "";
var oWnd = radalert(getMessage, 400, 140, 'Saved');
window.setTimeout(function () { oWnd.Close(); }, 3000);
}
}
Here I am setting my alert message in a hidden input field from code behind file and in the above event handler I am just checking if message is there than reset the hidden field and display the message. Your approach is also right and I have marked your answer but as I am displaying my message from multiple locations (Save button, Update routine etc.) so by assigning value to hidden input field and than resetting in above event handler looks more appropriate. Thanks once again for your help.