p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 2.0 Basics (http://p2p.wrox.com/forumdisplay.php?f=136)
-   -   Custom Postback and Gridview refresh (http://p2p.wrox.com/showthread.php?t=39118)

wirerider February 27th, 2006 07:20 PM

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!

wirerider February 27th, 2006 10:58 PM

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.

jbenson001 February 28th, 2006 01:37 AM

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


wirerider February 28th, 2006 01:19 PM

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!

jbenson001 February 28th, 2006 01:57 PM

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.


wirerider February 28th, 2006 04:33 PM

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


All times are GMT -4. The time now is 07:35 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.