0

我有一个网格视图,用于在 asp.net 网站中显示文章列表。当用户单击每篇文章(网格视图行)的“显示文章”链接时,如果用户通过身份验证,我希望加载文章文件:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        EnableModelValidation="True" GridLines="None" Width="100%">
        <Columns   >
            <asp:TemplateField>
                <ItemTemplate>
                    <table style="width:100%;border-color:Gray; border-style:solid; border-width:2px; padding:0;">
                     <tr style="background-color:White; width:100%;">
                            <td rowspan="2"  style="height:70px;width:10%" ><img  alt="" src="Images/Article.jpg" width="70px" height="70px"></td>
                            <td><table width="100%"><tr>

                            <td    width="50%"><asp:HyperLink ID="HyperLink1"  runat="server"  
                                    NavigateUrl='<%# Eval("ArticlePath","ArticlesList.aspx?Code={0}") %>'>
                                <p style="font-family:B Titr; font-size:13px;"><%#Eval("ArticleTitle")%></p></asp:HyperLink> </td>                               
                            <td   style="color:Gray; width:30%">article group:<span> <%#Eval("Title")%></span></td>
                            <td  style="text-align:left; width:20%" ><p><%# SDKClass.GetFarsiDate(Eval("ArticleAddedDate", "{0:D}"), true)%></td>                  
                    </tr>
                    <tr >
                   <td colspan="2"><p style="font-family:B Zar; font-size:14px;"><%#Eval("ArticleAbstract")%></p> </td>
                     <td  style=" text-align:left;   font-size:10" > <asp:HyperLink ID="HyperLink2"  runat="server" Text="show article" 
                                    NavigateUrl='<%# Eval("ArticlePath","ArticlesList.aspx?Code={0}") %>'>
                                </asp:HyperLink>
                                </td></tr>
                   </table>
                   </td>

                    </table>
                    <hr style="color:Orange">
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

在页面加载中:

if (Request.QueryString["Code"] != null)
{
    Response.Redirect(Request.QueryString["Code"]);
}

如何检查页面加载中选择的每一行以重定向到其文件?

4

2 回答 2

0

您可以通过将您的替换为如下来使用GridView'RowCommand事件。HyperlinkLinkButton

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    EnableModelValidation="True" GridLines="None" Width="100%"          
    OnRowCommand="GridView1_RowCommand">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="lnkArticle" runat="server"
                    CommandArgument='<%# Eval("ArticlePath") %>' 
                    CommandName="ShowArticle"
                    Text='<%# Eval("ArticleTitle") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

RowCommand事件 :

protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "ShowArticle")
    {
        //Check here if user is authenticated...
        //For example
        if (Session["user"] != null)
            Response.Redirect("~/YourPath" + Convert.ToString(e.CommandArgument));
        //CommandArgument would contain your article path...
    }
}

编辑:在您发表评论后

然后您需要检查用户是否在您正在重定向的页面上通过身份验证。就像下面...

//Page Load event of the page on which user gets redirected,
//After clicking your hyperlink
protected void Page_Load(Object sender, EventArgs e)
{
    //Put here the logic to check if user is authenticated or not...
    if (Session["user"] == null)
        Response.Redirect("~/UnAuthorized.aspx");
}
于 2013-09-11T06:19:08.443 回答
0

在加载网格之前根据用户过滤数据。

于 2013-09-11T05:43:08.223 回答