Results 1 to 7 of 7

Thread: <VS03>How to make the textbox text remain after postback

  1. #1

    Thread Starter
    Hyperactive Member nUflAvOrS's Avatar
    Join Date
    Jul 2007
    Location
    Malaysia/ Currently at Singapore
    Posts
    372

    <VS03>How to make the textbox text remain after postback

    Hi All,

    I design a datagrid which enabled user to edit,delete and add new record inside the grid.

    The add button and textbox control embeded in datagrid footer section.

    I want to do a validation when user trying to insert new record into database.

    What I want to do is check if templatecolumn textbox is blank then pop up a messagebox and

    setfocus to textbox until this stage all is fine but when everything completed the page will postback and

    reload the text inside textbox and cleared.

    I don't want the text inside the textbox clear after BLANK found. It will remain inside the textbox with

    unchanged.

    Hereon, I posted my html code and code behind.

    HTML Code:
    1. <body ms_positioning="GridLayout" id="myBody" runat="server">



    ASP Code:
    1. Private Sub dgSchoolName_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgSchoolName.ItemCommand
    2.         Dim pStrSql As String, myds As New DataSet, mydt As DataTable
    3.         Dim Txtbox As TextBox = DirectCast(e.Item.Cells(1).FindControl("txtSchoolNo"), TextBox)
    4.         Dim Txtbox_1 As TextBox = DirectCast(e.Item.Cells(2).FindControl("txtSchoolNameA"), TextBox)
    5.         Dim FTextbox As String = Txtbox.Text
    6.         Dim FTextbox_1 As String = Txtbox_1.Text
    7.         Dim myBody As System.Web.UI.HtmlControls.HtmlGenericControl = CType(Page.FindControl("myBody"), System.Web.UI.HtmlControls.HtmlGenericControl)
    8.  
    9.         If FTextbox = vbNullString Then
    10.             General_F.Setfocus(Txtbox, Page, "Form1")
    11.  
    12.             MsgBox1.alert("School No Cannot Be Blank")
    13.  
    14.             Exit Sub
    15.         ElseIf FTextbox_1 = vbNullString Then
    16.             General_F.Setfocus(Txtbox_1, Page, "Form1")
    17.             MsgBox1.alert("School Name Cannot Be Blank")
    18.  
    19.             myBody.Attributes.Add("onload", "document.getElementById('txtSchoolNo').value = '" & FTextbox & "'")
    20.  
    21.         Exit Sub
    22.         End If
    23. End Sub


    I have been posted my code above. I hope someone can help me thanks :-)
    Where there is no hope, there can be no endeavor.

    There are two ways of rising in the world, either by your own industry or by the folly of others.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: <VS03>How to make the textbox text remain after postback

    If I'm understanding you right, you're dynamically, on the fly creating a new textbox in a new row in the datagrid and attempting to save that info and on postback, the values are disappearing. Yes? No?

    If you dynamically created some controls in the grid, you'll need to recreate those controls in an event such as Page_Init. This way, the viewstate can be re-applied to those controls and the values preserved.

  3. #3

    Thread Starter
    Hyperactive Member nUflAvOrS's Avatar
    Join Date
    Jul 2007
    Location
    Malaysia/ Currently at Singapore
    Posts
    372

    Re: <VS03>How to make the textbox text remain after postback

    Quote Originally Posted by mendhak
    If I'm understanding you right, you're dynamically, on the fly creating a new textbox in a new row in the datagrid and attempting to save that info and on postback, the values are disappearing. Yes? No?

    If you dynamically created some controls in the grid, you'll need to recreate those controls in an event such as Page_Init. This way, the viewstate can be re-applied to those controls and the values preserved.

    Thanks guru mendhak I have been solved the problem.

    I posted my code here.

    asp Code:
    1. Private Sub dgSchoolName_ItemDataBound1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgSchoolName.ItemDataBound
    2.  
    3. If e.Item.ItemType = ListItemType.Footer Then
    4.             Dim txtSchoolNo As TextBox = CType(e.Item.Cells(1).FindControl("txtSchoolNo"), TextBox)
    5.             Dim txtSchoolName As TextBox = CType(e.Item.Cells(2).FindControl("txtSchoolNameA"), TextBox)
    6.             If Not txtSchoolNo Is Nothing And Not viewstate("SchoolNo") = vbNullString Then
    7.                 txtSchoolNo.Text = viewstate("SchoolNo")
    8.             End If
    9.             If Not txtSchoolName Is Nothing And Not viewstate("SchoolName") = vbNullString Then
    10.                 txtSchoolName.Text = viewstate("SchoolName")
    11.             End If
    12. end sub
    13.  
    14. Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender
    15.  
    16.         Dim intFooter As Integer = dgSchoolName.Controls(0).Controls.Count - 2
    17.         Dim txtSchoolNo As TextBox = CType(dgSchoolName.Controls(0).Controls(intFooter).FindControl("txtSchoolNo"), TextBox)
    18.         Dim txtSchoolName As TextBox = CType(dgSchoolName.Controls(0).Controls(intFooter).FindControl("txtSchoolNameA"), TextBox)
    19.         If Page.IsPostBack Then
    20.             ViewState("SchoolNo") = txtSchoolNo.Text
    21.             ViewState("SchoolName") = txtSchoolName.Text
    22.         End If
    23.     End Sub

    I still have some question to ask you.

    Is it the best way ?

    I need to remove all viewstate ?


    Thanks a billion
    Where there is no hope, there can be no endeavor.

    There are two ways of rising in the world, either by your own industry or by the folly of others.

  4. #4

    Thread Starter
    Hyperactive Member nUflAvOrS's Avatar
    Join Date
    Jul 2007
    Location
    Malaysia/ Currently at Singapore
    Posts
    372

    Re: <VS03>How to make the textbox text remain after postback

    Sorry Guru another problem occured.

    I will get an error if I want to display another page of datagrid.

    Code:
    Specified argument was out of the range of valid values. Parameter name: index 
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
    Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: index
    
    Source Error: 
    
    
    Line 325:        Dim myds As New DataSet
    Line 326:        Dim mydt As DataTable
    Line 327:        Dim Txtbox As TextBox = DirectCast(e.Item.Cells(1).FindControl("txtSchoolNo"), TextBox)
    Line 328:        Dim Txtbox_1 As TextBox = DirectCast(e.Item.Cells(2).FindControl("txtSchoolNameA"), TextBox)
    Line 329:        Dim FTextbox As String = Txtbox.Text
    Above is my error message
    error highlighted on line 327
    Where there is no hope, there can be no endeavor.

    There are two ways of rising in the world, either by your own industry or by the folly of others.

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: <VS03>How to make the textbox text remain after postback

    Yes, your way is fine.

    You're getting an error. What method is this happening in?

  6. #6

    Thread Starter
    Hyperactive Member nUflAvOrS's Avatar
    Join Date
    Jul 2007
    Location
    Malaysia/ Currently at Singapore
    Posts
    372

    Re: <VS03>How to make the textbox text remain after postback

    After I try to open another page in datagrid. (Custom paging).

    By using quickwatch I could not get the textbox value.
    Where there is no hope, there can be no endeavor.

    There are two ways of rising in the world, either by your own industry or by the folly of others.

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: <VS03>How to make the textbox text remain after postback

    Do a quickwatch on

    e.Item.Cells(1).FindControl("txtSchoolNo")

    To see if it evaluates to null.

    Also show your code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width