在GridView的超链接指定的恒定值 [英] Specifying constant value in the gridview hyperlink

查看:176
本文介绍了在GridView的超链接指定的恒定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HyperLinkField字段列的GridView,其中DataNavigateUrlFormatString是如下:

I have a gridview with a HyperLinkField column where the DataNavigateUrlFormatString is as following:

DataNavigateUrlFormatString="DetailedPage.aspx?OrderNo={0}"

我想补充上述DataNavigateUrlFormatstring另一个值 - 恒 - 这样,被叫页面(DetailedPage可以得到OrderNo两者的价值(动态传递)和所有行具有相同值

I would like to add to the above DataNavigateUrlFormatstring another value – constant - so that the called page (DetailedPage can get both the value of OrderNo (passed dynamically) and the same value for all rows.

例如,URL可能是这样的:
DetailedPage.aspx OrderNo = 100安培;文件名='myfilename.doc

For example, the url would be something like: DetailedPage.aspx?OrderNo=100&filename=’myfilename.doc’

请注意,再次使名'myfilename.doc'是所有行相同的,但将在页面的onload是已知的。理想情况下,我想第二个值(例如myfilename.doc),以从URL隐藏
但是,如果这是不可能的,它仍然可以工作。

Note, again that the name ‘myfilename.doc’ is the same for all rows but will be known in the OnLoad of the page. Ideally I would like the second value (e.g. myfilename.doc) to be hidden from the URL but if this is not possible, it will still work.

我该怎么办呢?

推荐答案

使用模板列超链接控制内,然后在code-背后,只需设置 NavigateUrl 属性的RowDataBound 事件的价值,像这样的:

Use a TemplateField with a HyperLink control inside and then in the RowDataBound event in code-behind, just set the NavigateUrl property to the value, like this:

标记:

<asp:GridView id="GridView1" runat="server"   
              OnRowDataBound="GridView1_RowDataBound">
    <Columns>
        ...Other columns here...
        <asp:TemplateField>
            <ItemTempalte>
                <asp:HyperLink id="HyperLink1" runat="server" Text="Details" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

code-背后:

Code-behind:

protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    // Only interact with the data rows, ignore header and footer rows
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        // Find the hyperlink control by ID
        var theHyperLink = e.Row.FindControl("HyperLink1") as HyperLink;

        // Verify we found the hyperlink before we try to use it
        if(theHyperLink != null)
        {
            // Set the NavigateUrl value here
            theHyperLink.NavigateUrl = String.Format("DetailedPage.aspx?OrderNo={0}&filename='{1}'", theOrderNumber.ToString(), theFileName);
        }
    }
}

请注意: theOrderNumber theFileName 将在从数据库页面的加载确定的值,例如。

Note: theOrderNumber and theFileName would be values determined upon load of the page from the database, for example.

这篇关于在GridView的超链接指定的恒定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