Close Page on click button press asp.net c# - c#

This is my first Page Name: RiskQuery.aspx. From this page on Click the
Link button of Grid View , open another page name, RiskMessage.aspx.
Both pages are under same master Page.
1st Page's Grid View Link button tag below:
<asp:HyperLinkField DataNavigateUrlFields="AppID" DataNavigateUrlFormatString="~\RiskMessage.aspx?AppID={0}" Text="Message" target="_blank" />
code behind of my RiskMessage.aspx:
if (!IsPostBack)
{
string id = null;
try
{
id = Session["cust_id"].ToString();
}
catch { }
if (id != null)
{
txtEntryHomeID.Text = id;
}
getMasterInfo();
}
I successfully opened the 2nd Page and from this page, I update my required
record. But I want to Close the 2nd page after I update the record on the [Save] button press.
I need help closing the 2nd page.

Add this line of code on Save button press:
Page.ClientScript.RegisterStartupScript(this.GetType(), "close", "<script language=javascript>self.close();</script>");

if suppose your page was popup u can close by using window.close()
here u can use instead of
Page.ClientScript.RegisterStartupScript
ClientScript.RegisterClientScriptBlock
otherwise go back one step of your history by using JavaScript
end of your saving code if it is popup
ClientScript.RegisterClientScriptBlock(Page.GetType(), "script", "window.close();", true);

Related

How to check if class attribute contains something?

I have two buttons, back and next:
By.XPath("/html/body/div/div[3]/main/div/div/form/div[1]/div[2]/div[1]/nav/div/button[1]")
By.XPath("/html/body/div/div[3]/main/div/div/form/div[1]/div[2]/div[1]/nav/div/button[2]")
First goes to previous page, second goes to the next page of my list. When I open my page, first button will be disabled until I get to another page or both will be disabled if my list is short or emty. I need to click those buttons if they are not disabled. Only diference between disabled and not is class attribute:
class="disabled btn btn-plain btn-default-hover"
class="btn btn-plain btn-default-hover"
So how can I check if buttons class atribute contains 'disabled'? scenario goes like this - if second button active click 'next' and then if first button active click 'back'
//el is the web element
if(el.getAttribute("class").split(" ").contains("disabled"))
{
//your code
}
Let's say you have two buttons with the ids "button1" and "button2"..
You can check with something like this:
if(document.getElementById("button1").className.indexOf("disabled") > 1) {
// what to do if disabled...
} else {
// what to do if active...
}
And of course, you can do the same for the other button...
by using jquery write the following code
var ClassAtttr = $("#YourElementId").attr("class");
if (ClassAtttr.indexOf('Your Expression'))
{
//do somthing
}
Why not try the '.Enabled'
WebElement backButton = driver.FindElement('Your Locator');
if(backButton.Enabled)
{
'Your action to be performed
}

How to make previous page refresh after I click a button in pop-up window?

My main page has a GridView and Buttons for add/update. When I click the update button, a window pops up(used JavaScript) which has input fields and a Button for actually updating the record. What I want to know is how to refresh the main page when I click the update button inside the pop-up window.
This is the partial code of what I tried on actual update's OnClick:
if (PreviousPage != null)
{
GridView gridv = (GridView)Page.PreviousPage.FindControl("GridView1");
gridv.DataBind();
}
While debugging, I realized that the if statement was never executed. Does that mean pop-up windows do not have a PreviousPage initially? If so how can I reach the Main page then? (i shall note that the main page is not a master page)
This is how I create the pop-up on button click from the Main page(so it's a new window):
function btnEditEP_Click() {
var recID = document.getElementById('<%=tboxEdit.ClientID%>').value;
window.open("editPopupEP.aspx?Txt=" + recID, "_blank", "toolbar=yes", "resizable=yes", "scrollbars=yes");
}
On your child window you can call a function something like this in your child window call on your update function
<script type="text/javascript">
function MyFunction() {
window.opener.PostBackParentWindow();
window.close();
}
</script>
and in your parent window call add this code
<script type="text/javascript">
function PostBackParentWindow() {
__doPostBack(null, null);
}
</script>
Hope it might help
On the update click also perform the close pop up call
<script>
var popupWindow;
function openw(url) {
popupWindow = window.open(url, "popup", "");
}
function closew() {
if (popupWindow) {
popupWindow.close();
}
}
</script>
open<br />
close
like described here . How to close a popup window in a parent window?
and then in closw you can call your parent page with true/false value depending on your update operation. You can use a hiddenfield for this.

Enter Does Not Work For Submit, ASP.NET Page Refreshes and Doesn't Search

I am working with an ASP.NET site with a C# back end. There is a .Master page where the tag is, and other search pages on the site will search when you hit enter after filling out the form. However we have one page that has a few text boxes and when you hit enter to search, it appears to just refresh and reload the page without searching. In order to search you have to hit the search button (which is an igtxt:WebImageButton ). All of the solutions I have found to this issue so far involve people using javascript to call some kind of function on submit. As far as I know there is no javascript being called when you hit the search button, it is all in the C# code. Once again I find myself searching SO and other sites for an answer but none of the solutions seem to fit my situation. The form tag is as follows:
<form id="form1" runat="server">
The web image buttons call a btn_SearchClick function that runs the search code. But since the form is started in the .Master file I can't edit that as it would effect all other pages as well. Is there any way to have enter call the btn_SearchClick from the form without having to put it in the form tag? I'm not sure what would've changed to cause this behavior on one page and none of the others.
if (!IsPostBack)
{
TextBox1.Attributes.Add("onKeyPress",
"doClick('" + btnSearch.ClientID + "',event)");
}
<SCRIPT type=text/javascript>
function doClick(buttonName,e)
{
//the purpose of this function is to allow the enter key to
//point to the correct button to click.
var key;
if(window.event)
key = window.event.keyCode; //IE
else
key = e.which; //firefox
if (key == 13)
{
//Get the button the user wants to have clicked
var btn = document.getElementById(buttonName);
if (btn != null)
{ //If we find the button click it
btn.click();
event.keyCode = 0
}
}
}
</SCRIPT>
or u can use default button.it's work while cursor's in this box
protected void Page_Load(object sender, EventArgs e)
{
this.Form.DefaultButton = this.btnSubmit.UniqueID;
}
Add some jquery to control the Enter key behavior on your textbox. Something like this:
$(function() {
$('#<%=txtTextbox.ClientID%>').keypress(function (e) {
if (e.which == 13) {
e.preventDefault();
$('#<%=btn_SearchClick.ClientID%>').click();
}
});
});

Best way to pass values from popup page to parent page

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.

Controlling one page controls on other page

I have been working on a project and i got stucked on in a problem as i have a popup on a page student.aspx and the popup get visible onpageload and i have another page as email.aspx and also email page do contain the button,what i want is when i click on a button the popup should get invisible.
i got a solution as
if (Page.PreviousPage != null){
TextBox SourceTextBox =
(TextBox)Page.PreviousPage.FindControl("TextBox1");
if (SourceTextBox != null)
{
Label1.Text = SourceTextBox.Text;
}}
how to implement it with modalpopupextender

Categories