This is an existing Visual Studio 2003 project (written in VB). There is an input form which contains many fields including a due date field. This field is popultaed based on the value selected in Reason type dropdown and it was also editable. Recently, I had to disable the due date field so that the populated value could not be changed (based on the logged in user). The probelm I am facing is that when the Submit button is clicked, an IE popup window opens and the vlaue in the `due date' field just disappers and becomes blank.
Private Sub btnSubmitOrdersTab_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmitOrdersTab.Click
If Not Me.txtPRCEmail.Value.Length = 0 Or Not hPRCEmail.Value.Length = 0 Then
SubmitClick()
Else
RegisterStartupScript("PRCEmail", "<script language=javascript>window.open (""" & CType(Application("cBaseURL"), String) & "administration/supervisors/Orders/prc_email_reason.aspx"",null,""height=200,width=400,status=yes,toolbar=no,menubar=no,location=no"");</script>")
End If
End Sub
I tried to get the value of the due date just before the RegisterStartupScript() function using duedatevalue = Me.txtDueDate.Value, but it is blank.
Is there any way that I can prevent the due date field from becoming blank. This is not an issue if the due date field is not disabled.
This is the Java Script function I am calling from the onload event of the <body> to disable the due date.
function DisableDueDate()
{
if (udisable == "true")
document.getElementById("divduedate").setAttribute('disabled',true);
else
document.getElementById("divduedate").setAttribute('disabled',false);
}
The reason I used disable is because the due date is in a <td> tag along with a gif next to it (not a server control), which pops up a calender when clicked so that the user can select a date. I want the entire content of the <td> tag disabled- including the gif.
In the btnSubmitOrdersTab_Click event you can save the value of the due date in a Session, and then in the Page_Load you can get the value from the Session and set the due date field with it.
Use readonly instead of using disabling the textbox, if you want to read the value of the texbox on server side.
document.getElementById("divduedate").setAttribute('readonly',true);
OR
document.getElementById("divduedate").readOnly=true
OR
Using jQuery:
$('#readonly').attr("readonly", true)
Disabled attribute will not allow "divduedate" to post its value to the server.
Don't set the Enabled property to False. Instead, use the ReadOnly property and set it to true.
Related
In a web application I am writing, I populate a HTML table with a button press; after the table is populated, then the user can click another button to export the table in a CSV file.
The issue is that, while the table is properly populated at he first button pressing, it appears to be empty when the application query it at the second button press.
A few code to explain the issue:
the HTML in ASPX page
<asp:Button ID="BTNPopulate" runat="server" Text="Populate" />
<table id="Table1" style="border-width: 1px; border-color: Black; padding: 5px" cellspacing="0" runat="server" />
<asp:Button ID="BTNExport" runat="server" Text="Export" />
now the first button
Protected Sub BTNTest_Click(sender As Object, e As EventArgs) Handles BTNTest.Click
Dim row As HtmlTableRow
Dim cell As HtmlTableCell
row = New HtmlTableRow()
row.BgColor = "Gray"
cell = New HtmlTableCell()
cell.Controls.Add(New LiteralControl("Test cell"))
row.Cells.Add(cell)
Table1.Rows.Add(row)
end sub
and the table is properly populated.
I was unable to obtain any info from it and in fact if I call the .Rows.Count method anyway in the second button click code-behind
Protected Sub BTNExport_Click(sender As Object, e As EventArgs) Handles BTNExport.Click
Response.ContentType = "text/csv"
Response.AddHeader("Content-Disposition", "attachment; filename='PFExport.csv'")
CSVBuilder.Append(Table1.Rows.Count)
Response.Write(CSVBuilder.ToString)
Response.Flush()
Response.End()
End Sub
the result in the file is always 0.
On the other hand, if I call the same .Rows.Count property at the end of the populate part of the code, it properly returns the result.
I tried to use the ASPTable instead of the HTMLTable, but the issue is the same.
I assume I am missing something basic here, not sure what.
ASP.NET (or anything on the web ftm) works over HTTP. HTTP is stateless. That means that whatever you do in one request is completely lost when you make a new request.
ASp.NET webforms tries to hide this from you by using something called ViewState. It's essentially a hidden field which it sends back and forth to repopulate your page every time you do a roundtrip. When the server reconstructs the page, it reads the info from the ViewState that was sent in and rebuilds the page from those values.
The problem you're having is that the page is not repopulated yet when your click-handler is triggered. There's two ways to fix this.
(preferred): Instead of reading the data from the table in the UI, just look it up in the database again.
Set a flag on the page in the click-handler and populate the CSV on the PreRender-event.
As explained by Kenneth, my error to think the information to be preserved between requests, while HTTP is stateless.
So I investigate this from a different perspecitve and I ended up using View State to store the information locally and retrieve it to generate the CSV at the Export button press.
So, to store the info I use
ViewState("TableResultsState") = CSVBuilder.ToString
immediately after the table is populated.
For the export button, I do
With Response
.ContentType = "text/csv"
.AddHeader("Content-Disposition", "attachment; filename='PFExport.csv'")
.Write(ViewState("TableResultsState").ToString)
.Flush()
.End()
End With
As the text quantity is limited, from my research I understand this should not have adverse effects.
Again, thanks to Kenneth for pointing out my very basic misunderstanding.
I have 14 Fields in my Web page; and for all of them I use RequiredFieldValidator.
This Validator it works fine for all the fields. And when I have an error it caches it and display it to the proper error message area.
In this area I have a button in order to clear the messages and return to the input page.
After I'm caching an error or two I return to the first page by pressing the reset button... correcting the errors... and go forth.
When I'm passing variables to all the fields (which means everything is good) then normally the Validator has to be change and pass the trap (which I have in my code) with no error messages in it...
Here is the trap code...
If Not String.IsNullOrEmpty(RegisterValidationSummary.ValidationGroup) Then
RegisterValidationSummary.Visible = True
ErrorPanel.Visible = True
btnClr.Visible = True
Return
End If
But no the trap works again and return me to the error area without any error displaying.
Only the reset button is there, and the error list is empty.
In order to be sure that all the errors are cleared I use in the reset button the following
Private Sub ClearValidators(sender As Object, e As EventArgs)
RegisterValidationSummary.ValidationGroup = String.Empty
End Sub
At this point the validators are cleared and the trap is not working... The problem is... that happens even when I have certain errors in my page or not.
Thus I use the following code in order to enable the Validators again.
For t = 0 To RegisterValidationSummary.Page.Validators.Count - 1
RegisterValidationSummary.Page.Validators.Item(t).Validate()
Next
Validators are not enabled and of course the trap is not working.
Is someone how knows what is going on and how I may have a certain error erase from validators... and enabled again in order to validate again the fields?
You are doing the validation wrong. First of all ValidationGroup is used to group validations so in a button click you validate the fields in that group. All server controls have this property so that you could group them together for validations by using the same text in this property. Checking the value in this property will not indicate that your values in your controls are valid or not.
So the following condition is always True
If Not String.IsNullOrEmpty(RegisterValidationSummary.ValidationGroup) Then
Therefore, you'll see the reset button all the time.
And, by doing the following you are removing the RegisterValidationSummary control from the ValidationGroup.
RegisterValidationSummary.ValidationGroup = String.Empty
Therefore, after this point you'll not see any error messages since there's no ValidationSummary in the validation group to show the summary of error messages.
Hope I explained why you are seeying what you are seeing right now. Ok, since we know the issue we can easily fix it now.
You should do something like this. Basically we should make use of Page.Validate() and If (Page.IsValid) Then condition to check whether all fields in the ValidationGroup are valid.
Page.Validate()
If (Page.IsValid) Then
' If this condition is true that means all your fields have correct values
Else
' If this condition is true some of your fields are invalid. You can see that in the Validation summary message.
' Not sure why you do the reset but your reset logic could go here
RegisterValidationSummary.Visible = True
ErrorPanel.Visible = True
btnClr.Visible = True
End If
Hope this helps to resolve your issue.
This is really weird error i'm getting and i'll try and explain as best I can.
I have two pages - Page 1 (form) and Page 2 (completed page)
From page 1 I put a variable into a database and then do a server.transfer to page two like so...
Server.Transfer("Page2.aspx", True)
On page 2 I then grab the variable called paymentOnHold which is set on Page 1 and goes into the database...
Here is how I set paymentOnHold on Page 1
Public Property paymentOnHold() As String
Get
Dim _paymentOnHold As Object = ViewState("paymentOnHold")
If _paymentOnHold IsNot Nothing Then
Return CType(_paymentOnHold, String)
Else
Return Nothing
End If
End Get
Set(ByVal value As String)
If Not String.IsNullOrEmpty(value) Then
ViewState("paymentOnHold") = value
Else
ViewState("paymentOnHold") = Nothing
End If
End Set
End Property
...
paymentOnHold = Date.Now.ToString("yyyyMMddHHmmss")
Here's how I grab the value on Page 2...
Dim myValue As String
If TypeOf PreviousPage Is Page1 Then
myValue = DirectCast(PreviousPage, Page1).paymentOnHold
End If
In my development environment where the databases are local the value in the DB and the value on page 2 both match - as you would expect...
In live environment the DB value is 3 or 4 seconds different (before) the one on Page 2 - even though I do not reset it or anything?
This has been driving me crazy for the last few hours and cannot work it.
Does anyone have any ideas/suggestions as to what might be causing this?
Thanks in advance
This could be an issue of saving the view state in first Page (form-1)
In asp.net Page lifecycle
1. Initalization (controls raise their Init event)
2. Load ViewState (Only on post back)
3. Load PostbackData
4. Load
5. Raise PostbackEvent
6. Save View State
7. Render
Server.Transfer() stops rendering the current page and starts rendering another one.That's why Server.Transfer() cannot be used to redirect to pages served by another server.
If you are doing Server.transfer before Event--> 6. Save View State you are not saving viewstate on the form-1
Solution
Response.redirect and session cache, as it is intended to exist per user and across multiple pages in the application.
Using ViewState in this manner is a brittle solution, because ViewState is not intended to exist outside of the scope of the page it was initiated in, much less passed between pages, which I realize you are not quite doing, but you are getting dangerously close to doing it.
The better approach is to use Session cache, as it was intended to exist per user and span multiple page requests.
Try this:
To store in Session, do this:
Session("PaymentOnHold") = [Date].Now.ToString("yyyyMMddHHmmss")
To retrieve a value from Session, do this:
' First check to see if the value is in Session cache or not
If Session("PaymentOnHold") IsNot Nothing Then
' Everything in Session cache is stored as an object so you need to cast it to get it out
Dim datePaymentOnHold As DateTime = TryCast(Session("PaymentOnHold"), DateTime)
End If
Now the Session value will be available no matter how you navigate to pages (Server.Transfer or Response.Redirect).
I have a div(runat=server) that contain repeater,this repeater is getting is value from datatable
when the datatable is empty I do this line of code in asp .net function
divname.innerHTML="<img src="...>"
on this page there is also a asp:button that fill the datatable with values.
when pressing the button ,the above line isn't excute,the page offcourse is doing postback, but the div content isn't changing back to his original value(the repeater).
What do I have to do in the postback in order to force the div to get his original values?
This is code example:
sub page_load(..
if ispostback=false then
run_div()
end if
end sub
sub run_div()
if datatable.Rows.Count=0 then
divname.innerHTML="<img src="...>"
end if
end sub
sub filldata(....
fill datatable
repeater.datasource=...
repeater.databind
run_div()
end sub
html look like this
<div runat=servver id=divname><asp:repeater>.....
<asp:button onclick="filldata">
Thanks for any help
Assuming the variable named datatable is a System.Data.DataTable, remember that it only exists and had data in the context of a sibgle run of the page lifecycle. if the page is re-generated (whether it's via a postback or an initial load of the page) the DataTable is empty until it's loaded.
However, the Repeater object is stored in teh ViewState. IT will have values on a postback if there were rows on the previous load.
Instead of looking at the number of rows in the table, you should be looking at the Repteater.Items.Count.
If repeater.Items.Count = 0 Then
divname.innerHTML="<img src="...>"
Else
...
End If
Hi
I have two aspx pages page1.aspx and page2.aspx. I have an InsertButton on page1. when I click Insert,I am opening page2.aspx using javasript( window.open). Now, in page2, i am giving some input in textboxes. when click on OK button on page2,that page has to be closed and I have to use those inputs in page1. How can I do this..
Could some one help me pls??
(I tried using session. But It didn't wrk for me.)
thanks,
try storing the values in page2.aspx in session variables.it will definitely work
its so simple....first you must bring that specified value to a session
like,,,,
session["PID"]=text1.text.ToString();
after that you must declare a public string/int/any variable on the another page(where u want to get the session value)
then pass the session["PID"] to the publically declared variable(public string VARNAME)
like,,,,,,
VARNAME=Session["PID"].Tostring();