Home > database >  How would I create a Custom WebLink and a page that stores objects permanently
How would I create a Custom WebLink and a page that stores objects permanently

Time:01-23

So, The idea that I have is I want to create a page that generates a link after a value is places. Example "Number of Players:" I have a text box taking numbers and enter 8. It then creates a custom link for a game master to give to the players and they can all access this main page. In the main page is a button "create a Character" When a player clicks it it runs through this set of prompts, and various other backend bits creating objects to use in a java backend. in the end it gives the player a link of their character where it allows them to see the character sheet and interact with various other appendages.

The java parts and the running of code after a character object is generated I think I have an idea already but its the custom URL and the locally stores part im not sure how to start. I want to make it so that if another GM opens the website they can generate a new link and it load a new page to store characters on.

Can someone help guide me on some resources to start looking into this idea?

  • Custom url generator
  • Each page locally stores based off webURL and isnt accessible via another different link.

I have the front page made for the "input players" and i have the the code to create a character. im unsure how to make them locally saved and how to generate a custom weblink

CodePudding user response:

Well, the answer is you don't create a whole new link, but in fact (we assume) that you save such information into a database.

Then, in place of providing a whole new link, you use what are called Parmaters to the URL.

in fact, asp.net provides 2 ways to do this.

Older way, URL parameter's:

So, they will look like this, say a booking to a hotel that you made:

  www.mycoolsite.com?MyBooking=123456

or you can use what are called "friendly url's", and the above would look like this:

  www.mycoolsite.com/Bookings/123456

In fact, want a good example? how about THIS VERY POST that you made here on Stackoverflow!!!

The URL and link to THIS post is thus this:

https://stackoverflow.com/questions/75202290/how-would-i-create-a-custom-weblink-and-a-page-that-stores-objects-permanently

Note how the above link looks/works.

In fact, you can type in this link, and get/see/jump/display/goto this question:

https://stackoverflow.com/questions/75202290

So, as you can see, you are quite much to create any URL you want, but they DO NOT REALLY exist.

The above as old style to get this question would look like:

https://stackoverflow.com?questions="75202290"

So, think of any web site you ever used, and note how often the URL has paratmers. Say, some city, and store locaiton

https://stackoverflow.com?City="New York"&StoreNum="1234"

So, in the above examples, you thus on say page load can pull/get/use/play with/enjoy/have those values pulled into your code - say on page load event, and use those values.

So, in effect, you don't "create" or "save" or "have" some new URL, but you have ONE URL, but one that accepts some kind of "value" that you pass, and thus that URL in a round about way represts the "thing".

That thing could be a store number, maybe a thing you purchased, maybe a game you started, or in this case, some new "game" board that people can go to.

So, we assume then that when users create a new game or store, or whatever? You are adding and saving such information into a database.

So, lets assume we have say some list of hotels like this grid view:

    <h3>Hotels</h3>
    <asp:GridView ID="GridView1" runat="server" Width="40%"
        AutoGenerateColumns="False" DataKeyNames="ID" CssClass="table">
        <Columns>
            <asp:BoundField DataField="FirstName" HeaderText="FirstName" />
            <asp:BoundField DataField="LastName" HeaderText="LastName" />
            <asp:BoundField DataField="HotelName" HeaderText="HotelName" />
            <asp:BoundField DataField="City" HeaderText="City" />
            <asp:BoundField DataField="Description" HeaderText="Description" />
            <asp:TemplateField>
                <ItemTemplate>
                    <button id="cmdView" runat="server"  
                        onserverclick="cmdView_Click" >
                        <span aria-hidden="true" > View</span>
                    </button>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

Now, do you want to pull the "list" based on the user "logon"?

If "yes", then you really don't need some special URL, but only ONE, since you can fill out the list of hotels (or active games, or whatever) by simple pull of data from the database.

So, lets assume everyone goes to the hotel list page, and thus the code to load above grid is this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not IsPostBack Then
        LoadGrid()
    End If

End Sub


Sub LoadGrid()

    Dim cmdSQL As New SqlCommand("SELECT * FROM tblHotelsA ORDER BY HotelName")
    GridView1.DataSource = MyrstP(cmdSQL)
    GridView1.DataBind()

End Sub

And now we see/have this:

enter image description here

Now, if I click on a "view" hotel? Then again, I don't need some kind of link, but just to "jump/navigate" to the web page that lists out ONE hotel.

But, we could have that friendly URL.

So, say when I click on a row above, I would grab the hotel name, and then jump to the page to display the ONE hotel, but that ONE page could have that friendly URL (or the old style web parameter's).

So, the code behind could/would do this (the view button click)

Protected Sub cmdView_Click(sender As Object, e As EventArgs)

    Dim btn As HtmlButton = sender
    Dim gRow As GridViewRow = btn.NamingContainer
    Dim intPK As Integer = GridView1.DataKeys(gRow.RowIndex).Item("ID")

    Dim cmdSQL = New SqlCommand("SELECT * FROM tblHotelsA WHERE ID = @ID")
    cmdSQL.Parameters.Add("@ID", SqlDbType.Int).Value = intPK
    Dim rstHotel As DataTable = MyrstP(cmdSQL)

    Dim strHotelName = rstHotel.Rows(0).Item("HotelName")
    Dim strURL As String = $"~/Hotel/{strHotelName}"

    Response.Redirect(strURL)

End Sub

So, if we click on that view button, above will navigate to this URL:

 http://localhost:52216/Hotel/Sandman-Inn

So, the user can save the above URL, or EVEN type it in.

If I type in the above URL by hand, then I would get/see this:

enter image description here

So, note the URL in above.

And the code for that page?

It looks like this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not IsPostBack Then

        Dim fURL As IList(Of String)
        fURL = Request.GetFriendlyUrlSegments

        If fURL.Count = 1 Then
            Dim strSQL As String =
                "SELECT * FROM tblHotelsA WHERE HotelName = @Hotel"
            Dim cmdSQL As New SqlCommand(strSQL)
            cmdSQL.Parameters.Add("@Hotel", SqlDbType.NVarChar).Value = fURL(0)

            Dim rstData As DataTable = MyrstP(cmdSQL)
            fLoader(Me.EditRecord, rstData.Rows(0))

        End If
    End If

End Sub

So, one often does NOT really need some links. Just display the saved game information from the database, and put it into a grid, and then the user can click on a row "view" button, and you pull the record from the database.

However, as noted, OFTEN you will want some type of "link" that the user can save/use/enjoy/share/have and thus one can simple add and adopt URL "parameter's", or as I think looks a lot better is to use friendlty URL's.

This web site (Stack Overflow) was written using .net and aspx pages, but take a look at the current URL of this page - you note that StackOverFlow used a friendly URL. (to be fair, SO used mvc pages, so they probably are cshtml pages, but it still .net, and it using IIS as the web server).

However, code behind does not have to "create" some hyperlink, but from a user point of view, they don't know, nor have to care.

So, your code behind will have the logic to pull the correct data into that page, and in most cases one would assume the information being pulled comes from a database.

  • Related