Maintain Tab index after post back with textchanged - asp.net

I have 3 textboxes that have AutoPostBack = true. On postback it's losing it's focus. I've been searching and tried many things but nothing is working.
The code below is a snippet of what i'm trying to do.
basically this first snippet isn't grabbing anything. Does anyone know what i'm doing wrong here
**Side note I'm not using Update Panel i'm using LayoutItemNestedControlContainer
Dim ctrl = From control In wcICausedPostBack.Parent.Controls.OfType(Of WebControl)()
Where control.TabIndex > indx
Select control
Protected Sub txtDEPTH_TextChanged(sender As Object, e As EventArgs) Handles txtDEPTH.TextChanged
UpdateProductTemp()
txtDEPTH.Focus()
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Page.IsPostBack Then
Dim wcICausedPostBack As WebControl = CType(GetControlThatCausedPostBack(TryCast(sender, Page)), WebControl)
Dim indx As Integer = wcICausedPostBack.TabIndex
Dim ctrl = From control In wcICausedPostBack.Parent.Controls.OfType(Of WebControl)()
Where control.TabIndex > indx
Select control
ctrl.DefaultIfEmpty(wcICausedPostBack).First().Focus()
End If
End Sub
Protected Function GetControlThatCausedPostBack(ByVal page As Page) As Control
Dim control As WebControl = Nothing
Dim ctrlname As String = page.Request.Params.[Get]("__EVENTTARGET")
If ctrlname IsNot Nothing AndAlso ctrlname <> String.Empty Then
control = page.FindControl(ctrlname)
Else
For Each ctl As String In page.Request.Form
Dim c As Control = page.FindControl(ctl)
If TypeOf c Is TextBox OrElse TypeOf c Is DropDownList Then
control = c
Exit For
End If
Next
End If
Return control
End Function

Which version are you using? Because, when I try to set focus() of a Textbox, the compiler gives me an error, because there is no such method. However, HTML provides an attribute, which auto focuses an focusable element.
Solution:
TextBoxToSetFocus.Attributes.Add("autofocus", "")
Regards,
Maheshvara

Related

When I try add insert a value into a asp.net Detailsview I get a "Object reference not set to an instance of an object"

What do I do to fix an "Object reference not set to an instance of an object" error. Which object is it referring to?
code:
Private Sub dvSMasterCurrentYear_DataBound(sender As Object, e As EventArgs) Handles dvSMasterCurrentYear.DataBound
Dim dv As DetailsView = New DetailsView
If DetailsViewMode.Insert Then
DirectCast(dv.FindControl("PlantYear"), TextBox).Text = GetYear()
End If
End Sub
Get Year returns to current year, it appears in the detailsview textbox "PlantYear". I try to insert the value using the above code.
thanks for your help.
Most likely FindControl is not actually finding the control. It would be wise to put a check in to ensure it has actually found what you intended it to find:
Private Sub dvSMasterCurrentYear_DataBound(sender As Object, e As EventArgs) Handles dvSMasterCurrentYear.DataBound
Dim dv As DetailsView = New DetailsView
If DetailsViewMode.Insert Then
Dim ctl = dv.FindControl("PlantYear")
If ctl IsNot Nothing Then
DirectCast(dv.FindControl("PlantYear"), TextBox).Text = GetYear()
Else
Throw New Exception("Control was not found")
End If
End If
End Sub

how to get the text value of dynamically created textbox

I want to get the value of my textbox that I created dynamically when I click a button
I need to do this cause the value of my textbox is used for retrieve data from database
how could I achieved this thing??
the flow is Button click - creating textbox - filling textbox with value - Button Click - Get Text of textbox
here is my code to make the textbox
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For i As Integer = 0 To 4
textbox = New TextBox With {.ID = "TextBox" & i}
plchld.Controls.Add(textbox)
Next
End Sub
I have tried something like this but the code didn't work
Protected Sub OkButton_Click(sender As Object, e As EventArgs) Handles OkButton.Click
Dim a(5) As String
For i As Integer = 0 To 4
a(i) = CType(plchld.FindControl("Textbox" & i), TextBox).Text
Next
End Sub
thanks in advance for any help
edit for the answer
I've found the way to solve this. I use request.form to get the value of my textbox.
Thanks for anyone that participating
Regards,
Julian
This is how I have done in my asp.net application.
Creating dynamic control
TextBox txtDate = new TextBox();
txtDate.EnableViewState = true;
txtDate.ID = "PreixValue" + 1;
txtDate.Text = "07 Feb 2014"
pnl.controls.add(txtdate);
To retrieve the value from that textbox
DateTime datefrom = DateTime.Now ;
for (int cnt = 0; cnt < Request.Form.Count; cnt++)
{
if (Request.Form.AllKeys[cnt].Contains("Prefixvalue"))
{
int ParamStartPoint = Request.Form.AllKeys[cnt].IndexOf("Prefix") + 4;
int ParamNameLength = Request.Form.AllKeys[cnt].Length - ParamStartPoint;
string[] ControlName = Request.Form.AllKeys[cnt].Substring(ParamStartPoint, ParamNameLength).Split('$');
if (ControlName[0] == "Date From")
{
datefrom = DateTime.Parse(Request.Form[cnt]);
//datefrom has value now
}
}
}
This is how I have done in my web application, but there may be other ways achieve this.
basically when you create Dynamic control in webform this will be available through Request.Form.
hope this helps you.
Protected Sub OkButton_Click(sender As Object, e As EventArgs) Handles OkButton.Click
Dim a(5) As String
For i As Integer = 0 To 4
Dim anotherObj As TextBox = Me.Controls.Item("Textbox" & i)
a(i) =anotherObj.Text
Next
The issue is that dynamic controls are lost on a postback so when the OkButton click event is handled, there is nothing inside your plchld control. You must recreate your controls with the same ID on postback if you want to retrieve the text in the textboxes.
Using your code, all you need to do is on postback determine if the textboxes were created and if so, recreate them.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' Determine if the text boxes were created and if so, recreate them.
If CBool(ViewState("TextBoxesCreated")) Then
CreateTextBoxes()
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
CreateTextBoxes()
ViewState("TextBoxesCreated") = True
End Sub
Private Sub CreateTextBoxes()
For i As Integer = 0 To 4
plchld.Controls.Add(New TextBox With {.ID = "TextBox" & i})
Next
End Sub
Protected Sub OkButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles OkButton.Click
Dim a(4) As String
For i As Integer = 0 To 4
a(i) = CType(plchld.FindControl("Textbox" & i), TextBox).Text
Next
End Sub
I don't know the full extent of what you are doing but I would suggest not creating them dynamically if you don't need to. Just show or hide the textboxes instead.
Reference: http://www.codeproject.com/Articles/3684/Retaining-State-for-Dynamically-Created-Controls-i

