Back Button Code not working - c#

I am using the following code for a "Back" Button:
<INPUT type="button" value="Button" onclick="window.history.back(); return true;">
But, on the Previous page I have two div elements. When the page loads it will show div1. The next time, when the button of div1 is clicked, div2 will show. On div2 I have a "Next" button. which redirects to the other page. I want code for a "Back" button, which will show div2 when Back is clicked. Also, If I use history.back() it will show div1.
HTML code
List all your liabilities as calculated on the day the debt was forgiven to
the debt was forgiven prior
NEXT
<div id="d2">
<table cellspacing="3">
<tr>
<td colspan="2" class="styleh">
<p>
<b>Part II. List the fair market value (FMV) of all your assets, calculated as of the
day prior to the debt being forgiven - this would be the "sell today, cash value."</b></p>
</td>
</tr>
<tr>
<td>
</td>
<td>
<button id="btncalc" onclick="return btncalc_onclick()">
Calculate</button>
<button id="Button2" runat="server">
BACK</button>
</td>
</tr>
</table>
</div>
Thank you in advance

Try it :
<INPUT type="button" value="Button" onClick="history.go(-1);return true;">
OR
You can also try Asp.net Wizard Control for your scenerio.You can handle NextButtonClick
PreviousButtonClick events through this control.

change return true; to return false;

I think you will have to maintain some state to accomplish this functionality...I would suggets you to use cookie for this..set cookie when you click second div and go to the next page (you would find lot of ways of setting cookie through javascript, Here is example) and when you go back to first page check this cookie and explicitly show the second div and immedietly clear that cookie so that it won't persist...this is the one way which came instantly in my mind...

If you want to remember what state the previous page was in, you'll have to use some sort of storage.
The methods I'm most familiar with is the session variable, but using it will require you to do the coding in the codebehind of the aspx page.
protected void url_onClick (object sender, EventArgs e) {
Session["state"] = "div2";
Response.Redirect("url_of_new_page");
}
Of course you'll have to create your page based on the state during Page_Load.
If you want do to it purely in javascript, you'll have to use cookies, which I can't explain unfortunately, but here's a link to w3schools.

Related

Show updated record without refresh

I have 2 page.
When double click row data in first page will be go to second page.
second page is for update purpose.
when successful update in second page and i want back to first page, the data show not updated in first page but when refresh the data will show updated record.
How can i get the updated record show in first page without click refresh?
below is first page aspx code:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<tr style="cursor: hand;" onmouseover="this.style.backgroundColor='#E6F0FF'"
onmouseout="this.style.backgroundColor='#FFFFFF'"
onclick="javascript:window.location.href='V_Updated.aspx?mail=<%#Eval("mail") %>'">
<td class="line_table_td" style="text-align: center;"><%#Eval(" mail ")%> </td>
<td class="line_table_td" style="text-align: center;"><%#Eval(" remarks ")%> </td>
</tr>
</ItemTemplate>
</asp:Repeater>
second page aspx button back code:
<td class="btn_bg" onclick = "history.back();">Back</td>
Better to use AJAX for updating data, if it is suitable for your project. This way you do not have to use two different pages.
http://www.webblogsforyou.com/ajax-introduction-how-to-use-ajax-in-asp-net-with-example/
Thanks!
Don't go back using the browser history (e.g. using history.back()). Different browsers handle caching differently, so it's difficult to control if your page would get refreshed. Provide a proper link to the previous page instead.
Simply put a link in second page like below :
<a href="firstpage.aspx" > Back </a>

Keeping values in textboxes permanently until changed by user in a sharepoint webpart

I am working on a Visual Web Part for a SharePoint 2013 site that is a "scoreboard" for people to track progress on a project. Basically what I have is a lot of textboxes that users will enter values in. I want the values to stay in the textbox until someone changes it.
<tr>
<td> </td>
<td>A-Crew</td>
<td>B-Crew</td>
<td>C-Crew</td>
<td>D-Crew</td>
</tr>
<tr>
<td>Daily</td>
<td>
<input id="msaCrewDaily" type="text" />
</td>
<td>
<input id="msbCrewDaily" type="text" />
</td>
<td>
<input id="mscCrewDaily" type="text" />
</td>
<td>
<input id="msdCrewDaily" type="text" />
</td>
</tr>
A few things I have tried are using PHP inside the input tag to save the value but it doesn't keep it after closing and re-opening the page with webpart on it. I've thought about using local storage but i'm not sure if that will work.
My Question
Is there a way to keep the the input even if the page is closed? If not would it be better to set up a list as a Data Source? Is the something I could do in C# to keep the values?
Other Information
This is a temporary solution until a database is set up then the values will come from there. However its going to take some time to set that up. Also as of now all my code is HTML and CSS. Any help or ideas are very much appreciated and thank you in advanced!!!
So my understanding is each user opens the page and edits the value right? Then why not create a simple list with the required columns you create one item with default 0 values/or whatever you want. And send the display Item form url to all the users. Whoever wants to edit, will just click on edit and save the form. You can in fact just add this on the page enabling the inline editing and share the page with others.
You can use local storage "jStorage" write a javascript which will be called onkey of textbox and store value on local storage and call it on page load. Just check on page load if local storage contains any value if yes then set the textbox with that value

