2013-03-08 17 views
3

我無法創建我想要的GridView。 我想讓用戶進入網站並查看附加到數據庫的GridView。 列爲:ID, InsertionTime, Filepath, ProccessedByUser 現在我希望用戶單擊他/她想要處理的文件路徑。當他/她單擊文件路徑時,我希望他們的用戶名(使用內置的asp網站身份驗證登錄)更新(添加)到數據庫中。C#如何在GridView上創建Hyperlink事件?

我的標記是標準的,我沒有必要用代碼來管理。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="ID" DataSourceID="AccessDataSource1" 
    onselectedindexchanged="GridView1_SelectedIndexChanged"> 
    <Columns> 
     <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
      ReadOnly="True" SortExpression="ID" /> 
     <asp:BoundField DataField="starttime" HeaderText="starttime" 
      SortExpression="starttime" /> 
     <asp:HyperLinkField DataNavigateUrlFields="path" DataTextField="path" 
      HeaderText="path" /> 
     <asp:BoundField DataField="user" HeaderText="user" SortExpression="user" /> 
    </Columns> 
</asp:GridView> 

我試過使用HyperlinkField,但它似乎不支持onlick事件。

有什麼建議嗎? 謝謝。

回答

9

我假設您正在尋找具有OnClick事件的LinkButton控件。現在

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="ID" DataSourceID="AccessDataSource1" 
    onselectedindexchanged="GridView1_SelectedIndexChanged"> 
    <Columns> 
     <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
      ReadOnly="True" SortExpression="ID" /> 
     <asp:BoundField DataField="starttime" HeaderText="starttime" 
      SortExpression="starttime" /> 
     <asp:TemplateField HeaderText="Path" SortExpression="Filepath"> 
      <ItemTemplate> 
       <asp:LinkButton ID="LbPath" runat="server" 
        Text='<%# Eval("Filepath") %>' 
        CommandName="PathUpdate" 
        CommandArgument='<%#Bind("path") %>'> 
       </asp:LinkButton> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:BoundField DataField="user" HeaderText="user" SortExpression="user" /> 
    </Columns> 
</asp:GridView> 

可以處理LinkButton'sclick eventGridView'sRowCommand事件。

protected void Gridview1_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
    if (e.CommandName == "PathUpdate") 
    { 
     string path= e.CommandArgument.ToString(); 
     // do you what you need to do 
    } 
} 

請注意,我用了一個TemplateField這在GridView最具活力的列類型,因爲你可以添加你想要的任何東西,甚至嵌套GridView的或UserControls

+0

嘿Tim!非常感謝!你解決了我的問題,但我仍然希望LinkBut​​ton的文本是一個綁定值。我怎樣才能做到這一點?我試過'綁定()',但它不被允許。 – devdc 2013-03-08 12:32:31

+0

@Hedgie:你爲什麼要用'Bind'而不是'Eval'?無論如何,它是隻讀的。編輯我的答案以顯示如何在Text屬性中使用'Eval'。 – 2013-03-08 12:35:57

+0

美麗!奇蹟般有效!非常感謝蒂姆! :) – devdc 2013-03-08 16:59:33

相關問題