Can't set focus on TextBox inside detailsview - asp.net

The DetailsView.FindControl("TextBox1") returns me the correct textbox where I want to set focus but setting focus using any of the following ways does not work
(TextBox)DetailsView.FindControl("TextBox1").Focus()
Page.SetFocus("TextBox1");
Got the Id of this textbox by Viewing page source and SetFocus on that.
Made sure that DetailsView is in Edit/Insert mode before focus is set.
None of the above has worked, I am stuck.
Note - Its works if DetailsView is inside Panel but not working where it is inside tabpanel

Instead of
(TextBox)DetailsView.FindControl("TextBox1").Focus();
Try
ScriptManager.RegisterStartupScript(this, this.GetType(), "SetFocus", "document.getElementById('" + (TextBox)DetailsView.FindControl("TextBox1").ClientID + "').focus();", true);
UPDATE
Here's another approach that uses ScriptManager:
TextBox textBox = (TextBox)DetailsView.FindControl("TextBox1");
ScriptManager.GetCurrent(this).SetFocus(textBox);

Related

How to Copy Content and Add New Record in Telerik Radgrid

Okay, basically I have a master page that only contains a RadGrid. I am using a custom popup WebUserControl form to handle the input for the update/insert controls. When I enter the edit mode I have a button called "Copy & Add New Record". When the user clicks on this button I want to copy 2/3 of the page content, open a new record, and then paste that information in the appropriate textboxes.
I have no problem copying or pasting the information. The problem lies with closing my current edit form and then opening a new record form. I tried closing the form using:
Dim temp As RadGrid = Parent.Page.FindControl("rgRT")
temp.MasterTableView.ClearEditItems()
temp.MasterTableView.IsItemInserted = True
And then setting the above statement to true to try to open the new record form. However it did not work. The popup edit form was still in the same position, I received no errors, and the only event to occur was an autopostback. I feel like this is something extremely easy but I cannot for the life of me figure it out.
Try adding a Rebind() call after clearing the insert items. This should put your grid in the defauilt (view mode) and remove the IsItemInserted = True call.
Try registering a script to init insert on the client so it goes into edit mode: http://www.telerik.com/forums/how-to-add-new-row-in-radgrid-clientside-which-should-be-inline-and-stay-in-editmode#hJ-0yiaxakaxzdOf-UXDYw (tip: you would want to use the Sys.Application.Load event for your code to execute, earlier calls may cause errors, something like below).
string script = "function f(){$find(\"" + RadGrid1.ClientID + "\").get_masterTableView().fireCommand("InitInsert", ""); Sys.Application.remove_load(f);}Sys.Application.add_load(f);";
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "key", script, true);
then, use the ItemCommand event to set default values: Telerik RadGrid - How do I set default data on insert?

Focus on textbox inside gridview and updatepanel

After breaking my head over something apparently simple, here I am:
I have an ASP.NET GridView wrapped inside an UpdatePanel. Once the user enters something in the last textbox of the last row and moves out, I add a new row to the gridview. This is done server-side by firing the OnTextChanged event of the textbox and setting its AutoPostBack property to true. This bit is Ajaxified by using the UpdatePanel.
My simple requirement is: I need to set the focus on the first textbox of the newly added row once the partial refresh is over.
What I tried:
//Get the newly added row (basically the last row)
GridViewRow newRow = myGridView.Rows[myGridView.Rows.Count - 1];
//Get the TextBox control on which I want to set focus
TextBox textBox = newRow.FindControl("txtMyCode") as TextBox;
//Set the focus
ScriptManager.GetCurrent(this.Page).SetFocus(textBox);
On stepping through the code, each of the above lines execute, and yet, when the partial postback completes, the textbox doesn't have the focus. The ScriptManager.GetCurrent(this.Page) returns an instance of the AjaxControlToolkitScriptManager, which is there on the Master page.
Any ideas?
You could write jquery to achieve this bit if you were not able to set focus from server site.
<script type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(BindEvents);
function BindEvents() {
//write your logic here lets say
//if your grid view empty row is visible then set focus
}
</script>
You can use javascript pageLoad function which is Ajax-specific function fired every time the content of ajax update panel is refreshed. Inside that function trigger 'focus' on your text box.
function pageLoad() {
document.getElementById('ClientIdOfYourTextBox').focus(); }
You need to get ClientId of newly added text box in javascript. You can achieve it by saving it in hidden field server-side once it's added and then get hidden field's 'value' client-side or by building jquery expression (e.g. get last from all 'input' controls with id containing 'txtMyCode')

