如何将gridview的超链接字段的navigationUrl设置为类的属性?
问题描述:
我有一个类,当我定义它的对象时,我必须为其上载一个描述文件,并且描述路径另存为类中的一个属性.所以我想当此类对象加载到gridview中时,有一个列作为超链接,当我单击列中的文本时,它会下载该行的描述文件.
请帮助我
谢谢.
i have a class that when i define an object of it i have to upload a description file for it and the path of description save as a property in class. so i want when the objects of this class load in gridview i have a column as a hyperlink that when i click the text in column it download the description file for that row.
please help me
thank you.
答
您必须在gridview.rowdatabound
事件中执行此操作,请参阅:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx [
You have to do that ingridview.rowdatabound
event, refer this:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx[^]
我以这种方式更改了代码,当我的页面中有一个DropDownList时,我从中选择一个项目,将加载GridHomework的数据.
I changed my code in this way, i have a DropDownList in my page when i select an item from it, the GridHomework''s data is loaded.
protected void dpdGroup_SelectedIndexChanged(object sender, EventArgs e)
{
int GroupId =Convert.ToInt32( dpdGroup.SelectedItem.Value);
CourseGroup group = DB.CourseGroups.Where(g => g.Id == GroupId).First();
IEnumerable<homework> lstHomework = group.GetHomework();
GridHomework.DataSource = lstHomework;
GridHomework.DataBind();
foreach (GridViewRow gvRow in GridHomework.Rows)
{
int HwId = (int)GridHomework.DataKeys[gvRow.RowIndex].Value;
Homework Hw = DB.Homeworks.Where(h => h.Id == HwId).FirstOrDefault();
HyperLink link = new HyperLink();
link =(HyperLink)gvRow.FindControl("HyperLink1");
link.NavigateUrl = "http://" + Request.Url.Authority + "/HomeWorkManagementSystem/Upload/" + Hw.DscripPath;
}
}
</homework>