Log-in to a website on button click within the web browser control

I have a program I have created for work that keeps track of information. We get this information from various intranet sites. I am wanting to include a form on my program that will essentially be a customized web browser that allows me to navigate those sites more easily. This is a learning project for me so please forgive my inexperience.
For this question I am looking for a way after loading a page with the browser control I can log in to that page from credentials stored in a string. I do not have access to the HTML code of the site I am trying to input this information in to and have seen many tutorials on how to do this via GetElementId but after using Firebug I found neither of the two textboxes I need to input into have an id (that I can see anyway). The site never changes and I did consider using the GetElementFromPoint but not sure how to go about it this way and it did not seem optimal. GetElementsByTagName seems to be the best way of going about it but am unsure of how to implement the code.
Here is the information of the two elements i was able to get from firebug.
Username textbox:
<input type="text" name="j_username" size="12" onblur="this.value=this.value.toLowerCase(); setUserIdCookie();" click="this.value=this.value.toLowerCase();" onkeyup="this.value=this.value.toLowerCase();">
Password textbox:
<input type="password" onkeypress="checkCapsLock( event )" name="j_password" size="12">
So i need to pass for example from my Winform usernameString to the j_username and then passwordString to the j_password and then click the <input type="submit" name="submit" value="Login">button within the web control. If i can get an idea of how to go about this from this one site I should be able to learn enough from it to implement in the other sites as well.
Thanks in advance for any assistance and please let me know if any other information is required please.
The full HTML Code:
<form method="POST"action="j_security_check" name="login">
<table border="0"width="30%"cellspacing="3"cellpadding="2">
<tr>
<td class="default"><b>Login</b></td>
<td class="default"><input onkeyup="this.value=this.value.toLowerCase();"
click="this.value=this.value.toLowerCase();"
onblur="this.value=this.value.toLowerCase(); setUserIdCookie();"
type="text"size="12" name="j_username"></td>
</tr>
<tr>
<td class="default"><b>Password</b></td>
<td class="default"><input type="password" size="12"
name="j_password"onKeyPress="checkCapsLock( event )"></td>
</tr>
<tr>
<td colspan="2" align="center"><p class="default">
<inputtype="submit"value="Login"name="submit"></td>
</tr>
</table>
</form>
EDIT
I have found a way to input in to the boxes
var x = webBrowser1.Document.All.GetElementsByName("j_username");
x[0].InnerText = (usernameString);
var y = webBrowser1.Document.All.GetElementsByName("j_password");
y[0].InnerText = (passwordString);
But am having trouble figuring out how to click the Login button
Of course after posting this nice long post and working on this for hours i finally stumbled upon the answer. For anyone who is having the same issue I was here is a way to accomplish this. In my scenario this solution works well:
private void button4_Click(object sender, EventArgs e)
{
//Set username and password strings
string usernameString = "username";
string passwordString = "password";
//Input in to username field
var x = webBrowser1.Document.All.GetElementsByName("j_username");
x[0].InnerText = (usernameString);
//Input in to password fields
var y = webBrowser1.Document.All.GetElementsByName("j_password");
y[0].InnerText = (passwordString);
//Click the login button
var s = webBrowser1.Document.All.GetElementsByName("submit");
s[0].InvokeMember("click");
}

How to retrieve data from fields inside the fancybox in code-behind

