how to determine which textbox is focused and add text to it? - c#

I am creating web application in asp.net. I have 5 text box which are asp controls. I want to get which text box having focus on that time and then when I am press "add" button, the "hello" text has to added to the particular text box which are got focused before pressed the "add" button.
I need Asp.net (with C# code) to move to my next step

Since the last focus will be on the Add button, you need to "remember" the TextBox before the Button is clicked.
Try to use onfocus() event.
<!-- Add a HiddenField control to "remember" the id of a TextBox -->
<asp:HiddenField ID="HiddenField1" runat="server" />
<!-- Assign current focus to that hidden field -->
<asp:TextBox onfocus="document.getElementById('HiddenField1').value=this.id" ...>
<asp:TextBox onfocus="document.getElementById('HiddenField1').value=this.id" ...>
If that works, you will be able to get a value in code-behind as
string c = HiddenField1.Value;
if (!string.IsNullOrEmpty(c))
if (c == "TextBox1")
TextBox1.Text = "hi there";
Note: depends on your environment you might need to call ClientID (e.g. getElementById('<%=HiddenField1.ClientID%>')
UPDATE
As I told you above you might need to use HiddenField1.ClientID to find the HiddenField Control. When your source looks as
<input type="hidden" name="ctl00$MainContent$HiddenField1" id="MainContent_HiddenField1" />
<input name="ctl00$MainContent$TextBox1" type="text" id="MainContent_TextBox1"
onfocus="document.getElementById('HiddenField1').value=this.id" />
<input name="ctl00$MainContent$TextBox2" type="text" id="MainContent_TextBox2"
onfocus="document.getElementById('HiddenField1').value=this.id" />
<input type="submit" name="ctl00$MainContent$Button1" value="Button" id="MainContent_Button1" />
That means that you use MasterPage and asp.net changes id of your control to MainContent_HiddenField1. So when you use document.getElementById('HiddenField1') it will not find such control because its id is different. You either need to use
document.getElementById('MainContent_Button1').value=this.id
which will work but might require changes if you remove master page or will move the textbox to other container
or use
document.getElementById('<%=HiddenField1.ClientID%>').value=this.id
which will automatically inject correct id (recommended asp.net way)

Related

How to set the current tab/content on page postback

I have the following JSFiddle which changes the tab/content based on user selection: http://jsfiddle.net/sikni8/3d64w6gf/1/
HTML snippet sample:
<asp:Button ID="btnRegister" ClientIDMode="Static" runat="server" Text="Register" OnClick="btnRegister_Click" />
<!--<input type="submit" name="commit" value="Register" runat="server" />-->
The btnRegister button perform some action when clicked. When the page does a postback, how can I ensure the tab/content which was there is displayed.
For example, if I click on the Register button the page refreshes and brings the first tab/content to view. I would like to change it so that when Register button is clicked and the page reloads, it should remain with the same tab/content.
I can use sessionStorage but I would like to request some assistance.
Tried this but didn't work:
<script>
$(document).ready(function (e) {
localStorage.setItem("whichTab", $("#tabs nav ul li.tab-current"));
alert(localStorage.getItem("whichTab"));
});
</script>
I am using cbpFWTabs.js file to achieve the tab class switch which is in the fiddle.
I did something similar by using hidden field, where I stored the id/class of the current tab in hidden field and after postback I just displayed the current tab again.

Checkbox not working in asp.net and HTML mixed page

I'm grappling with an issue where the site is asp.net/C# but controls on the .aspx pages are HTML and I'm not sure how well it would go over if I would change everything into asp.net controls. Also the change is minor. I was tasked to add a check box, as in <input type="checkbox" name="disableFeatureManager" runat="server" id="disableFeatureManager" />Disable Feature Manager and in the .cs page I want to check if the box is checked and make decisions based on that, but the control's checked property is always false. The submit button is also a HTML control: <input type="submit" value="Start" name="submitButton" />
In the Page_Load ckecking if check like this returns false.
if (disableFeatureManager != null && disableFeatureManager.Checked)
nextURL.Append(FeatureManagerChoices.CreateQueryStringFromFormData(Request.Form));
You could keep your checkbox as an Html server control by doing the following:
<input type="checkbox" name="disableFeatureManager" runat="server" id="disableFeatureManager" />
Then you could change your button to a web control as follows:
<asp:Button ID="submitStart" runat="server" OnClick="btn1_Click" Text="Start" ClientIDMode="Static" />
The only difference with the above rendered HTML will be the name that is output but you will have an Id that is submitStart due to the ClientIdMode being static, if you need a friendly consistent Id for javascript manipulation.
To wire in the event add this code which will read the value from the checkbox:
protected void btn1_Click(object sender, EventArgs e)
{
var isChecked = disableFeatureManager.Checked;
}
As I mentioned before, I was trown into the deep end of MVC. Co-worker offered a simple answer(as least in this case):
bool isChecked = false;
if(Request["disableFeatureManager"] != null)
isChecked = (Request["disableFeatureManager"].ToLower() == "on");
if (!isChecked)
nextURL.Append(FeatureManagerChoices.CreateQueryStringFromFormData(Request.Form));
I think this can be further simplified in to one IF statement.

Can I add HTML button triggers to update panel

Is it possible to add HTML input buttons to asp.net triggers, as I have a message box it works perfectly for a gridview which is in update panel,
but when I go to a different page of gridview, message box displays but buttons stops working, I don't know how to debug it, please help.
this is the button,
<input type="button" id="Button2" value="Cancel" cssclass="rightButton" />
and can I add it to,
<asp:AsyncPostBackTrigger ControlID="Button2" EventName="Click" />
OR should I not ?
with runat server tag you use html controls in c# script.
i.e
<input type="button" id="Button2" value="Cancel" cssclass="rightButton" />
should be
<input type="button" id="Button2" value="Cancel" cssclass="rightButton" runat="server" />
and the when you double click on it(assuming VS as IDE), it will make a new click event in c# snippet. Besides its good practice to use direct html tags when complex functions are not needed, it saves time to display the page, as an asp component first translates itself in the html and then goes to browser; while this approach saves time of translation.
Regards
Yes you can! to see errors ,first remove UpdatePanel from your code and then test elements without it,in this status if any error occurs will be shown, after test you can add it again.

How to I change a form's data in ASP.NET/C# code behind on onclick?

I have this input in a form:
<input name="keywords" type="text" id="keywordSearch" value="Enter keywords / publication number" class="watermark" />
I then have an click function that gets called when the user submits the form. How do I get the value of the keywords and change it in the click submit function in my code behind?
Is it Form.keywords.value = 'my keywords';?
Use Request.Form["keywords"] to retrieve the value that was posted.

Form GET in ASP.NET

I'm trying to convert a classic ASP page to ASP.NET 3.5.
On the page, there is a small form to submit your e-mail address to an external newsletter site. Here's the code:
<form name="emailForm" action="http://www.site.com/emailsignup.aspx" method="get">
<input type="text" name="email" />
<input type="submit" id="btnSubmit" name="btnSubmit" />
</form>
I was hoping I'd just be able to drop this on the page and it would work, but it doesn't, it just reloads the page.
How am I supposed to do this? Does it need to be done in the code behind of the button's click event?
In ASP.Net, by default controls - like the button - that cause postbacks will submit the page back to itself EVEN if you set the action attribute on the page to another file. What you want is called Cross-Page Posting. The following MSDN pages shows you how to do this with ASP.Net 4, but there is a link at the top to get to older versions:
http://msdn.microsoft.com/en-us/library/ms178140.aspx
http://msdn.microsoft.com/en-us/library/ms178139.aspx
Otherwise you can just use the Button's Click Event Handler in the code behind.
Hope this helps.
you must be missing runat="server" in the form tag
try to create a page through the IDE and paste the code for input tags between the form tags
<input type="text" name="email" />
<input type="submit" id="btnSubmit" name="btnSubmit" />
It surely is ending nested in the form aspx get by default.
Depending on the layout of your page, you can modify it so you don't end with a nested form. If that's not possible, I think you can't get around to use a form, so instead you'll have to look at a different solution like building the get with js.
The easiest way would be to put that <form> tag outside the main <form runat="server"> tag that usually wraps all ASP.NET controls.
If you're using a master page and the only content placeholder you can use is within that <form runat="server" tag, or you need this form tag in the page structure within the main <form runat="server"> tag then you need to:
Take out the simple <form> tag but leave the HTML <input> tags. Handle the client onclick event (the JavaScript versions, not ASP.NET postback handlers) of the submit button. That handler should grab the e-mail from the text box and issue something like window.location = 'http://www.site.com/emailsignup.aspx?email=....' in Javascript. Make sure to cancel the default HTML button action handler so it doesn't bubble up and submit the ASP.NET form too.

Categories