![](/img/trans.png)
[英]ASP.NET MVC 4 How to add rows to table containing dropdownlistfor
[英]how to add redirect to asp.net rows
我有這樣的網格,
<asp:GridView ID="pgv" CssClass="Grid" runat="server" ShowHeader="false" AllowPaging="False" OnRowCreated="pgv_RowCreated"></asp:GridView>
這就是我的填充方式
List<FruitObject> fruitList = new List<FruitObject>();
fruitList = GetFruitNameAndURL();
pgv.DataSource = fruitList;
pgv.DataBind();
但是現在,對於每一行水果,我都有一個URL,在單擊時,我希望行導航到該URL,無論如何,我可以在頁面加載中執行此操作嗎?
FruitObject有兩個字符串,即Name和URL。
您可以在RowDataBound事件上執行此操作。
將OnRowDataBound屬性和處理程序添加到GridView聲明中,如下所示:
<asp:GridView ID="pgv" CssClass="Grid" runat="server"
ShowHeader="false"
AllowPaging="False"
OnRowCreated="pgv_RowCreated"
OnRowDataBound="pgv_RowDataBound"></asp:GridView>
在頁面后面的代碼中,添加處理程序
void pgv_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
//get the underlying datasource object for the row
FruitObject fo = (FruitObject)e.Row.DataItem;
e.Row.Attributes.Add("onclick", string.Format("window.location = '{0}", fo.URL));
}
}
這是假設您的FruitObject具有URL屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.