|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
February 27th, 2006, 07:20 PM
|
Authorized User
|
|
Join Date: Jan 2006
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Custom Postback and Gridview refresh
I'm using javascript to capture information associated with a Gridview and put in hidden fields.
I want to now:
A) Send the page back to the server, using javascript
B) In the codebehind
1) react to the page return
2) extract the hidden fields and update the gridview source
3) initiate a refresh on the gridview
I don't know how to do A, B1 and B3.
Any guidance on how to do these things would be appreciated.
Thanks!
|
February 27th, 2006, 10:58 PM
|
Authorized User
|
|
Join Date: Jan 2006
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Ah...I learned that I could just do a form.submit for A), and pick up the hidden fields in Page_Load (with a judicious use of IsPostBack and value tests).
So the remaining issue is B3.
|
February 28th, 2006, 01:37 AM
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
If you figured out A. then the gridview should refresh. I assume you use a forms(0).submit(); to submit the page. If so, then the gridview should refresh, unless you included the code with in the if not ispostback statement.
Jim
|
February 28th, 2006, 01:19 PM
|
Authorized User
|
|
Join Date: Jan 2006
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for your reply.
Here are the key code elements:
=================the key page elements (other than the gridview, which is generated entirely from the designer)
<asp:HiddenField ID="Hidden1" runat="server" value = ""/>
<input id="Button1" type="button" style="z-index:103; left: 180px; position: absolute; top: 8px;" onClick="javascript:Button1Click()" value="button" />
<input id="Text1" style="z-index: 104; left: 16px; position: absolute; top: 8px"
type="text" />
============client code
<script language=javascript>
//-------------------------
function GetElem(varString){return document.getElementById(varString) }
//-------------------------
function Button1Click() {
//alert(GetElem("Hidden1").value)
alert("text1 = " + GetElem("Text1").value);
GetElem("Hidden1").value = GetElem("Text1").value
alert("Hidden1 = " + GetElem("Hidden1").value);
document.forms[0].submit();
}
alert ("set hidden1 to blank")
GetElem("Hidden1").value = ""
</script>
===============code behind
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack Then
If Me.Hidden1.Value.Trim <> "" Then
MsgBox("updating at server " & Me.Hidden1.Value)
Dim DB As New DB 'the database maintenance class
Call DB.AddRecord(Me.Hidden1.Value)
Me.Hidden1.Value = ""
End If
End If
End Sub
=================================
The DB gets updated...I can see the new record in the table, and the gridview will show it if I recompile.
However,the gridview doesn't refresh, even on a page refresh.
Also, for some reason the value of "Hidden1" doesn't get reset to blanks, so on each page load, it sees a non-blank value in Hidden1, and updates the database again.
Any insight on these points would be appreciated.
Thanks!
|
February 28th, 2006, 01:57 PM
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
I think your update is happening AFTER the grid binds, and that's why you are not seeing the updated records. As for the Hidden1 field, it looks like it should work.
|
February 28th, 2006, 04:33 PM
|
Authorized User
|
|
Join Date: Jan 2006
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for the input.
I got it to work, except for the fact that it doesn't seem to clear out the hidden1 value, so keeps re-adding to the db with each page refresh. But I don't think that's a gridview refresh issue, so I'm going to try to isolate it and figure it out separately.
Here's the solution I arrived at (with thanks to several parties in different forums for promptings that eventually got me to this.)
=====key page elements (except completely vanilla gridview, created in designer, and form1 method = "post")
<input type=hidden id="Hidden1" runat=server value = "" />
<input id="Button1" type="button" style="z-index:103; left: 180px; position: absolute; top: 8px;" onClick="javascript:Button1Click()" value="button" />
<input id="Text1" style="z-index: 104; left: 16px; position: absolute; top: 8px" type="text" />
===========javascript
<script>
function Button1Click() {
document.form1.Hidden1.value = document.form1.Text.value
document.forms[0].submit(); // not very complicated here...a nice surprise
}
</script>
===========codebehind
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack Then
If Trim(Me.Hidden1.Value) <> "" Then ' the re-add hidden1 gets past this :-/
Dim DB As New DB
Call DB.AddRecord(Me.Hidden1.Value) ' the db update method
Me.Hidden1.Value = "" ' the re-add gets past this, too :-/
Me.GridView1.DataBind() ' this was the missing piece for a while
End If
End If
End Sub
|
|
|