ASP.NET下GridView中設定連結
阿新 • • 發佈:2019-01-08
在GridView中常常要設定連結,下面局裡說明。
前臺GridView程式碼如下:
<asp:GridView ID="dataview" runat="server" Width="80%" AutoGenerateColumns="False" BackColor="White" GridLines="Horizontal" HeaderStyle-Height="35px" RowStyle-Height="33px" onrowdatabound="grid_CameraInfo_RowDataBound" > <Columns> <asp:HyperLinkField DataTextField="SEL_TIME" Target="_self" HeaderText="日期" ControlStyle-ForeColor="#FF6600" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" HeaderStyle-Width="19.5%" ItemStyle-Width="20%"> <ControlStyle ForeColor="#FF6600" /> </asp:HyperLinkField> <asp:HyperLinkField DataNavigateUrlFields="ROUTE_ID,CROSSING_ID" DataNavigateUrlFormatString="..\Login\Login.aspx?CAMER_NM={0}&SEL_TIME={1}" DataTextField="CAMER_NM" Target="show" HeaderText="老師名稱"/> <asp:BoundField DataField="ROUTE_ID" HeaderText="ID" /> <asp:BoundField DataField="CROSSING_ID" HeaderText="CrossID" /> </Columns> <HeaderStyle HorizontalAlign="Center"/> </asp:GridView>
後臺程式碼如下:
我們看到一種是直接在前臺程式碼中寫好的。一種是通過後臺設定的。protected void grid_CameraInfo_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { HyperLink timeHref = e.Row.Cells[0].Controls[0] as HyperLink; string routeName = DataBinder.Eval(e.Row.DataItem, "ROUTE_ID").ToString(); timeHref.NavigateUrl = "Monitor.aspx?route=" + routeName; } }