Cấp bậc tác giả:

DOTNET

Nhận dạng Control Header và Value Assign trong Gridview RowDataBound

Được viết bởi webmaster ngày 22/02/2015 lúc 12:04 AM
Bài viết này sẽ cho bạn thấy làm thế nào có thể nhận dạng control headẻ và gán giá trị trong GridView RowDataBound.
  • 0
  • 6970

Nhận dạng Control Header và Value Assign trong Gridview RowDataBound

Đầu tiên tôi sẽ tạo ra ứng dụng asp.net mới và thêm đoạn code dưới đây vào trang.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Detect Header Control and Assign Value of Gridview RowDataBound In Asp.Net Using C#.Net
    </title>
</head>
<body>
    <form id="form1" runat="server">
    <table width="100%" cellpadding="4" cellspacing="4">
        <tr>
            <td align="left">
                <asp:GridView ID="GridView1" runat="server" Width="100%"
                    AutoGenerateColumns="False"
                    ShowFooter="True" onrowdatabound="GridView1_RowDataBound">
                    <Columns>
                        <asp:TemplateField>
                            <HeaderTemplate>
                                  <asp:Label ID="headerName" runat="server" Text=''></asp:Label>
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                            </ItemTemplate>
                            <FooterStyle Font-Bold="True" />
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <HeaderTemplate>
                                  <asp:Label ID="headerAddress" runat="server" Text=''></asp:Label>
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:Label ID="lblNamAddress" runat="server" Text='<%# Eval("Address") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <HeaderTemplate>
                                  <asp:Label ID="headerUserType" runat="server" Text=''></asp:Label>
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:Label ID="lblUserType" runat="server" Text='<%# Eval("UserType") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>

Trong đoạn mã trên, tôi đã sử dụng ItemTemplate để ràng buộc các trường GridView và sử dụng control label để hiển thị các giá trị sử dụng Eval. Trong HeaderTemplate tôi đã thêm vào control label.

using System;
using System.Data.SqlClient;
using System.Data;
using System.Web.UI.WebControls;
using System.Collections;
namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetDistinctValue();
            }
        }
        private void GetDistinctValue()
        {
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["con"].ToString());
            try
            {
                DataTable objdt = new DataTable();
                string query = "select * from UserDetail;";
                SqlDataAdapter da = new SqlDataAdapter(query, con);
                con.Open();
                da.Fill(objdt);
                con.Close();
                if (objdt.Rows.Count > 0)
                {
                    GridView1.DataSource = objdt;
                    GridView1.DataBind();
                }
            }
            catch
            {
                con.Close();
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                Label headerName = (Label)e.Row.FindControl("headerName");
                Label headerAddress = (Label)e.Row.FindControl("headerAddress");
                Label headerUserType = (Label)e.Row.FindControl("headerUserType");

                headerName.Text = "NAME";
                headerAddress.Text = "ADDRESS";
                headerUserType.Text = "USER TYPE";
            }
        }
    }
}

Mã trên tôi đã sử dụng một chức năng để ràng buộc dữ liệu.

Bây giờ tôi đã thực hiện xong việc chạy ứng dụng để kiểm tra.
gridview-header.png

Nguồn bài viết: DOTNET.VN

BÌNH LUẬN BÀI VIẾT

Bài viết mới nhất

LIKE BOX

Bài viết được xem nhiều nhất

HỌC HTML