【问题标题】:C# asp.net Gridview navigate to url in gridview rowC# asp.net Gridview 导航到 gridview 行中的 url
【发布时间】:2017-07-24 09:07:37
【问题描述】:

我目前正在做一个内部项目,我创建了一个连接到 SQL 表的 GridView,如下所示:

GridView1

我使用以下代码创建了视图内容按钮:

<Columns>
    <asp:ButtonField ButtonType="Button" Text="View Content" CommandName="Select" />
    <asp:BoundField DataField="ContentID" HeaderText="ContentID" InsertVisible="False" ReadOnly="True" SortExpression="ContentID" />
    <asp:BoundField DataField="Code" HeaderText="Code" SortExpression="Code" />
    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
    <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
    <asp:BoundField DataField="URL" HeaderText="URL" Visible="true" SortExpression="URL" />                
</Columns>

但这就是我现在卡住的地方。 我想点击查看内容按钮并让它导航到所选行上的 URL。

URL 来自 SQL 表,并且有一个字符串,所以我想,它需要先转换,但我可能错了。

我开始把我的代码放在下面:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewCommandEventArgs x = (GridViewCommandEventArgs)e;
    if (x.CommandName == "Select")
    {
        GridViewRow row = GridView1.SelectedRow;
    }
}

【问题讨论】:

    标签: c# url gridview


    【解决方案1】:

    gridviewGridView1_RowCommand 事件中添加您的代码:

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
           if (e.CommandName == "Select")
            {
                GridViewRow row = (GridViewRow)(((BoundField)e.CommandSource).NamingContainer);
                int index = row.RowIndex;
    
                string url = (GridView1.Rows[index].FindControl("URL") as BoundField).Text;
                Response.Redirect(url); // url can be from your sql table
    
            }    
    }
    

    注意:不要忘记在 GrindView 中添加 OnRowCommand 事件&lt;asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand" &gt;

    【讨论】:

    • 感谢您的回复。我听从了你的建议,现在它似乎想做点什么,但我收到以下错误。无法将“ASP.enquirehomepage_aspx”类型的对象转换为“System.Web.UI.WebControls.GridViewRow”类型。有什么想法吗?
    • 将您的 urlLabel 转换为 BoundField.. 看我的编辑
    • 我已进行了建议的更改,但出现以下错误。 无法将类型 'system.web.ui.control' 转换为 'system.web.ui.webcontrols.boundfield'。 这是在将 Label 更改为 BoundField 之后。你熟悉这个错误吗?我试图自己研究它,但在这种情况下没有发现任何有用的东西。
    • 使用Control 而不是BoundField
    • Control 不包含 Text 的定义。
    【解决方案2】:
                GridViewRow row = (GridViewRow)(((BoundField)e.CommandSource).NamingContainer);
                int index = row.RowIndex;
    
                string url = (GridView1.Rows[index].FindControl("URL") as BoundField).Text;
                Response.Redirect(url); // url can be from your sql table
    

    所以我做出了改变。不幸的是,BoundField 不包含NamingContainer 的定义。

    另外,GridView1.Rows[index].FindControl("URL") as BoundField 由于以下错误而失败,无法通过引用转换、装箱转换将类型“system.web.ui.control”转换为“system.web.ui.webcontrols.boundfield”,拆箱转换或空类型转换。

    【讨论】:

    • 所以你必须在templatefield 中转换你的gridview 才能使用我的答案:)
    猜你喜欢
    • 2012-12-25
    • 2013-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多