简体   繁体   English

如何从ASP.Net gridview列字段链接到.aspx页

[英]How to link from an ASP.Net gridview column field to an .aspx page

I'm using an asp.net gridview to display a list of data from an mdb file. 我正在使用asp.net gridview显示来自mdb文件的数据列表。 There are 5 columns displayed, with approximately 240 rows of data. 显示5列,其中包含约240行数据。 Each row displays "first name", last name", "title", "genre", "issue", and "id". 每行显示“名字”,“姓氏”,“标题”,“体裁”,“问题”和“ id”。

Each "title" entry has corresponding .aspx page. 每个“标题”条目都有对应的.aspx页。 There are approximately 180 actual .aspx pages that corresponds to the "title" entries in the grid. 大约有180个实际的.aspx页,它们对应于网格中的“标题”条目。 An example of this relationship looks like: title = "Once upon a time in a long story"; 这种关系的一个例子如下:title =“长篇故事中的某个时候”; and the aspx page might be /alongstory.aspx. 并且aspx页面可能是/alongstory.aspx。

What I want to accomplish: Allow users to click the title field on the grid and open corresponding .aspx page. 我要完成的工作:允许用户单击网格上的标题字段并打开相应的.aspx页。

What I've done so far: created a aspLabel on the "title" field - Text='<%#Eval("Title") %>'; 到目前为止,我已经完成了什么:在“标题”字段上创建了一个aspLabel-Text ='<%#Eval(“ Title”)%>'; and added C# code for label load event: 并为标签加载事件添加了C#代码:

    aspLabel l = sender as aspLabel;
    l.ClientsideEvents.Click = String.Format("function(s,e) {{window.location = \"{0}"; }}, GetPageUrl(l));

And the GetPageUrl: 和GetPageUrl:

     Private string GetPageUrl(aspLabel l)
     {GridViewDataItemTemplateContainer c = l.NamingContainer as GridViewDataItemTemplateContainer:
    var value = (string)DataBinder.Eval(c.DataItem, "Title"); 
    string result;
    switch (value) {
         case "in the mirror":
            result = "AnotherTitlePage.aspx";
            break;
        case "When your Eyes":
            result = "AnotherTitlePage2.aspx";
            break;
        case "Her Delivery":
            result = "ATitlePage1.aspx";
            break;
        case "You Never Know What You Might See":
            result = "TitlePage3.aspx";
            break;              
        default:
            result = "TitlePageDoesNotHavesameNameAsDBEntry.aspx";
            break;
    }
    return Page.ResolveUrl(result);
}

While this works, it requires all titles be hard coded into corresponding select statement along with the corrosponding url, which equates to 240 cases! 在此过程中,需要将所有标题以及相应的URL硬编码到相应的select语句中,这相当于240种情况! Further, the only field which uniquely identifies each row is the "id" value, which in an integer - I've had a problem trying to set up a case select for int and return the proper url (casting error is the problem). 此外,唯一标识每一行的唯一字段是“ id”值,该值以整数表示-我在尝试为int设置case select并返回正确的url时遇到了问题(铸造错误是问题)。

What I hope to accomplish: Find the most efficient way to open the corrosponding url for the click on the "title" field of the grid view without hard coding both the field name from the grid column and the url into the case select. 我希望完成的任务:找到最有效的方法来打开相应的URL,以单击网格视图的“标题”字段,而无需将网格列中的字段名称和URL都硬编码为case select。

So, given the information above, would a case select be the best approach for this requirement? 因此,鉴于上述信息,选择案例来满足此要求是最好的方法吗? Any examples or suggestions of better approaches would be most appreciated! 任何更好的方法的例子或建议,将不胜感激!

Instead of making 180 actual .aspx pages, you must have to use concept of routing introduced in c# 4.0 and above. 不必制作180个实际的.aspx页,必须使用c#4.0及更高版本中引入的路由概念。

here you can generate N number of pages dynamically and only one page will be work as template. 在这里,您可以动态生成N个页面,并且只有一页可以用作模板。

please refer below link for more details 请参考以下链接了解更多详情

URL Routing in asp.net as example www.jobdoor.in asp.net中的URL路由,例如www.jobdoor.in

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM