hey out there, this is driving me nuts! i'm trying to transfer a number of 'DataKeys' from one page to a second page. i can get the values to come across using 'Server.Transfer' but when a button on the receiving page is clicked (causing a postback), the url changes and i lose the values. how do i keep them?! it's a simple 'post page ones values to page two, perform a search on page two by clicking a button, select an option and go back to the 1st page' kinda thing. on my initial page i've a gridview: <asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_onRowCommand" DataKeyNames="UserID,FirstName" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="userid" HeaderText="userid" />
<asp:BoundField DataField="firstname" HeaderText="firstname" />
asp:TemplateField
<ItemTemplate>
<asp:Button ID="btnSearchPage" runat="server" Text="Button" CommandName="gotoSearchPage" CommandArgument='<%# Container.DataItemIndex %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView> and this is my 'OnRowCommand' command: Protected Sub GridView1_onRowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) 'grab the row info Dim strCmdName As String = e.CommandName.ToString Dim intDataItemIndex As String = e.CommandArgument.ToString 'get the row details Context.Items("UserID") = GridView1.DataKeys(intDataItemIndex).Item("UserID") Context.Items("FirstName") = GridView1.DataKeys(intDataItemIndex).Item("FirstName") If (strCmdName = "gotoSearchPage") Then Server.Transfer("~/searchPage.aspx", True) End If End Sub
on my receiving page 'searchPage.aspx' i can get the DataKeys using this in the page init: Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init If (Not IsPostBack) Then intFacilityID = Context.Items("FacilityID") End If End Sub
but the moment i click the search button, the url changes and hey presto - no more data keys?! this must be possible?! any suggestions would be great, cheers, jake
jake williamson
Posts
-
server.transfer gridview DataKeys from 1st page to 2nd page, lost values on postback? -
cant set text of two password fields on update pagehey Venk259, thanks for getting back to me - have a feeling this should be a simple one, just needs the knowledge! the 'dtr' is a SqlDataReader thats populated using a stored procedure: Dim dtr As SqlDataReader i then set the form fields with the values, for example: txtUserName.Text = dtr.Item("username").ToString which works for the 1st password field but not the 2nd! hope that makes things a bit clearer, thanks for any suggestions, jake
-
cant set text of two password fields on update pagecoding: aspx web pages in vb.net 2.0 database: sql server express this is a weird one... i'm trying to set the text of two password fields from a database query and it wont let me! this is in the code behind: txtPassword.Text = dtr.Item("password").ToString txtPasswordCompare.Text = dtr.Item("password").ToString and this is the page:
Please retype password
the 1st text box 'txtPassword' gets populated but not the 2nd? i can get round it using: txtPassword.Attributes.Add("value", dtr.Item("password").ToString) txtPasswordCompare.Attributes.Add("value", dtr.Item("password").ToString) but then the passwords render as live text in the source code - far from ideal... is there a way round this?! any suggestions would be great, cheers, jake -
setting sessions from a database queryhi richard, wow, thanks for the reply! finally feeling like i'm getting somewhere with the beast that is asp.net! the funny thing is it's beginning to remind me of the flash actionscripting i did a while back... interesting. taking your advice with the div thing to show success/failure i've put the code together as a 'finished' product. there's some area's i'm still unsure about: 1) cant figure out why i have to create a 'SqlDataAdapter' and then a 'DataTable' and then fill them respectively - the whole thing seems a bit long winded! 2) idealy i would like to show the returned data back to the page without using labels as they create additional code i dont need. for example, the 1st label returns as: userID = <span id="lblUserID">1</span> and i dont need the span! not sure how to get round that one... 3) tidying up the objects at the end of the script - which do i need to do?! at the moment, i have this: dsUsers.Dispose() dsUsers.Dispose() myAdapter.Dispose() dt.Dispose() conn.Close() do i need to do it?! that aside, here's the front end code (had to put on my site as it's too long for the forum): SettingSessionsFromDatabaseQuery.aspx and this is the code behind file: SettingSessionsFromDatabaseQuery.aspx.vb watcha reckon? any improvements?! thanks again for all your advice, jake
-
setting sessions from a database queryhi richard, thank you for your post, it's really helped me out! as you say, it's swings and roundabouts; there's never a '100% this is the only way you do it' solution with programming and it's always good to get a 2nd opinion to make sure your heading in the right direction. i agree with using stored procedures instead of inline sql but for forum posts, i usually type it out to avoid confusion with column names etc (also handy when someone spots a blunder in the sql!). everything is now working, the only thing i've left to solve is this: i currently show the session values back to the page in text box's. as all my asp code in a 'code behind' file, i can response.write a message (or bind it to a label) back if no data is returned sort of like this: If (dt.Rows.Count = 0) Then Response.Write("no data returned") End If but what i cant figure out is how to not show the text boxs on the page, just the message? i know i could do the same if statement on the page, but that means not using the code behind file which i'm very keen on. back to google for a bit me thinks! thanks again for your help, jake
-
setting sessions from a database queryi've made a bit of progress with this problem. this works, but i'm *pretty* sure that it's not the right way to do it. this code 'loops' through all the database returns but in my example, there should be only one row returned. i also cant figure out how to do a (this is in english not code):
if (dataSet is empty) Then set a error message Else create the sessions End If
hmmm, tricky. this is the code i've put together:Dim conn As SqlConnection = [con string here] Dim dsProjects As DataSet = New DataSet() Dim cmd As SqlDataAdapter = New SqlDataAdapter("SELECT userID, firstName, lastName FROM users WHERE userID = 1", conn) cmd.Fill(dsProjects, "tblProjects") Dim dr As DataRow Dim dt As DataTable = dsProjects.Tables("tblProjects") For Each dr In dt.Rows Session("clientID") = dr("clientID") Session("clientID") = dr("firstName") Session("clientID") = dr("lastName") Next
what do you reckon out there? am i going about this the right or wrong way?! cheers, jake -
setting sessions from a database query1st post so go easy! coding: aspx web pages in vb.net 2.0 database: sql server express i would like to set session variable values from a database query and i cant find out how to do it! the page flow would be something like this: 1) query database using 'SELECT userID, firstName, lastName FROM users WHERE userID = 1' 2) if data is returned: create userID, firstName and lastName sessions from the database values for those columns. 3) if no data is returned tell the user an error has occurred. 4) finally display the session values or the error message on the page. i can find thousands of tutorials on how to bind data to controls (GridView, DataList, Repeater etc) but none telling me how to query the database and set sessions, cookies or page variables... if anyone can shed some light on this, it'd be great! thanks out there, jake