2009-12-31 70 views
1

參考Link loaded into my gridview try to navigate to my local server。我在數據網格中的列是Customer#,Description,Link。如何編輯綁定到數據網格的數據?

我有一個函數在rowDataBound上調用,但是如何檢索行中的鏈接,以便我可以編輯它,然後將它重新綁定到datagrid?

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> 

回答

1

如果我正確認識你,你想獲得一個數據項的值稱爲鏈接。如果是的話,那麼這樣的事情應該工作:

編輯:我想你的意思是,你想從數據庫中抓取值Link,操縱它,然後設置HyperLink到的網址新,操縱值,如果是的話那麼它會是這樣的:

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> 

我修改了ASP從你原來的問題通過添加ID並從HyperLink控件中刪除對NavigateUrl屬性的引用。

代碼

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; 
      } 
     } 
    } 
} 
+0

是的,這就是我想要的。但是,如何在修改後在數據網格中顯示更新後的鏈接? – Justen 2009-12-31 16:14:10

+0

假設鏈接控件是在網格中定義的,那麼你會想要做一個DOK提到的FindControl,並用你抓取的值來設置它。 – CAbbott 2009-12-31 16:16:00

+0

FindControl被設置爲等於HyperLink變量。我如何將HyperLink變量轉換爲字符串,然後返回到HyperLink? – Justen 2009-12-31 16:49:44

1

的RowDataBound被稱爲在GridView的每一行,包括頁眉,頁腳,等等。因此,你應該通過檢查只包含數據的行開始。

一旦你在一排,有幾種方法來檢查單元格。一個是使用單元格索引(這裏是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>"; 

    } 

一個更好的辦法是使用的FindControl與項目的ID。

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

我不太確定如何從FindControl()方法編輯它。我會怎麼做從超鏈接到字符串並返回到超鏈接? – Justen 2009-12-31 16:32:05

+0

在上面的代碼片段中,您提到了HyperLink對象hlParent。您現在可以編寫代碼,例如hlParent.NavigateURL =「someURL」。在你的代碼中,只需鍵入hlParent,一個點和一個ntellisense顯示你的選項。 – DOK 2009-12-31 16:51:23

+0

我試過了,它給了我一個錯誤: 「對象引用未設置爲對象的實例。」 – Justen 2009-12-31 16:55:50

0

你可能也想看看讓gridview爲你做。

如果這是您正在嘗試執行的操作,則可以使用datanavigateurlformatstring屬性插入查詢字符串參數。

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

+0

嗯,它不只是插入參數,我必須先檢查。就像用戶輸入「google.com」一樣,我需要將其格式化爲「http://www.google.com」,或者如果他們輸入「www.stackoverflow.com」,我需要將其格式化爲「http: 「如果有辦法用datanaviagteformatstring來做到這一點,那麼我很樂意知道。 – Justen 2009-12-31 18:10:04

+0

那麼,這個網站autoformats它,但我需要添加http://和www。鏈接到他們沒有被用戶輸入,以便它成爲一個絕對的URL而不是一個相對的URL – Justen 2009-12-31 18:16:13

+0

啊我現在看到 - 你修改它時,用戶鍵入它?或僅用於顯示? – 2009-12-31 18:24:20

相關問題