If I am reading this correct, you have a GV, and based on the data from database, you want to use code to cook up, create a URL for the hyper link button in the GV based on that data?
Sure, you can use a hyper link button, or even a plane jane asp.net button in the GV.
Lets do BOTH cases (button, and a hyper link.
so, a simple GV - and we drop in a hyper link, and drop in a button into the GV.
So, our markup looks like this:
Ok, now our code to fill the GV looks like this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadGrid();
}
void LoadGrid()
{
using (OleDbConnection conn = new OleDbConnection(Properties.Settings.Default.AccessDB))
{
using (OleDbCommand cmdSQL = new OleDbCommand("SELECT ID, Animal, PictureName from tblAnimals", conn))
{
conn.Open();
DataTable rstData = new DataTable();
rstData.Load(cmdSQL.ExecuteReader());
GridView1.DataSource = rstData;
GridView1.DataBind();
}
}
}
And now we have this output:
so, now we want some code to take information from each data row, and cook up + create a URL link of anything we want based on the row data.
The code for the hyper link, and also the plane jane button to jump to that new page can thus look like this:
We use the GV data row binding event for this
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// get data row
DataRowView gData = (DataRowView)e.Row.DataItem;
// example set hyper link
HyperLink myHyper = (HyperLink)e.Row.FindControl("HyperLink1");
// create URL based on row data
myHyper.NavigateUrl = "~/Content/Animals/" + gData["PictureName"];
// example buttion click
Button myBtn = (Button)e.Row.FindControl("Button1");
myBtn.CommandArgument = "~/Content/Animals/" + gData["PictureName"];
}
}
So, note how I wired up both a hyper link based on the row data (could be anything you cook up and imagine).
And for good measure, I also wire up the plane jane asp button that I also dropped into the GV. As a result, both the hyper link click, or the button click wind up doing much the same thing - navigation to a new URL on click.
Of couse, for the button click, I shove the url into the command argument - (a handy trick by the way).
Thus, for the hyper link, we don't have (need) a event stub, but for the button click, we have this code (click) event:
protected void Button1_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
Response.Redirect(btn.CommandArgument);
}
so you are quite much free to take the data row information, and based on that information cook up and create any URL you want for that hyper link button, or as the above shows, you can even use a simple button and again, the end result is quite much the same.
Regards,
Albert D. Kallal (Access MVP 2003-2017)
Edmonton, Alberta Canada