I have four textboxes within an UpdatePanel, and AutoPostBack is set to True for all of them. When the user enters a value and hits Enter, I want the cursor to move to the next textbox automatically. When I don't have the controls in an UpdatePanel, the standard textbox.focus method works fine.
I found some code here that led me to create this:
Protected Sub txtField_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txtField1.TextChanged,
txtField2.TextChanged, txtField3.TextChanged
'Based on the textbox where data was changed, move the cursor to the next field.
Try
Dim sm As ScriptManager = ScriptManager.GetCurrent(Me)
'As multiple textboxes fire this same event, determine which textbox triggered this.
Dim MyTextbox As TextBox = TryCast(sender, TextBox)
Select Case MyTextbox.ID.ToString
Case "txtField1"
sm.SetFocus(txtField2)
Case "txtField2"
sm.SetFocus(txtField3)
Case "txtField3"
sm.SetFocus(txtField4)
End Select
Catch ex As Exception
lblError.Text = "Error in [txtField_TextChanged]: " & ex.Message
End Try
End Sub
What is really strange is that this works ONCE on whatever field I try first. After that, the event is fired, but the focus does not change. Is there something additional I need to to for subsequent calls?
I would appreciate any suggestions. Thank you!
Try setting the EnablePartialRendering property of the ScriptManager to false, like so:
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="false"></asp:ScriptManager>
I hope this helps! Good luck, and happy coding :)
Related
I have a dropdownlist with autopostback and an update panel, it works correctly,
but I have also other controls in my page with autopostback.
What I need is to control when the page is autopostback but the dropdownlist is not autopostback do something, like this:
If is not Page.autopostback then
'do something
else if is not MyDropdownlist.autopostback then
' do something different
End if
I can use this:
If is not Page.autopostback then
End If
But I canĀ“t do this:
If is not MyDropdownlist.autopostback then
End If
So how can I do this? I hope my explanation has been helpful, thanks.
The __EVENTTARGET request form variable has the name of control that caused postback to happen. You can query the name of this control and do whatever you want to do.
For example,
If IsPostBack Then
Dim postBackControlId As String = Request.Form("__EVENTTARGET")
If Not String.IsNullOrEmpty(postBackControlId) Then
If postBackControlId = "DropdownList1" Then
' the postback happened due to DropdownList1
Else
' the postback happened due to some other control.
End If
End If
End If
on textbox blank, i wanted to clear my gridview source
But i was not able to do it in vb.net.
After referring several answers i tried following unsuccessfull attempts:
grdUsers.rows.clear() : Does not work with vb.net
grdUsers.DataSource=""
grdUsers.columns.clear()
But it does not worked out.
Please help me to clear my datasource of gridview.
If your DataGridView is bound to a DataSource and you want to clear it then you can use the Nothing keyword followed by a DataBind().
grdUsers.DataSource = Nothing
grdUsers.DataBind()
Here is more information on the DataBind() method.
If you want to clear your rows when the text is empty in TextBox1, you would create a TextChanged event for your textbox ...
Private Sub TextBox1_TextChanged(sender As Object, e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text.Trim = "" Then
grdUsers.DataSource = Nothing
grdUsers.DataBind()
End If
End Sub
I have a gridview which has a List (Of MyObject) as its datasource.
gv.DataSource = MyListObject
gv.Databind()
Under a templatefield i have a button configured to delete a record from MyListObject and then rebind it. To add a record i have the below code
Protected Sub btnAddRecord_Click(sender As Object, e As EventArgs) Handles btnAddRecord.Click
Dim Customer As New Customer
With Customer
.Name = txtName.Text
.Surname = txtSurname.Text
.....
.ID += MyListObject.Count
End With
MyListObject.Add(Customer)
gv.DataSource = MyListObject
gv.DataBind()
End Sub
This works fine, but then i need to allow the user to delete a record if need be:
Private Sub gv_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv.RowCommand
If e.CommandName = "Delete" Then
MyListObject.RemoveAt(e.CommandArgument)
gv.DataSource = Nothing
gv.DataBind()
gv.DataSource = MyObjectList
gv.DataBind()
upnl.UpdateMode = UpdatePanelUpdateMode.Conditional
upnl.Update()
End If
End Sub
When i click the button it deletes the record but doesnt refresh the data. By that i mean when the record is added i am assigning the ID as a row ID and then use that ID to remove the record. Since the List and Gridview values are now out of sync i set the datasource to nothing and rebind it in order that i was going to have the values reset and the ID would be the correct one - but this doesnt works as i expected.
Could anyone advise where im going wrong and how to correct this problem?
Is gridview in the updatepanel? If yes, that panel should also be refreshed.
I added CommandArgument='<%# Container.DataItemIndex %>' which resolved the issue as i was then deleting the row the user clicked against.
Just as a side note, I'd probably handle what get's updated when on the client side using the ajax script manager rather than doing it in the code behind. Saves headaches. The above might be updating the update panel correctly, but the ajax plumbing may not be there on the client side.
The Repeater control in the following test case contains two runat="server" DIVs. Each one of them gets a TextBox appened to them through the ItemCreated event of the Repeater. Both of the have AutoPostBack=True, and TextChanged event wired to txt_TextChanged. However, only the TextBox from the first level properly points to the event on the postBack of the page. The second level TextBox also causes the postBack to occur, but its value does not persist in the VIEWSTATE as well as the event does not fire.
Here's a direct link to the test case in a .zip file (uploaded to my personal website), as well as all the code needed. The project is built in VS2010 using .NET Framework 4, but the problem also exists in 1.1, 2, and 3.5.
Anybody has any ideas on what is wrong and how to make this work?
ASPX
<asp:Repeater ID="rep" runat="server">
<ItemTemplate>
<!-- first level works -->
<div id="divOutside" runat="server">
<!-- second level doesn't work -->
<div id="divInside" runat="server"></div>
</div>
</ItemTemplate>
</asp:Repeater>
Code-Behind
Public Class WebForm1
Inherits System.Web.UI.Page
Private Sub WebForm1_Init(sender As Object, e As System.EventArgs) Handles Me.Init
If Not IsPostBack Then
Dim Table As New DataTable()
Table.Columns.Add("Column1")
Dim Row As DataRow = Table.NewRow()
Row("Column1") = ""
Table.Rows.Add(Row)
rep.DataSource = Table
rep.DataBind()
End If
End Sub
Private Sub repTest_ItemCreated(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rep.ItemCreated
' outside
Dim divOutside As HtmlGenericControl = DirectCast(e.Item.FindControl("divOutside"), HtmlGenericControl)
Dim txtInput As New TextBox
With txtInput
.ID = "txtInputOutside"
.AutoPostBack = True
.Text = "Event gets called, value persists accross postBack."
.Width = 400
End With
AddHandler txtInput.TextChanged, AddressOf txt_TextChanged
divOutside.Controls.Add(txtInput)
' inside
Dim divInside As HtmlGenericControl = DirectCast(e.Item.FindControl("divInside"), HtmlGenericControl)
txtInput = New TextBox
With txtInput
.ID = "txtInputInside"
.AutoPostBack = True
.Text = "Event NOT called, value is lost during postBack."
.Width = 400
End With
AddHandler txtInput.TextChanged, AddressOf txt_TextChanged
divInside.Controls.Add(txtInput)
End Sub
Protected Sub txt_TextChanged(sender As Object, e As EventArgs)
End Sub
End Class
Is there any reason why the textboxes have to be added to the divs dynamically? why not just put them in the aspx page with the repeater then on itemdatabound enable/disable or do whatever you need to do. That should work fine.
By the way, if you were to use panels instead of divs your on the fly approach will work.
Here's a link to Microsoft Connect, where I reported this is an official bug:
https://connect.microsoft.com/VisualStudio/feedback/details/652655/asp-net-bug-in-event-linking-of-2nd-level-dynamic-controls
It contains 2 PARTIAL workarounds that work in the uploaded files section, if anybody is interested or encounters the same problem, as well as details to what I found in the Workarounds tab.
To keep it short, it is the order of getting references to the containers and the order the TextBoxes are appended to their appropriate containers which either causes the issue or works as expected.
But, not to forget the most important point, the only reason the first-level TextBox is there in the first place is to showcase what I want as functionality. If the first-level TextBox does not get appended at all, then both workarounds fail to provide any sort of fix. Please keep that in mind when reviewing that problem - finding a workaround that revolves around changing the order of the items is not an actual solution or a fully working workaround!
Thanks everyone for the input!
Do you know how to access textboxes added to a radgrid that are not bound but are used to trap any row related input a user typed in to the textbox for that column.
I need to access this data server side when a postback occurs.
Your thoughts are greatly appreciated
Thanking you
Tony
That depends on how those textboxes are being added/created. If by 'not bound' you mean they are in Template columns you should be able to use .FindControl in one of the grid's events to grab that textbox.
And again which event will depend on what is causing the postback to happen.
For the purpose of this code example I'll assume you are dealing with a Command item on the grid
Private Sub radGrid_ItemCommand(ByVal source As Object, ByVal e As Telerik.WebControls.GridCommandEventArgs) Handles RadGrid1.ItemCommand
Select Case e.CommandName
Case "Update"
Dim txt as Textbox
txt = e.Item.FindControl("textboxID")
If Not txt is Nothing Then someObject.someString = txt.Text
Case Else
'do something else
End Sub
Hope that helps.
Private Sub radGrid_ItemCommand(ByVal source As Object, ByVal e As Telerik.WebControls.GridCommandEventArgs) Handles RadGrid1.ItemCommand
Select Case e.CommandName
Case "Update"
Dim txt as Textbox
txt = e.Item.FindControl("textboxID")
If Not txt is Nothing Then someObject.someString = txt.Text
Case Else
'do something else
End Sub