問題
使用 DataGrid加入樣板控制項並繫結Javascript
解決方法
在DataGrid裡加入一個樣版欄位, 裡面放了兩個linkButton主要是做"修改/刪除"功能的呈現
修改功能:導向另一頁面
刪除功能:彈出對話視窗,給使用者確認
DataGrid的是間繫結:ItemDataBound,ItemCommand
.aspx部分
<asp:DataGrid ID="dgData" runat="server" AllowSorting="True" AutoGenerateColumns="False" Width="100%" CellPadding="2"> <ItemStyle Height="25px"></ItemStyle> <HeaderStyle HorizontalAlign="Center" ForeColor="White" BackColor="#006699"></HeaderStyle> <Columns> <asp:TemplateColumn HeaderText="功能"> <ItemStyle Wrap="False" HorizontalAlign="Center"></ItemStyle> <ItemTemplate> <asp:LinkButton ID="lbEdit" runat="server" CommandName="Edit">修改</asp:LinkButton> <asp:Label ID="lblSeparate" runat="server">/</asp:Label> <asp:LinkButton ID="lbDelete" runat="server" CommandName="Delete">刪除</asp:LinkButton> </ItemTemplate> </asp:TemplateColumn> <asp:BoundColumn DataField="Model_status" HeaderText="目前狀態"></asp:BoundColumn> <asp:BoundColumn DataField="cr_date" HeaderText="建立日期" SortExpression="CR_date"></asp:BoundColumn> </Columns> </asp:DataGrid>.vb部分
Protected Sub dgData_ItemDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgData.ItemDataBound If e.Item.FindControl("lbEdit") IsNot Nothing Then Dim lbEdit As LinkButton = CType(e.Item.Cells(0).FindControl("lbEdit"), LinkButton) lbEdit.PostBackUrl = "myPag2.aspx" End If If e.Item.FindControl("lbDelete") IsNot Nothing Then Dim lbDelete As LinkButton = CType(e.Item.Cells(0).FindControl("lbDelete"), LinkButton) lbDelete.Attributes.Add("onclick", "return confirm('確認刪除否?');") End If End Sub Protected Sub dgData_ItemCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgData.ItemCommand If e.CommandName = "Sort" Then' Exit Sub end if '以下寫應處理流程 If e.CommandName = "Delete" Then'確認刪除後所進入的事件 '做刪除的處理動作 Exit Sub end if End Sub
沒有留言:
張貼留言