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!

Hyperlink control 1

Status
Not open for further replies.

Jouster

Programmer
Dec 4, 2000
82
US
Hi,

New to the asp.net and I'm trying to figure out how to dynamically add text to the navigateUrl property.

I am databinding to a grid and i want a column for hyperlinks.

Here is the code I have:

<asp:TemplateField HeaderText="">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:HyperLink ID="lnkAddToCart" runat="server" ImageUrl="~/assets/common/icons/addtocart.gif"
NavigateUrl="~/commerce/cart.aspx?AddPartNo=">Add To Cart.</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>

I want to dynamically fill in the part number after the "cart.aspx?AddPartNumber=" how is this handled in asp.net?

thanks,

Rick
 
There are at least a couple of ways I can think of to do this.

One would be to use a HyperLinkField rather than the TemplateField with a Hyperlink in it, and then set the DataNavigateUrlFormatString.

Another would be to use the RowDataBound event and the FindControl method to get a reference to the Hyperlink control and then modify it's NavigateUrl property.

Mark,

Darlington Web Design
Experts, Information, Ideas & Knowledge
ASP.NET Tips & Tricks
 
Could you possibly point me to some examples of these?

Thanks,
Rick
 
The first method can be found by looking at the help files:


and the second would be something along the lines of:
Code:
    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        Select Case e.Row.RowType
            Case DataControlRowType.DataRow
                Dim h As HyperLink = CType(e.Row.FindControl("MyHyperlink"), HyperLink)
                h.NavigateUrl = ...
        End Select
    End Sub




Mark,

Darlington Web Design
Experts, Information, Ideas & Knowledge
ASP.NET Tips & Tricks
 
Thank you, your help is much appreciated as I am new to asp.net.
 
I think I'm getting close, but the data from the cell is not showing up.
Here is the code:

void RefurbPartsListGrid_RowDataBound(Object sender, GridViewRowEventArgs e)
{

if(e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink h = (HyperLink)e.Row.FindControl("lnkAddToCart");
h.NavigateUrl = String.Format("~/outlet/test.asp?AddPartNo={0}",e.Row.Cells[0].Text);
}
}

I've tried a few different things, still can't get the part number to show in the link...

Thanks,Rick
 
use the underlying dataitem to get the value, not the value from the grid.
 
I've tried:

DataBinder.Eval(Container, "DataItem.ItemCode")

and

dt.Rows[e.Row.RowIndex].Cells[0].Value

is that what you mean?
Neither one worked.

 
Code:
Dim dr As DataRow = DirectCast(dataitem, DataRow)
h.NavigateUrl = String.Format("~/outlet/test.asp?AddPartNo={0}",dr("The column name you want").ToString

You will have to convert it to C#
 
Sorry jbenson001 I don't understand what your trying to do. I am new to ASP.Net, but know ASP very well.

I am reposting my event code here:

void RefurbPartsListGrid_RowDataBound(Object sender, GridViewRowEventArgs e)
{

if(e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink h = (HyperLink)e.Row.FindControl("lnkAddToCart");
h.NavigateUrl = String.Format("~/outlet/test.asp?AddPartNo={0}",e.Row.Cells[0].Text);
}
}



I can't belive this has never been done before. I've been trying to find an example with no luck. MSDN does it this way and it doesn't work (go figure).

Thanks for any help...
 
the code I posted goes in your databound event of the grid

Create the datarow object first
then do the second line in your If statment

like i said, I wrote it in VB, you need to convert to C#
 
This is the part I don't understand completely(changed to c#):

DataRow dr = (DataRow)dataitem;
Compiler has no idea what dataitem is. and the table i did the first query to databind to the grid is not in the scope??

 
I am saying that my code is in VB, you will have to convert to C# since that is the language you are using

Also, what are you binding the datagrid to? Are you using a datasource control, a dataset, datatable?
 
Sorry, should have posted the code...

myConnectionString = new OleDbConnection();
myConnectionString.ConnectionString= "Provider=SQLOLEDB.1;database=PFA_Live;Server=xxxxxx;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Use Encryption for Data=False;Tag with column collation when possible=False;uid=xxxxxxx;pwd=xxxxxxx;";

String rnSQL = "SELECT * FROM Refurb_View ORDER By ItemName ASC";
da = new OleDbDataAdapter(rnSQL, myConnectionString);
da.Fill(ds);
dt = ds.Tables[0];
myConnectionString.Close();
RefurbPartsListGrid.DataSource=dt;
RefurbPartsListGrid.DataBind();



}

void RefurbPartsListGrid_RowDataBound(Object sender, GridViewRowEventArgs e)
{

if(e.Row.RowType == DataControlRowType.DataRow)
{


HyperLink h = (HyperLink)e.Row.FindControl("lnkAddToCart");
h.NavigateUrl = String.Format("~/outlet/test.asp?AddPartNo={0}",e.Row.Cells[0].Text);


}
}


</script>



<asp:Content ContentPlaceHolderID="mainslot" runat="server" ID="main">
<table width="100%"><tr><td style="height: 116px">
<b><asp:literal ID="welcome" runat="server" Text="Welcome to the Pro-Face Outlet Store" /></b><br /><br />
<asp:GridView
ID="RefurbPartsListGrid"
runat="server"
AllowPaging="False"
EmptyDataText="No Parts Found"
AllowSorting="False"
Width="100%"
AutoGenerateColumns="False"
OnRowDataBound="RefurbPartsListGrid_RowDataBound">
<RowStyle CssClass="npbody" />
<HeaderStyle HorizontalAlign="Center" CssClass="npsubheader" />
<EmptyDataRowStyle CssClass="npemtpy" Height="50px" />
<Columns>
<asp:TemplateField HeaderText="nothing" >
<ItemStyle HorizontalAlign="Center" width="200px" height="200px"></ItemStyle>
<ItemTemplate>
<asp:literal ID="ItemCode" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.ItemCode")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Available Outlet Products" >
<ItemStyle HorizontalAlign="Center" width="200px" height="200px"></ItemStyle>
<ItemTemplate>
<img src='/outlet/images/<%# DataBinder.Eval(Container.DataItem, "U_XXDPFA_OutletPic") %>' alt="" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top"></ItemStyle>
<ItemTemplate>
<b>Part Number:</b> <asp:Literal ID="PartNo" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ItemCode") %>' visible="true"></asp:Literal><br /><br />
<b>Description:</b> <asp:Literal ID="PartDesc" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ItemName") %>' visible="true"></asp:Literal><br /><br />
<b>Outlet Price:</b> <asp:Literal ID="PartPrice" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Price","{0:c}") %>' visible="true"></asp:Literal><br /><br />
<b>Quantity Available:</b> <asp:Literal ID="PartQty" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "OnHand","{0:F0}") %>' visible="true"></asp:Literal><br />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:HyperLink ID="lnkAddToCart" runat="server" ImageUrl="~/assets/common/icons/addtocart.gif" >Add To Cart.</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerSettings Position="TopAndBottom" Mode="Numeric" />
<PagerStyle HorizontalAlign="Right"></PagerStyle>
</asp:GridView>
 
Try:
Code:
h.NavigateUrl = String.Format("~/outlet/test.asp?AddPartNo={0}",e.Item.DataItem("You column name")
 
Good try but here is the error:

Compiler Error Message: CS0117: 'System.Web.UI.WebControls.GridViewRowEventArgs' does not contain a definition for 'Item'

 
what event are you putting the code in?
What version of Visual Studio are you using?
 
in the _RowDataBound event as shown above.

I'm editing the page from a text editor. The compiler message is in the browser when i try to view the page.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top