 |
|
ทำ link ใน GridView ใน asp.net ด้วย C# ผมอยากทราบวิธีการใส่ link ไปหน้าใหม่ใน dataGridView นะครับ |
|
 |
|
|
 |
 |
|
ใช้การ FindControl ใน Databound น่ะครับ
Code (VB.NET)
<%@ Page Language="VB" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">
Dim objConn As OleDbConnection
Dim objCmd As OleDbCommand
Sub Page_Load(sender As Object, e As EventArgs)
Dim strConnString As String
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="& _
Server.MapPath("database/mydatabase.mdb")&";"
objConn = New OleDbConnection(strConnString)
objConn.Open()
IF Not Page.IsPostBack() Then
BindData()
End IF
End Sub
Sub BindData()
Dim strSQL As String
strSQL = "SELECT * FROM customer"
Dim dtReader As OleDbDataReader
objCmd = New OleDbCommand(strSQL, objConn)
dtReader = objCmd.ExecuteReader()
'*** BindData to GridView ***'
myGridView.DataSource = dtReader
myGridView.DataBind()
dtReader.Close()
dtReader = Nothing
End Sub
Sub Page_UnLoad()
objConn.Close()
objConn = Nothing
End Sub
Private Sub myGridView_RowDataBound(sender As Object, e As GridViewRowEventArgs)
'*** CustomerID ***'
Dim hplCustomerID As Hyperlink = CType(e.Row.FindControl("hplCustomerID"),Hyperlink)
IF Not IsNothing(hplCustomerID) Then
hplCustomerID.Text = e.Row.DataItem("CustomerID")
hplCustomerID.NavigateUrl = "Page.aspx?CustomerID=" & e.Row.DataItem("CustomerID")
End IF
'*** Email ***'
Dim lblName As Label = CType(e.Row.FindControl("lblName"),Label)
IF Not IsNothing(lblName) Then
lblName.Text = e.Row.DataItem("Name")
End IF
'*** Name ***'
Dim lblEmail As Label = CType(e.Row.FindControl("lblEmail"),Label)
IF Not IsNothing(lblEmail) Then
lblEmail.Text = e.Row.DataItem("Email")
End IF
'*** CountryCode ***'
Dim lblCountryCode As Label = CType(e.Row.FindControl("lblCountryCode"),Label)
IF Not IsNothing(lblCountryCode) Then
lblCountryCode.Text = e.Row.DataItem("CountryCode")
End IF
'*** Budget ***'
Dim lblBudget As Label = CType(e.Row.FindControl("lblBudget"),Label)
IF Not IsNothing(lblBudget) Then
lblBudget.Text = FormatNumber(e.Row.DataItem("Budget"),2)
End IF
'*** Used ***'
Dim lblUsed As Label = CType(e.Row.FindControl("lblUsed"),Label)
IF Not IsNothing(lblUsed) Then
lblUsed.Text = FormatNumber(e.Row.DataItem("Used"),2)
End IF
End Sub
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - GridView</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView id="myGridView" runat="server" AutoGenerateColumns="False" onRowDataBound="myGridView_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:HyperLink id="hplCustomerID" runat="server"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CustomerID">
<ItemTemplate>
<asp:Label id="lblCustomerID" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label id="lblName" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:Label id="lblEmail" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CountryCode">
<ItemTemplate>
<asp:Label id="lblCountryCode" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Budget">
<ItemTemplate>
<asp:Label id="lblBudget" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Used">
<ItemTemplate>
<asp:Label id="lblUsed" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
|
 |
 |
 |
 |
Date :
2009-08-06 10:07:25 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลืมบอกครับว่าผมเขียน ASP.NET เขียนแบบ C# นะครับ
|
 |
 |
 |
 |
