Analytics

顯示具有 DataGrid 標籤的文章。 顯示所有文章
顯示具有 DataGrid 標籤的文章。 顯示所有文章

2010年8月3日 星期二

[ASP.NET]使用 DataGrid加入樣板控制項並繫結Javascript (Added template using the DataGrid control entry and tying Javascript)


問題

使用 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

2010年7月22日 星期四

[ASP.NET]使用DataGrid超連結資料行繫結資料(Hyperlink data using the DataGrid row bind Profile)


問題

使用DataGrid超連結資料行繫結資料



解決方法

當我們資料來源的Table有欄位是url的位置的時候,如果知道如何使用Gridview的超連結資料行來繫結資料的話,就可以省的很多code囉!

做法如下:
1.對DataGrid點一下=>點右上角的箭頭=>展開功能後點"屬性產生器"
1
2
2.此時會出現DataGrid屬性頁面=>點左邊第2個的"資料行"=>於左邊的"可用資料行"點選"超連結資料行"(HyperLinkField)=>按中間的>按鈕將該欄位加入"選取的資料行"=>可看見點選的"超連結資料行"(HyperLinkField)的欄位被加入了
3
 
3.點擊"選取的資料行"裡剛加入的"超連結資料行"=>可看到下方有"文字欄位"以及"URL欄位(DataNavigateUrlFields)=>
 文字欄位是要繫結資料來源裡的URL說明的欄位名
4
URL欄位(DataNavigateUrlFields)是要繫結資料來源裡(dataset or more)的URL欄位名
5

=>設定好後最下方有"目標",這邊是給你選url繫結後要怎樣顯示,例如跳出新視窗,或是在本身頁面顯示等...
6

=>按下確定,即完成設定!

如何,是不是簡單很多!

2010年7月7日 星期三

[ASP.NET]DataGrid裡有樣板時匯出Excel時寫法(DataGrid was written while exporting Excel template when)


問題

DataGrid裡有樣板時匯出Excel時寫法

背景:匯出EXCEL之前已經有寫過一篇
問題:當GridView裡加入樣板,而樣板裡加入一個TextBox控制項,發現在匯出Excel的時候,整個aspx都被寫入Excel,一整個傻眼,

解決方法

改寫如下
Dim file_name As String = DateTime.Now.ToString("yyyyMMddHHmmss") & ".xls"
Dim myTextWriter As New System.IO.StringWriter

'寫入標題    
myTextWriter.WriteLine("姓名" & vbTab &"電話"&"vbTab&"備註") 

'以迴圈方式讀取DataGrid的值    
For i As Integer = 0 To dgData.Items.Count – 1
myTextWriter.WriteLine(dgData.Items(i).Cells(2).Text & vbTab _    
& dgData.Items(i).Cells(2).Text.Replace("&nbsp;", "") & vbTab _    
& CType(dgData.Items(i).FindControl("txtContract_Content"), TextBox).Text)

Next    

myTextWriter.Close()    
Response.AddHeader("Content-Disposition", "attachment; filename=test.xls")    
Response.ContentType = "application/ms-excel"    
Response.ContentEncoding = System.Text.Encoding.GetEncoding("big5")    
Response.Write(myTextWriter)    
Response.End()

怪了!還是一樣不能解決,明明用StringWriter是以string的方式組資料!!
結果,問題出於CType(dgData.Items(i).FindControl("txtContract_Content"), TextBox).Text雖然抓到了控制項,但....
少了.ToString()??
改成CType(dgData.Items(i).FindControl("txtContract_Content"), TextBox).Text.ToString()就好了!!
這.....太扯了~

2010年5月28日 星期五

[ASP.NET]DataGrid轉成Excel檔(DataGrid converted to Excel files)


問題

DataGrid轉成Excel檔



解決方法

'Create DataSet ds first
myDataGrid.DataSource = ds.Tables("mydataset")
myDataGrid.DataBind()
Response.Clear()
' Set the content type to Excel(設定資料格式內容為EXCEL)
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("Content-Disposition", "attachment;filename=my.xls")
EnableViewState = False
Dim myTextWriter As New System.IO.StringWriter
Dim myHtmlTextWriter As New HtmlTextWriter(myTextWriter)
myHtmlTextWriter.SingleQuoteChar.ToString()
Response.Buffer = True
Response.Charset = "utf-8"
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8")
myDataGrid.RenderControl(myHtmlTextWriter)
'Write the HTML to the browser
Response.Write(myTextWriter.ToString())
Response.End()

熱門文章