|
-
Feb 10th, 2009, 04:11 AM
#1
Thread Starter
Hyperactive Member
<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:
<body ms_positioning="GridLayout" id="myBody" runat="server">
ASP Code:
Private Sub dgSchoolName_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgSchoolName.ItemCommand
Dim pStrSql As String, myds As New DataSet, mydt As DataTable
Dim Txtbox As TextBox = DirectCast(e.Item.Cells(1).FindControl("txtSchoolNo"), TextBox)
Dim Txtbox_1 As TextBox = DirectCast(e.Item.Cells(2).FindControl("txtSchoolNameA"), TextBox)
Dim FTextbox As String = Txtbox.Text
Dim FTextbox_1 As String = Txtbox_1.Text
Dim myBody As System.Web.UI.HtmlControls.HtmlGenericControl = CType(Page.FindControl("myBody"), System.Web.UI.HtmlControls.HtmlGenericControl)
If FTextbox = vbNullString Then
General_F.Setfocus(Txtbox, Page, "Form1")
MsgBox1.alert("School No Cannot Be Blank")
Exit Sub
ElseIf FTextbox_1 = vbNullString Then
General_F.Setfocus(Txtbox_1, Page, "Form1")
MsgBox1.alert("School Name Cannot Be Blank")
myBody.Attributes.Add("onload", "document.getElementById('txtSchoolNo').value = '" & FTextbox & "'")
Exit Sub
End If
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.
-
Feb 10th, 2009, 10:17 AM
#2
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.
-
Feb 10th, 2009, 10:35 PM
#3
Thread Starter
Hyperactive Member
Re: <VS03>How to make the textbox text remain after postback
 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:
Private Sub dgSchoolName_ItemDataBound1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgSchoolName.ItemDataBound
If e.Item.ItemType = ListItemType.Footer Then
Dim txtSchoolNo As TextBox = CType(e.Item.Cells(1).FindControl("txtSchoolNo"), TextBox)
Dim txtSchoolName As TextBox = CType(e.Item.Cells(2).FindControl("txtSchoolNameA"), TextBox)
If Not txtSchoolNo Is Nothing And Not viewstate("SchoolNo") = vbNullString Then
txtSchoolNo.Text = viewstate("SchoolNo")
End If
If Not txtSchoolName Is Nothing And Not viewstate("SchoolName") = vbNullString Then
txtSchoolName.Text = viewstate("SchoolName")
End If
end sub
Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender
Dim intFooter As Integer = dgSchoolName.Controls(0).Controls.Count - 2
Dim txtSchoolNo As TextBox = CType(dgSchoolName.Controls(0).Controls(intFooter).FindControl("txtSchoolNo"), TextBox)
Dim txtSchoolName As TextBox = CType(dgSchoolName.Controls(0).Controls(intFooter).FindControl("txtSchoolNameA"), TextBox)
If Page.IsPostBack Then
ViewState("SchoolNo") = txtSchoolNo.Text
ViewState("SchoolName") = txtSchoolName.Text
End If
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.
-
Feb 10th, 2009, 11:14 PM
#4
Thread Starter
Hyperactive Member
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.
-
Feb 11th, 2009, 02:00 PM
#5
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?
-
Feb 12th, 2009, 01:26 AM
#6
Thread Starter
Hyperactive Member
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.
-
Feb 13th, 2009, 05:55 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|