I am using the below code behind but I can't seem to get the Primary key field (Id) to be added to the url?
ProtectedSub
addparentDV_ItemInserting(ByVal
sender AsObject,
ByVal
e As
System.Web.UI.WebControls.DetailsViewInsertEventArgs)
Handles
addparentDV.ItemInserting
e.Values("ParentId")
= "1"
Dim
Url AsString
= "~/Content.aspx?"
Dim
Title AsTextBox
= CType(addparentDV.FindControl("Title"),
TextBox)
Dim
Description AsTextBox
= CType(addparentDV.FindControl("TextBox2"),
TextBox)
Set DetailsView DataKeyNames property to the "Id" field and bind the "Id" as a DetailsView BoundField. then obtain the new inserted record Id in DetailsView
ItemInserted (not ItemInseting) event:
Protected Sub addparentDV_Inserted(sender As Object, e As DetailsViewInsertedEventArgs)
If e.Exception = Nothing AndAlso e.AffectedRows = 1 Then
Dim ID As [String] = e.Values("id").ToString()
Dim Url As String = "~/Content.aspx?ID=" + ID
Else
MessageLabel.Text = e.Exception.Message
e.ExceptionHandled = True
e.KeepInInsertMode = True
End If
End Sub
Please mark the replies as answers if they help or unmark if not.
Feedback to us
Diesel1970
Member
75 Points
63 Posts
Add Primary Key to Url of the item to be inserted
Nov 16, 2012 01:14 AM|LINK
I am using the below code behind but I can't seem to get the Primary key field (Id) to be added to the url?
Protected Sub addparentDV_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles addparentDV.ItemInserting
e.Values("ParentId") = "1"
Dim Url As String = "~/Content.aspx?"
Dim Title As TextBox = CType(addparentDV.FindControl("Title"), TextBox)
Dim Description As TextBox = CType(addparentDV.FindControl("TextBox2"), TextBox)
e.Values("Url") = Url & "Title=" & Server.UrlEncode(Title.Text) & "&" & "PageName=" & Server.UrlEncode(Description.Text) & "&" & "Pk=" & e.Values("Id")
e.Values("SortOrder") = "1"
End Sub
I would like to add the next Primary Key Id to the end of the url like:
~/Content.asp?Title=sometitle&PageName=somepage&Pk=2
I just don't know how to get the next record to be inserted when inserting...
thanks
Frank Jiang ...
All-Star
16006 Points
1728 Posts
Microsoft
Re: Add Primary Key to Url of the item to be inserted
Nov 20, 2012 09:33 AM|LINK
Hi,
Set DetailsView DataKeyNames property to the "Id" field and bind the "Id" as a DetailsView BoundField. then obtain the new inserted record Id in DetailsView ItemInserted (not ItemInseting) event:
Protected Sub addparentDV_Inserted(sender As Object, e As DetailsViewInsertedEventArgs) If e.Exception = Nothing AndAlso e.AffectedRows = 1 Then Dim ID As [String] = e.Values("id").ToString() Dim Url As String = "~/Content.aspx?ID=" + ID Else MessageLabel.Text = e.Exception.Message e.ExceptionHandled = True e.KeepInInsertMode = True End If End SubFeedback to us
Develop and promote your apps in Windows Store
Diesel1970
Member
75 Points
63 Posts
Re: Add Primary Key to Url of the item to be inserted
Nov 22, 2012 01:45 AM|LINK