Date :
2009-08-06 11:00:05 |
By :
loogway |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (C#)
<%@ Page Language="C#" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">
OleDbConnection objConn;
OleDbCommand objCmd;
void Page_Load(object sender,EventArgs e)
{
String strConnString;
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
Server.MapPath("database/mydatabase.mdb") + ";";
objConn = new OleDbConnection(strConnString);
objConn.Open();
if(!Page.IsPostBack)
{
BindData();
}
}
void BindData()
{
String strSQL;
strSQL = "SELECT * FROM customer";
OleDbDataReader dtReader;
objCmd = new OleDbCommand(strSQL, objConn);
dtReader = objCmd.ExecuteReader();
//*** BindData to GridView ***//
myGridView.DataSource = dtReader;
myGridView.DataBind();
dtReader.Close();
dtReader = null;
}
void Page_UnLoad()
{
objConn.Close();
objConn = null;
}
void myGridView_RowDataBound(Object s, GridViewRowEventArgs e)
{
//*** HyperLink CustomerID ***//
HyperLink hplCustomerID = (Label)(e.Row.FindControl("hplCustomerID"));
if (hplCustomerID != null)
{
hplCustomerID.Text = (string)DataBinder.Eval(e.Row.DataItem, "CustomerID");
hplCustomerID.HyperLink = "Page.aspx?CustomerID=" + (string)DataBinder.Eval(e.Row.DataItem, "CustomerID");
}
//*** CustomerID ***//
Label lblCustomerID = (Label)(e.Row.FindControl("lblCustomerID"));
if (lblCustomerID != null)
{
lblCustomerID.Text = (string)DataBinder.Eval(e.Row.DataItem, "CustomerID");
}
//*** Email ***//
Label lblName = (Label)(e.Row.FindControl("lblName"));
if (lblName != null)
{
lblName.Text = (string)DataBinder.Eval(e.Row.DataItem, "Name");
}
//*** Name ***//
Label lblEmail = (Label)(e.Row.FindControl("lblEmail"));
if (lblEmail != null)
{
lblEmail.Text = (string)DataBinder.Eval(e.Row.DataItem, "Email");
}
//*** CountryCode ***//
Label lblCountryCode = (Label)(e.Row.FindControl("lblCountryCode"));
if (lblCountryCode != null)
{
lblCountryCode.Text = (string)DataBinder.Eval(e.Row.DataItem, "CountryCode");
}
//*** Budget ***//
Label lblBudget = (Label)(e.Row.FindControl("lblBudget"));
if (lblBudget != null)
{
lblBudget.Text = DataBinder.Eval(e.Row.DataItem, "Budget").ToString();
}
//*** Used ***//
Label lblUsed = (Label)(e.Row.FindControl("lblUsed"));
if (lblUsed != null)
{
lblUsed.Text = DataBinder.Eval(e.Row.DataItem, "Used").ToString();
}
}
}
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - GridView</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView id="myGridView" runat="server" AutoGenerateColumns="False" onRowDataBound="myGridView_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:HyperLink id="hplCustomerID" runat="server"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CustomerID">
<ItemTemplate>
<asp:Label id="lblCustomerID" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label id="lblName" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:Label id="lblEmail" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CountryCode">
<ItemTemplate>
<asp:Label id="lblCountryCode" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Budget">
<ItemTemplate>
<asp:Label id="lblBudget" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Used">
<ItemTemplate>
<asp:Label id="lblUsed" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
Ref :
(C#) ASP.NET GridView Control - FindControl
(C#) ASP.NET HyperLink - asp:HyperLink
|
 |
 |
 |
 |
Date :
2009-08-06 11:29:46 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตัวที่ผมเขียนมันเป็นแบบนี้ครับ มันไม่ขึ้น link ให้คลิกมันจะติดตรงไหนครับช่วยดูให้หน่อย
Code (C#)
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class index : System.Web.UI.Page
{
private string strcon1;
SqlConnection conn;
SqlConnection conn2;
DataTable dt_data;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["sess_citizenid"].ToString() == "")
{
Response.Redirect("Default.aspx");
}
strcon1 = Session["strcon"].ToString();
conn = new SqlConnection(strcon1);
conn2 = new SqlConnection(strcon1);
this.heard();
}
private void heard()
{
dt_data = new DataTable();
dt_data.Columns.Add(new DataColumn("เรื่อง",typeof(string)));
string sql1 = "SELECT * FROM section";
conn.Open();
SqlCommand cmd1 = new SqlCommand(sql1, conn);
SqlDataReader daread = cmd1.ExecuteReader();
while (daread.Read())
{
DataRow drows = dt_data.NewRow();
string section_id=daread["section_id"].ToString();
drows[0]=daread["section_name"];
//drows["status"] = "0";
dt_data.Rows.Add(drows);
//query ตาราง catagory_help
string sql2 = "SELECT * FROM catagory_help WHERE section_id='" + section_id + "'";
conn2.Open();
SqlCommand cmd2 = new SqlCommand(sql2, conn2);
SqlDataReader daread2 = cmd2.ExecuteReader();
while (daread2.Read())
{
DataRow drows2 = dt_data.NewRow();
string cat_id = daread2["cat_id"].ToString();
drows2[0] = daread2["cat_name"];
//drows2["status"] = "1";
dt_data.Rows.Add(drows2);
}
conn2.Close();
}
conn.Close();
GridView1.DataSource = dt_data;
GridView1.DataBind();
HyperLinkField h = new HyperLinkField();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
h.DataTextField = GridView1.Rows[i].Cells[0].ToString() ;
h.NavigateUrl = "data_user.aspx";
}
}
}
ขอบคุณครับ...
|
 |
 |
 |
 |
Date :
2009-08-06 11:57:21 |
By :
loogway |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอดู source ที่เป็น .aspx หน่อยครับ
|
 |
 |
 |
 |
Date :
2009-08-06 15:37:58 |
By :
superpheak |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (C#)
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="index" Title="tes" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<table border="0" align="center" cellpadding="0" cellspacing="1"
style="width: 956px">
<tr>
<td>
<asp:GridView ID="GridView1" runat="server" CellPadding="4" Width="932px"
BackColor="White" BorderColor="#CC9966" BorderStyle="None"
BorderWidth="1px">
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<RowStyle BackColor="White" ForeColor="#330099" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
</asp:GridView>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</table>
</asp:Content>
|
 |
 |
 |
 |
Date :
2009-08-06 16:04:23 |
By :
loogway |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|