【问题标题】:How do I edit data being bound to a datagrid?如何编辑绑定到数据网格的数据?
【发布时间】:2010-12-31 10:43:48
【问题描述】:

参考Link loaded into my gridview try to navigate to my local server。我在数据网格中的列是客户编号、描述、链接。

我设置了一个在 rowDataBound 上调用的函数,但是如何检索行中的链接以便我可以对其进行编辑,然后将其重新绑定到数据网格?

protected void grdLinks_RowDataBound( object sender, GridViewRowEventArgs e )
{
    if ( e.Row.RowIndex == 2 )
    {

    }
}

这是我的 gridview 代码

<asp:GridView ID="grdLinks" runat="server" AutoGenerateColumns="False" DataSourceID="ldsCustomerLinks"
            OnRowDataBound="grdLinks_RowDataBound" EmptyDataText="No data was returned."
            DataKeyNames="ID" OnRowDeleted="grdLinks_RowDeleted" Width="80%" BackColor="White"
            HorizontalAlign="Center" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
            CellPadding="3" GridLines="Vertical">
            <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
            <Columns>
                <asp:BoundField DataField="CustomerNumber" HeaderText="Customer Number" SortExpression="CustomerNumber" />
                <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
                <asp:HyperLinkField DataTextField="Link" HeaderText="Link" SortExpression="Link" DataNavigateUrlFields="Link" Target="_blank" />
            </Columns>
</asp:GridView>

<asp:LinqDataSource ID="ldsCustomerLinks" runat="server" ContextTypeName="ComplianceDataContext"
            TableName="CustomerLinks" EnableDelete="true">
</asp:LinqDataSource>

【问题讨论】:

    标签: c# asp.net datagrid


    【解决方案1】:

    如果我对您的理解正确,您希望获取名为 Link 的数据项的值。如果是这样,那么这样的事情应该可以工作:

    编辑:我认为您要从数据库中获取值Link,对其进行操作,然后将HyperLink 的Url 设置为新的,操纵值,如果是这样,那么它看起来像这样:

    ASPX 页面(已更新以反映发布的代码)

    <Columns>
        <asp:BoundField DataField="CustomerNumber" HeaderText="Customer Number" SortExpression="CustomerNumber" />
        <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
        <asp:TemplateField HeaderText="Link"> 
            <ItemTemplate>
                <asp:HyperLink ID="hlParent" runat="server" Text='<% #(Eval("Link")) %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    

    我通过添加ID 并从HyperLink 控件中删除对NavigateUrl 属性的引用来修改原始问题的ASP。

    代码

    protected void grdLinks_RowDataBound( object sender, GridViewRowEventArgs e )
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string link = DataBinder.Eval(e.Row.DataItem, "Link") as string;
            if (link != null && link.Length > 0)
            {
                // "FindControl" borrowed directly from DOK
                HyperLink hlParent = (HyperLink)e.Row.FindControl("hlParent");
                if (hlParent != null)
                {
                    // Do some manipulation of the link value 
                    string newLink = "https://" + link
    
                    // Set the Url of the HyperLink  
                    hlParent.NavigateUrl = newLink;
                }
            }
        }
    }
    

    【讨论】:

    • 是的,这就是我想要的。但是,修改后如何在数据网格中显示更新的链接?
    • 假设链接控件是在网格中定义的,那么您需要执行 DOK 提到的 FindControl 并使用您获取的值进行设置。
    • FindControl 被设置为等于 HyperLink 变量。如何将 HyperLink 变量转换为字符串,然后再转换回 HyperLink?
    • 我已经添加了我的 gridview 代码,如果它清除了任何东西。 hlParent 继续返回 null,尽管我将其更改为 HyperLink Link = (HyperLink).e.Row.FindControl("Link")
    • 您需要将模板字段添加到我拥有的网格中,因为您试图覆盖数据绑定行为。此外,hlParent 为空,因为您需要控件上的“hlParent”ID
    【解决方案2】:

    为 GridView 中的每一行调用 RowDataBound,包括页眉、页脚等。因此,您应该从只检查包含数据的行开始。

    一旦你在一行中,有几种方法可以检查单元格。一种是使用单元格索引(此处为 2)。在您的情况下,这似乎很简单,但如果您重新排列列,则会导致挫败感。

    这是来自MSDN 的示例:

    void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
      {
    
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
          // Display the company name in italics.
          e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";
    
        }
    

    更好的方法是使用带有项目 ID 的 FindControl。

    protected void gvBarcode_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       if (e.Row.RowType == DataControlRowType.DataRow)
       {
         HyperLink hlParent = (HyperLink)e.Row.FindControl("hlParent");
       }
    }
    

    【讨论】:

    • 我不太确定如何通过 FindControl() 方法对其进行编辑。我该怎么做才能从超链接转到字符串并返回到超链接?
    • 在上面的代码 sn-p 中,你有一个对 HyperLink 对象 hlParent 的引用。您现在可以编写诸如 hlParent.NavigateURL = "someURL" 之类的代码。在您的代码中,只需键入 hlParent 和一个点,然后 leet ntellisense 会显示您的选项。
    • 我试过了,但它给了我一个错误:“对象引用未设置为对象的实例。”
    • 感谢您的帮助,我知道了。
    【解决方案3】:

    您可能还想考虑让 gridview 为您做这件事。

    如果您正在尝试这样做,您可以使用 datanavigateurlformatstring 属性插入查询字符串参数。

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hyperlinkfield.datanavigateurlformatstring.aspx

    【讨论】:

    • 这不仅仅是插入参数,我必须先检查。就像用户输入“google.com”一样,我需要将其格式化为“google.com”,或者如果他们输入“www.stackoverflow.com”,我需要将其格式化为“stackoverflow.com”如果有用 datanaviagteformatstring 做到这一点,那么我很高兴知道。
    • 好吧,这个网站会自动格式化它,但我需要添加 http:// 和 www。链接到用户未输入的链接,使其成为绝对 URL 而不是相对 URL
    • 啊我现在明白了——当用户输入它时你会修改它吗?还是仅用于展示?
    • 我保留用户输入的内容并显示它,但我修改了导航 URL。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-28
    • 2011-08-06
    • 2016-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多