Using hyperlink to call page: Alternatives?

Discussion in 'ASP .Net' started by CJM, Jul 4, 2007.

  1. CJM

    CJM Guest

    Bit of a noob question... I'm finally starting to move across .ASP.NET from
    ASP 3.0, and I'm trying to make the most of .NET rather than simply do what
    I did before, albeit with different syntax. I'm converting a simple ASP
    application to .NET as a learning vehicle.

    I have an application that allows the user to search a simple DB containing
    information about machine tooling. The user can search for details of a
    specific item or for groups of items that have specific characteristics.

    In each case, the application returns the same information, and certain
    columns are returned as hyperlinks to allow further searching; for example,
    if I search for Retainer #1 I get one record returned, but if I click on
    the value in the value in the Tip Width field, the the hyperlink calls the
    same page again, but with different search paremters that returns all
    Retainers with a similar Tip Width.

    Snippet:
    <tr>
    <td><a href="viewc105.asp?retainer=<%=iRetNo%>"
    target="_blank"><%=iRetNo%></td>
    <td><a
    href="searchc105.asp?searchval=<%=fTipWidth%>&search=Search&SearchField=2"><%=fTipWidth%></a></td>
    <td><%=.fields("Thickness")%></td>
    <td><%=.fields("Depth")%></td>
    <td><%=.fields("Style")%></td>
    <td><%=.fields("InternalAngle")%></td>
    <td><%=Format3dp(.fields("DevelopedWidth"))%></td>
    <td><a
    href="searchc105.asp?searchval=<%=fFlange%>&search=Search&SearchField=3"><%=fFlange%></a></td>
    </tr>

    Now, I know I *could* easily use the same technique in ASP.NET. I'm using a
    a GridView control to generate the table, so I've configured several of the
    columns as HyperTextField columns.

    My first question is whether there are any alternatives to this technique?
    Rather than submitting the page, then checking for certain QueryString
    fields, is there any better method in ASP.NET? I'm not sure why I think
    there may be better methods, but I just have a felling that there might be.

    My second question is how to actually construct the table that I want to
    render. As I've said, I started using a GridView, but I'm in two minds as to
    whether should bind the Gridview to the DataReader that I've created, and
    then retrospectively, edit the HypertextField columns to point to the right
    hyperlinks; or should I manually construct each row in a While
    DBReader.Read() loop. Any thoughts?

    The overall problem I have at the moment is that I can imagine a dozen ways
    to acheive even the simplest of tasks and all of the received wisdom I've
    accumulated in ASP3.0 can only be thrown out of the window. I'm sure I can
    achieve everything I want somehow, but I'd rather aim for a slick solution
    than a series of ASP3-esque bodges...

    Thanks in advance

    Chris
     
    CJM, Jul 4, 2007
    #1
    1. Advertisements

  2. CJM

    Teemu Keiski Guest

    Hi,

    in ASP.NEt you wouldn't do redirects such as you did with classic asp, at
    least not in same sense. In most cases you'd do a postback which means that
    page is posted back to itself (in ASP sense means redirect to itself but in
    ASP.NEt page has state which is kept on subsequent postbacks with help of
    view state, so it is a lot more than just a redirect).

    With GridView for example, you'd get a event raised as a result to that a
    Link button is clicked (you'd do it using a LinkButton or ButtonField), and
    that event can directly provide arguments related to the row you clicked,
    for example ids, column values etc without the need for you check
    QueryString manually.

    http://asp.net/learn/dataaccess/tutorial28vb.aspx?tabid=63


    --
    Teemu Keiski
    AspInsider, ASP.NET MVP
    http://blogs.aspadvice.com/joteke
    http://teemukeiski.net
     
    Teemu Keiski, Jul 4, 2007
    #2
    1. Advertisements

  3. CJM

    CJM Guest

    Teemu,

    Thanks for the response.

    I'm afraid I've been running around in circles trying to implement what you
    suggested, but I'm getting nowhere.

    I read the article you suggested, unfortunately it covers everything but
    using a LinkButton in a GridView. I can imagine how it will work; when the
    linkbutton is click, its handler will update the SearchVal field, and will
    select (ie set to 'checked') the appropriate radio button before calling
    the original code in the btnSearch_Click sub.

    However, I can't even get the LinkButton to display the appropriate value .
    How do I bind the LinkButton in the TemplateField to the RetainerNo in the
    bound DataReader?

    Here's the relevant code snippets:

    <asp:GridView ID="GridRetainers" AutoGenerateColumns="False"
    runat="server">
    <Columns>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:LinkButton ID="btnRetainer" runat="server"></asp:LinkButton>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField HeaderText="Thick" DataField="Thickness" />
    <asp:BoundField HeaderText="Depth" DataField="Depth" />
    <asp:BoundField HeaderText="Style" DataField="Style" />
    <asp:BoundField HeaderText="Angle" DataField="InternalAngle" />
    <asp:BoundField HeaderText="Dev Width" DataField="DevelopedWidth" />
    </Columns>
    </asp:GridView>


    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles btnSearch.Click

    Dim oConn As New SqlConnection("...connection string...")
    Dim oCmd As New SqlCommand
    Dim drRetainers As SqlDataReader

    oConn.Open()
    With oCmd
    .Connection = oConn
    .CommandType = Data.CommandType.StoredProcedure

    If rdoRetainer.Checked Then
    .CommandText = "mnd_ListRetainersByRetNo"
    .Parameters.Add("RetainerNo", Data.SqlDbType.VarChar, 10).Value =
    SearchVal.Text
    End If
    If rdoTipWidth.Checked Then
    .CommandText = "mnd_ListRetainersByTipWidth"
    .Parameters.Add("TipWidth", Data.SqlDbType.VarChar, 10).Value =
    SearchVal.Text
    End If
    If rdoFlange.Checked Then
    .CommandText = "mnd_ListRetainersByFlange"
    .Parameters.Add("Flange", Data.SqlDbType.VarChar, 10).Value =
    SearchVal.Text
    End If
    End With

    drRetainers = oCmd.ExecuteReader
    With GridRetainers
    .DataSource = drRetainers
    .DataBind()
    .GridLines = GridLines.None
    .CellSpacing = 1

    End With
    End Sub

    Thanks

    Chris
     
    CJM, Jul 5, 2007
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.