Background
So I am building a dashboard. At some point the user presses an 'add widget' button. A fancybox loads the target div inside a fancybox. The code looks like this:
<script type="text/javascript">
$(document).ready(function (){
$("#various1").fancybox({
'titlePosition': 'inside',
'transitionIn': 'none',
'transitionOut': 'none',
'closeBtn': 'true'
});
});
</script>
<!-- Add Widget Button-->
<div class="cell_title_buttonAddWidget" style="right: 90px;">
<a id="various1" href="#AddWidget">Add New Widget</a>
</div>
The fancybox contains some textfields and dropdownlists. These dropdownlists are populated by database data. The user makes a selection (e.g. which type of chart he wants to see within the widget). Eventually the user selected all his choices and entered text in the textfields (e.g. Widget Title). User presses "save" button. The code for this looks like this (For the sake of simplicity, I've added only one field for the widget title):
<div style="display: none">
<div id="AddWidget" style="width:800px">
<div class="cell" style="margin: 0;">
<div class="cell_title">Add Widget</div>
<table style="margin: 10px;">
<tbody>
<tr>
<td class="form_label">Widget Title:</td>
<td>
<input id="AddWidgetTitle" runat="server" style="width: 250px;">
</td>
</tr>
<tr>
<td class="form_label"></td>
<td>
<input id="saveWidget" runat="server" type="button" value="Save" OnServerClick="saveWidget_Click">
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
What I want to accomplish for now
Upon pressing the save button, I want the saveWidget_Click function to be fired, which it does. Then I want to put the value of AddWidgetTitle in a string (later into a database), by reading AddWidgetTitle.value.
The problem
Upon pressing the input button, I fire the saveWidget_Click function in my code-behind. However, before doing so, the page_load function is executed. At this point all the textfields and dropdownlist selected items are EMPTY. The textfields read "". I want to save the data within saveWidget_Click function by starting an INSERT query line into my database, but the data is empty.
Maybe I am approaching the 'pop up window with data inside of it' the wrong way. But I am hoping I am just missing something and the thing I want is possible using fancybox.
In conclusion
I can populate the textfields and dropdownlists within my fancybox with database data. However, I can't save the edited textfields and selected dropdownlist items within my database, because upon pressing a save button, the page reloads and all the entered data is gone.

Reading dynamically generated HTML element value in codebehind in ASP.NET

I have an asp.net page where I have the below markup. Basically this markup is generated from codebehind by reading records from a table and looping through them. For each record in table, there will be a div block.
Basically this form is to read/show settings for a user. The settings entries are stored in a table.
<div id='divContainer' runat='server'>
<div id='div1' runat='server'>
<table>
<tr>
<th>Name</th>
<td><input type='text' id='txtName1' value='something' /></td>
</tr>
</table>
</div>
<div id='div2' runat='server'>
<table>
<tr>
<th>Domain name</th>
<td><input type='text' id='txtName2' value='something' /></td>
</tr>
</table>
</div>
<div id='div3' runat='server'>
<table>
<tr>
<th>URL</th>
<td><input type='text' id='txtName3' value='something' /></td>
</tr>
</table>
</div>
<div id='div4' runat='server'>
<table>
<tr>
<th>Some other value is enabled ?</th>
<td><input type='checkbox' id='chk4' /></td>
</tr>
</table>
</div>
</div>
The id's of each input element will be unique. Now in codebehind I want to read the values of each input element to save the changes user made. How can I read the elements here? Since the mark up is generated in codebehind as a string and appended the the INNER HTML of the external div, I can't read values like we do for a control which we drag and drop in the IDE.
If these are being sent back to the page in a standard HTTP POST then you can read the values in the Request.Form NameValueCollection.
Essentially, all of the server controls that become form elements get translated into standard HTML form elements just as you have there, albeit with more markup generated by .NET to help it identify them. Then it automatically maps their values back to the server controls for you on the postback, but the values themselves are still just in a standard HTTP POST, which you can access manually.
(This is also a common method used when posting a form from one ASP .NET application to another.)
If you want to grab your values for the generated controls you have to do 2 things.
Generate the input controls with a runat='server' tag for each control (otherwise they will not be included in the Request.Forms collection.) This is probably the step your missing.
<input type='text' id='txtName1' runat='server' value='something' />
Grab your values from the Request.Form collection on postback
string txtValue1 = Request.Form["txtName1"];
It really should be that easy. I tested this against your code using a DIV as the container and a simple javascript to inject the control string into the innerHTML. If your getting any issues you may have to debug and see if the dynamic control ID has changed due to inserting them into naming container or something.
So the brunt of the story is that when you dynamically add a control after Page_Init then POSTBACK values can not be inserted back into those controls.
CF: http://www.15seconds.com/issue/020102.htm and http://msdn.microsoft.com/en-us/library/ms178472.aspx
Some of the other answers here suggest "oh, add a runat=server to the control" but when you create it in the codebehind, and not in the Page_Init, then that makes ZERO difference.
Let me know if that's not how you're creating the controls or if that's not how you're using them and I'll revise this answer on more details. It really all boils down to how you're trying to access the values.
Generally, you'd place those input controls you're creating dynamically (in this case, a TextBox) inside something like a panel control (the container). Then after the user has posted their data, you'd loop that container panel.Controls collection and retrieve each TextBox text.
Be aware that some caveats apply when working with dynamically created controls because ASP is of stateless nature.
This page shows how to implement this:
Adding Dynamic Rows in ASP.Net GridView Control with TextBoxes
I didn't test it but I can suggest that:
Add your dynamic controls with runat="server" tag inside another controls with runat="server"(such as panel control). Then you can access them like this:
Textbox t = (Textbox)panel1.controls.findControl("dynamicControlId");

Categories