Missing Controls in page after timer method ticks

I have some code that handles a timer method, that just updates a label and changes it to a last updated at this time.
Protected Sub specialNotesTimer_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles specialNotesTimer.Tick
Label1.Text = "Panel refreshed at: " + DateTime.Now.ToLongTimeString()
End Sub
When it ticks however, any other control on the page cannot be found via a button press. Image buttons or my other dynamically created controls, w.e the case, are missing.
I have a function that finds the fired control on the page, then returns the control so I can determine what to do
Public Shared Function GetPostBackControl(ByVal thePage As Page) As Control
Dim myControl As Control = Nothing
Dim ctrlName As String = thePage.Request.Params.Get("__EVENTTARGET")
If ((ctrlName IsNot Nothing) And (ctrlName <> String.Empty)) Then
myControl = thePage.FindControl(ctrlName)
Else
For Each Item As String In thePage.Request.Form
Dim c As Control = thePage.FindControl(Item)
If (TypeOf (c) Is System.Web.UI.WebControls.Button) Then
myControl = c
End If
Next
End If
Return myControl
End Function
This works before the timer.tick, but not after. The control won't be listed as an Item in thePage.Request.Form, and it'll throw a null exception, when I know the control is there.

set field to a value after DetailView is updated

After the DetailsView is updated and clicked the Update Command button on the DetailView; I want to set one of the fields to “Complete”. What am I doing wrong here? Pls. help.
here is my code behind:
Protected Sub Test(sender As Object, e As System.Web.UI.WebControls.DetailsViewUpdatedEventArgs) Handles DetailsView1.ItemUpdated
If IsPostBack Then
Dim TextBox7 As TextBox = TryCast(DetailsView1.Rows(0).Cells(0).FindControl("TextBox7"), TextBox)
TextBox7.Text = "Complete"
End If
End Sub
I have used this method and got it to work.
Protected Sub PageDetailsView_ItemCommand(sender As Object, e As DetailsViewCommandEventArgs)
If Me.PageDetailsView.DefaultMode = DetailsViewMode.[ReadOnly] Then
If e.CommandName.Equals("GenerateModifiedDate", StringComparison.CurrentCultureIgnoreCase) Then
Dim modifiedDateLabel As Label = TryCast(Me.PageDetailsView.FindControl("ModifiedDateLabel"), Label)
modifiedDateLabel.Text = DateTime.Now.ToString()
End If
Else
If e.CommandName.Equals("GenerateModifiedDate", StringComparison.CurrentCultureIgnoreCase) Then
Dim modifiedDateTextbox As TextBox = TryCast(Me.PageDetailsView.FindControl("ModifiedDateTextbox"), TextBox)
modifiedDateTextbox.Text = DateTime.Now.ToString()
End If
End If
End Sub

whats wrong in this code?

whats wrong in this code
i have a Imagebutton2 in gridview whose commandname is xxx and i have added modalpopup extendar panel2 with ImageButton2 i want when image button 2 will be clicked then the modalpopup will display and retrieve the values from selected gridview row to the literal3 control of panel 1 which acts as a modalpopup control for gridview ?
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
Dim myrow As GridViewRow = CType(CType(e.CommandSource, Control).NamingContainer, GridViewRow)
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
If e.CommandName = "xxx" Then
Dim lab5 As Label = DirectCast(myrow.FindControl("Label5"), Label)
Dim lit3 As Literal = Me.Panel2.FindControl("Literal3")
lit3.Text = lab5.Text
End If
End Sub
Many things can fail:
e.CommandArgument may not be an integer
e.CommandSource may not be of Control type
the NamingContainer may not be of GridViewRow type
myRow can be null, at least when the code is ran
the "Label5" control may not be found, or may not be of Label type, when the code is ran
the "Literal3" control may not be found (null ref) or may not be of Literal type, when the code is ran
...

Resources