hyperlinks - does an easier way exist?

P

PiotrO

hello everybody,
i'm posting this problem because i spent a few hours
on reading forums, newsgroups, tutorials and others
but i still dont know how to solve my problem.
It concerns displaying hyperlink in a datagrid.
Hyperlink is stored in an sql database as plain text.
my table in the database consists of 3 columns: id, text, link.
i would like to display this link as hyperlink (click and redirect to
another web place)

i'm trying to do this similarly to displaying an image (it works with
images)...so
i created a hyperlink column called hyperlink
could you tell me what i'm doing wrong? maybe there is an easier way to do
this?

<asp:DataGrid id="DataGrid2" runat="server"
OnItemDataBound="DataGrid2_ItemDataBound">
....
<asp:HyperLinkColumn DataNavigateUrlField="id" HeaderText="hyperlink"
NavigateUrl='sklep2.aspx?id=<%#
Convert.ToString(DataBinder.Eval(Container.DataItem,"id")) %>'
</asp:HyperLinkColumn>
....


void DataGrid2_ItemCommand(object sender, DataGridCommandEventArgs e) {
System.Web.UI.WebControls.HyperLink linka = new
System.Web.UI.WebControls.HyperLink();
if((e.Item.ItemType == ListItemType.Item)||(e.Item.ItemType ==
ListItemType.AlternatingItem)){
linka.NavigateUrl = "sklep2.aspx?link="+e.Item.Cells[3].Text;
}
}

and

void BindGrid(){
string conStr = "server=\'(local)\'; trusted_connection=true;
database=\'test\'";
SqlConnection Con = new SqlConnection(conStr);
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM squad", Con);
DataSet ds = new DataSet();
da.Fill(ds,"squad");
DataGrid2.DataSource = ds;
DataGrid2.DataBind();
}


file sklep2.aspx :

void Page_Load(object sender,EventArgs e){
string LinkID = Request.QueryString["id"];
string conStr = "server=\'(local)\'; trusted_connection=true;
database=\'test\'";
SqlConnection Con = new SqlConnection(conStr);
SqlCommand Com = new SqlCommand("SELECT * FROM squad WHERE id="+LinkID,
Con);
try{
Con.Open();
SqlDataReader MYDATA;
MYDATA = Com.ExecuteReader(CommandBehavior.CloseConnection);
Response.Write("<a
href="+MYDATA["link"].ToString()+">link</a>");
Con.Close();
}
catch(SqlException EXEPT){
Span1.InnerHtml = "READING Failed. DETAILS: "+EXEPT.ToString();
}}

best regards,
p.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top