Change value and text of button

I have a textbox and a button with an onClick event connected to it, and when the button is clicked once I want to change the text of the button and clear the textbox. How can i accomplish this?
If you want to do this on a postback (a complete refresh of the page), simply put code in your button click handler to set these values. For example, textbox.Text = string.Empty;
If you want to set it on the client side (without refreshing the page), then you'd need to use client script such as JavaScript.
This needs you to know about TextBox and its properties. Normally the value of a textbox you use its Text property as TextBox1.Text. In your case you put this statement in the button's click event
string txtValue=TextBox1.Text;
TextBox1.Text=string.Empty;
To see how to use TextBox and its properties. This can help

ASP .Net Textbox Textchanged event

I have a webpage. I show records from table, lets say, students in my page. I query all the students and show them in grid. I want to use a textbox for filtering the datagridview results. For example if the user types a in the textbox the grid will show only the students who has "a" in his/her name. I want to refresh the grid at the same time while textbox is being edited.
i have set the autopostback property of textbox to true, and i refresh the grid in textbox's textchanged event.But the textchanged event fires only after the textbox loses focus. How can I make it fire after user types just one character ? thanks.
You have to use the onKeyDown event. However, I'd advise you to use ASP.NET AJAX or jQuery to load the results with Ajax.
Here is one example from asp.net: http://www.asp.net/ajaxlibrary/AjaxControlToolkitSampleSite/AutoComplete/AutoComplete.aspx
Another one, from Code project:
http://www.codeproject.com/Articles/38803/Google-Like-Search-TextBox
You might want to show some your present code, if there is a particular method you want to go with for this. Otherwise your going to get a people telling you the way they would do it.
Does it look something like this right now?
<asp:Textbox id="myTextbox" runat="server" onChange="txtChanged" AutoPostBack="true"/>
public void txtChanged(object sender, EventArgs e)
{
//Get text from textbox
string text = ((TextBox)sender).Text;
//Do what ever it is you want to do to edit the text
text = text.ToUpper();
//Update the other textbox with this text
txtMyText2.Text = text;
}
I think the best and most clean way is to use Rad Controls, here is an example on how to do it:
http://demos.telerik.com/aspnet-ajax/controls/examples/integration/gridandcombo/defaultcs.aspx?product=grid
The event TextChanged only fires when you send a request to server. If you wanna launch an event or make a function when the text inside textbox changes, use an OnKeyDown event (right with Schiavini).
You can use PicNet to do this in the Client instead of the Server for a better User experience. You can find it here http://www.picnet.com.au/resources/tablefilter/demo.htm Remember that the Gridview is rendered as a HTML table, therefore you can freely use this jQuery plugin.
Good luck!

ASP.NET GridView postback not setting posted controls' values

When adding an EditItemTemplate of some complexity (mulitple fields in one template), and then parsing the controls from the RowUpdating event, the controls that were manually entered by the user have no values. My guess is there is something going on with when the data is bound, but I've had instances where simply adding and attribute to a control in codebehind started the behavior and removing that code made the code work. As a work-around, I can Request(controlname.UniqueId) to get it's value, but that is rather a hack.
Edit
When I access the value like so
TextBox txtValue = gvwSettings.SelectedRow.FindControl("txtValue") as TextBox;
the text box is found, but the .Text is not the user input.
Did you turn off ViewState?
Did you add control programmatically in the template? If so, did you create them at the correct stage?
You should be able to use the GridViewUpdateEventArgs to retrieve the inputted value, for example:
TextBox txtValue = gvwSettings.SelectedRow.FindControl("txtValue") as TextBox;
I have used that syntax before and it works like a charm.
Moved post-back data-bind to Page_Init

Resources