[ASP.NET][問答集] GridView篇

  • 7773
  • 0
  • 2012-03-12

摘要:[問答集] GridView篇

這篇文主要是想記錄一些小弟在論壇中解答的實作,不見得是最佳解,但一定是出於個人實作

,以往一些實作範例隨著電腦重灌或是隨手一作,幾乎不太會留下記錄(除了工作上自已的Sample之外)

,想想也是浪費了,反正都花時間實作了,就順手貼上來好了,下一次再有看到類以的問題,也可以

直接給連結Sample看就可以了,呵........

因此一些較小的實作就會更新在同一篇( 我想別為了衝量而衝量比較好 ),若是較複雜的就會考慮另發文了

 

 

案例1:Gridview 特定的資料列樣式 ( 原始問題連結 )

實作:

基本上在GridView的RowDataBound去處理即可,因為GridView在展現資料列時會進行這個事件

所以如果想在資料列上做手腳的,從這個事件下手就沒錯啦

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
   //判斷是否為資料列
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    	//若code = 01時,Row的背景色變成藍色
        if (DataBinder.Eval(e.Row.DataItem, "code").ToString() == "01") 
            e.Row.BackColor = System.Drawing.Color.Blue;            
    }
}

 

 

 

案例2:怎樣將gridview內的literal的內容當databond時變成hyperlink 原始問題連結  )

實作:

基本上在GridView的RowDataBound配合findcontrol即可,原理同上一則

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Literal lt = (Literal)e.Row.FindControl("Literal1");
        lt.Text = " 點部落";
    }
}

 

 

案例3:GridView更新前欄位如何使用Confirm驗證(原始問題連結)

實作:採SqlCommand + GridView方式,程式沒有很嚴謹,例如try-catch之類的,請自行調整

 

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            onrowdatabound="GridView1_RowDataBound">
            <Columns>
                <asp:BoundField DataField="id" HeaderText="ID" />
                <asp:BoundField DataField="code" HeaderText="Code" />
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Button ID="Button1" runat="server" Text="Save" onclick="Button1_Click" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
 public partial class GridViewConfirm : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
	    if(!IsPostBack)
	        DataLoad();
	}



	private void DataLoad()
	{
	    using (SqlConnection conn = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["DBconnStr"].ToString()))
	    {
	        conn.Open();
	        using (SqlCommand command = new SqlCommand())
	        {
	            command.Connection = conn;
	            command.CommandText = "your query sql";
	            IDataReader idr = command.ExecuteReader();
	            this.GridView1.DataSource = idr;
	            this.GridView1.DataBind();
	        }
	        conn.Close();
	    }
	}

	protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
	{
	    if (e.Row.RowType == DataControlRowType.DataRow)
	    {
	        Button bt = e.Row.FindControl("Button1") as Button;
	
	        if (bt != null)
	            bt.Attributes.Add("onclick", "return confirm('確定要修改?');");
	    }
	}

	protected void Button1_Click(object sender, EventArgs e)
	{
	    Button bt = (Button)sender;
	
	    TextBox tb = bt.NamingContainer.FindControl("TextBox2") as TextBox;
	
	    Response.Write(tb.Text+"被刪掉了!!"); //測試用而已,請依需求自行修改
	
	    DataLoad();
	}
}

 

 


 

若本文對您有所幫助,歡迎轉貼,但請在加註【轉貼】及來源出處,並在附上本篇的超連結,感恩您的配合囉。

By No.18