Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Gridview Hyperlink Question... 1

Status
Not open for further replies.

VisualGuy

Programmer
May 27, 2003
162
US
In my gridview, the first column is a hyperlink field with an account number as the data.

<asp:GridView ID="GridView2" runat="server" AllowSorting="True"
AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
CellPadding="3" CellSpacing="2" AlternatingRowStyle-BackColor="#CCFFFF"
AllowPaging="True" DataKeyNames="Account_Number">
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<Columns>
<asp:HyperLinkField DataTextField="Account_Number" HeaderText="AccountNumber"
NavigateUrl="~/Default2.aspx?myAccount={0}"
SortExpression="Account_Number" />
<asp:BoundField DataField="Full_Name" HeaderText="Full_Name"
SortExpression="Full_Name" />
<asp:BoundField DataField="Last_Name" HeaderText="Last_Name"
SortExpression="Last_Name" />
<asp:BoundField DataField="Social_Number" HeaderText="Social_Number"
SortExpression="Social_Number" />
</Columns>
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />

<AlternatingRowStyle BackColor="#CCFFFF"></AlternatingRowStyle>
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:purgedAccountsConnectionString %>" >
</asp:SqlDataSource>

When I click on the hyperlink, two things happen that are wrong. Although it does take me to the correct page, that page's load doesn't work. The other problem is that the value that I'm trying to pass, doesn't get passed. I looked at the code behind and this is what I saw...

<td><a href="Default2.aspx?myAccount={0}">9999999999999999</a></td><td>JOE SHERIDAN</td><td>SHERIDAN</td><td>666666666</td>
</tr><tr style="color:#8C4510;background-color:#CCFFFF;">
<td><a href="Default2.aspx?myAccount={0}">8888888888888888</a></td><td>ALICE J SHERIDAN</td><td>SHERIDAN</td><td>777777777</td>
</tr><tr style="color:#8C4510;background-color:#FFF7E7;">
<td><a href="Default2.aspx?myAccount={0}">7777777777777777777</a></td><td>ALVA-GAY SHERIDAN</td><td>SHERIDAN</td><td>888888888</td>
</tr><tr style="color:#8C4510;background-color:#CCFFFF;">
<td><a href="Default2.aspx?myAccount={0}">666666666666666</a></td><td>AMY K SHERIDAN</td><td>SHERIDAN</td><td>999999999</td>

Does anyone have ideas?
 
your immediate problem. NavigateUrl expects a string, it doesn't do any formatting of values. you want
Code:
<asp:hyperlinkfield
   datatextfield="Account_Number"
   datanavigateurlfields="Account_Number"
   datanavigateurlformatstring="~/Default2.aspx?myAccount={0}"          
   ... />
some other pointers
DataSources pure evil. there are a number of reasons. the most evident is you cannot debug them. then you have issues of unit tests and SoC.

use css instead of inline styling, it's much easier to manage your dom when it's not cluttered with layout details.

Default2.aspx doesn't express the intent of the page. renaming it view_account_details.aspx or something like that reveals intent. your objects and variables should also follow this expressiveness.

your second snippet of "code" above, is not code behind, it html. code behind refers to the server side code (vb, c#, etc).



Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Jason,

You helped me pass the value!!! I still don't understand why my Page_Load won't work on my default2.aspx.

The code behind the page looks like:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

Dim Account As String = Request.QueryString("myAccount")
TextBox1.Text = Account
End Sub


Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Account As String = Request.QueryString("myAccount")
TextBox1.Text = Account
End Sub

So, you see, that the button and the load do exactly the same things. However, the load event never kicks in. I know, I set a line break as well. Nothing...

When I click the button however, I get what I need.

The page itself looks like this...

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
<html xmlns="<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>


</div>
</form>
</body>
</html>

Nothing special. Just testing right now. What do you think is the problem?
 
drop this on page 2 as a test as well.
Code:
<p><%=Request.QueryString("myAccount")</p>
my guess is page_load isn't wired up correctly. when i was using webforms i would explicitly wire all my events, instead of using the markup. this way I knew what my pages where doing. try this
Code:
public Default2()
{
   Load += testing_query_string;
}

public override void Dispose()
{
   Load -= testing_query_string;
   base.Dispose();
}

private void testing_query_string(EventArgs e)
{
   if(IsPostback) return;
   Textbox1.Text = Request.QueryString("myAccount");
}
you'll need to translate c# to vb.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I translated it, but it doesn't know what Load is...I'm sure I must have to declare it, but as what?

Public Sub New()
Load += testing_query_string
End Sub

Public Overloads Overrides Sub Dispose()
Load -= testing_query_string
MyBase.Dispose()
End Sub

Private Sub testing_query_string(ByVal e As EventArgs)
If IsPostback Then
Exit Sub
End If
Textbox1.Text = Request.QueryString("myAccount")
End Sub
 
Load is the Load event. so however you wire a handler to an event. it might be onload. it's been awhile since I dealt with webforms/page life cycle.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top