1.可以用RowCommand事件,不用另外在RowDataBound用e.Row.Attributes另外加事件上去
protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName != "SendMail") return;
int id = Convert.ToInt32(e.CommandArgument);
// do something
}
2.另一種自己寫事件 例如BtnUp_Click
Button btn = (Button)sender;
GridViewRow gvr = (GridViewRow)btn.NamingContainer;
string str_emp_id = gvr.Cells[0].Text;
抓row index 用 gvr.Rowindex
使用Template的特定控制項:gvr.FindControl("Button1");
註 : AutoGenerateColumns="false" Bind後欄位不自動增加
註 : ItemStyle-HorizontalAlign="Center" 文字置中
註 : DataItemIndex等於Binding中的絕對index
由後台餵資料,資料欄位要包含DataField會自動
<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_OnRowCommand" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="name" HeaderText="Name" />
<asp:BoundField DataField="email" HeaderText="Email" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CausesValidation="false" CommandName="SendMail"
Text="SendMail" CommandArgument='<%# Eval("id") %>' OnClick="BtnUp_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
================================================
RowDataBound
e.Row.Cells[0].Attributes["onclick"] = "if (!confirm('確 定 要 歸 還 設 備 嗎?')) { return false; };" + this.Page.ClientScript.GetPostBackEventReference((GridView)sender, "Del$" + e.Row.Cells[0].Text);
整個Row
e.Row.Attributes["onclick"] = "if (!confirm('確 定 要 歸 還 設 備 嗎?')) { return false; };" + this.Page.ClientScript.GetPostBackEventReference((GridView)sender, "Del$" + e.Row.Cells[0].Text);
================================================
點選GridView後回到TOP MaintainScrollPositionOnPostback="false"
在ASPX的最上面那行,如果要保持在點選的位置時就改true
<%@ Page Title="" Language="C#" MasterPageFile="~/it_mg/MasterPage.master" AutoEventWireup="true" EnableEventValidation="false" MaintainScrollPositionOnPostback="false" CodeFile="Default.aspx.cs" Inherits="it_mg_pc_data_Default" %>
========================================================
注意事項 GridView生命週期
要注意,動態加入Control控制項在RowDataBound跟RowCreated的執行,有所不同
要去看一下GridView生命週期
========================================================
抓GridView的值
String strmsg = this.GridView1.Rows[0].Cells[0].Text;
========多個GridView共用一個RowCommand=================================
//這樣只會抓到GridView1
//gv_DataKey = GridView1.DataKeys[int.Parse(gv_cmdArgument)].Value.ToString();
改這樣就可以多個GridView共用一個RowCommand
//gv_DataKey = ((GridView)sender).DataKeys[int.Parse(gv_cmdArgument)].Value.ToString();
======================================================================
html GridView1 DataKeyNames 可以設定多個值,例如:DataKeyNames="ID,ST_ID"
用 , 號分開,DataKeyNames="patno"
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
Panel1.Visible = true;
lblACT.Text = GridView1.DataKeys[int.Parse(e.CommandArgument.ToString())].Value.ToString();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
// string strgw = this.Master.Page.FindControl("GridView1").ClientID.ToString();
// GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
// String js = "javascript:__doPostBack('ctl00$MainContent$GridView1','Select$" + e.Row.RowIndex.ToString() + "'" + ")";
// e.Row.Attributes.Add("onclick", js);
//註冊onclick事件去呼叫RowCommand
e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackEventReference((GridView)sender, "Select$" + e.Row.RowIndex);
}
ASPX
<asp:GridView ID="gvUsers" runat="server" AutoGenerateColumns="False" DataKeyNames="UserID" OnRowDataBound="gvUsers_RowDataBound" RowStyle-CssClass="Row" onrowcommand="gvUsers_RowCommand"> <Columns> <asp:BoundField DataField="FirstName" HeaderText="First Name" ReadOnly="True" /> <asp:BoundField DataField="LastName" HeaderText="Last Name" ReadOnly="True" /> <asp:BoundField DataField="Email" HeaderText="Email" ReadOnly="True" /> <asp:TemplateField> <ItemTemplate> <asp:Button runat="server" Text="SELECT" CommandName="Select" /> </ItemTemplate> </asp:TemplateField> </Columns> <HeaderStyle BackColor="#F06300" Font-Bold="True" ForeColor="#FFFFCC" /> </asp:GridView>
RowDataBound Event
protected void gvUsers_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { object objTemp = gvUsers.DataKeys[e.Row.RowIndex].Value as object; if (objTemp != null) { string id = objTemp.ToString(); //Do your operations } } }
RowCommand Event
protected void gvUsers_RowCommand(object sender, GridViewCommandEventArgs e) { Control ctl = e.CommandSource as Control; GridViewRow CurrentRow = ctl.NamingContainer as GridViewRow; object objTemp = gvUsers.DataKeys[CurrentRow.RowIndex].Value as object; if (objTemp != null) { string id = objTemp.ToString(); //Do your operations } } 抓UpdatePanel裡的控制項 Panel mpContentPlaceHolder = (Panel)UpdatePanel1.FindControl("PanelLabEditor"); foreach (object ctrl in mpContentPlaceHolder.Controls) { if (ctrl is System.Web.UI.WebControls.TextBox) { TextBox textctrl = (TextBox)ctrl; textctrl.Text = ""; } } // 'ctl00$MainContent$GridView1' // Panel mpContentPlaceHolder; // mpContentPlaceHolder = // (Panel)Master.FindControl("ctl00$MainContent$PanelLabEditor"); protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { string str1 = e.CommandArgument.ToString(); string str2 = e.CommandName.ToString(); string str3 = e.CommandSource.ToString(); if (str2 == "Select") { PanelLabEditor.Visible = true; lblACT.Text = " 修改 - 檢驗資料 "; btnAdd.Visible = false; btnMod.Visible = true; btnDel.Visible = true; string labpk = GridView1.DataKeys[int.Parse(e.CommandArgument.ToString())].Value.ToString(); BindLabData(labpk); System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "opennewwindow", "myFunction()", true); } else { PanelLabEditor.Visible = false; } } <script type="text/javascript"> function myFunction() { alert('test'); } </script> 讓資料文字置中,要一個一個欄位各別設定
20150507補充
if (e.Row.RowType == DataControlRowType.DataRow) //判斷是否數據行; { DataRowView drv = (DataRowView)e.Row.DataItem; string haveimg = drv["Hd_HaveImage"].ToString(); string uid = drv["Hd_Id"].ToString(); } 可以直接讀取數據庫中未綁定到列的字段. 或者可以用 if (e.Row.RowType == DataControlRowType.DataRow) //判斷是否數據行; { string haveimg = DataBinder.Eval(e.Row.DataItem , "Hd_HaveImage").ToString(); } 隱藏欄位 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow) { // e.Row.Cells[0].Visible = false; //設欄位隱藏 e.Row.Cells[1].Visible = false; //設欄位隱藏 } } 控製項利用CommandArgument 資料繫結的動作 要先轉TemplateField再去繫結 Eval("繫結的欄位") 這樣就能傳key值了 protected void LinkButton1_Click(object sender, EventArgs e) { annex_oid_ULR = "pic_view.aspx?annex_oid=" + ((LinkButton)sender).CommandArgument.ToString(); ClientScript.RegisterStartupScript(GetType(), "message", "<script >window.open('" + annex_oid_ULR + "', 'annex_pic', config='height=600,width=800, location=no,status=no,toolbar=no').focus();</script>"); } 前台寫法 OnClientClick = "if (confirm('您確定要刪除嗎?')==false) {return false;}" CommandField 刪除前跳出詢問視窗 如果不轉TemplateField的話,可以用下列的方式增加 另外就是如上另開視窗javascript:window.open protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { rowView = (DataRowView)e.Row.DataItem;//抓GridViewRowData annex_oid_ULR = "pic_view.aspx?annex_oid=" + rowView["annex_oid"]; e.Row.Cells[4].Attributes["onclick"] = "javascript:window.open('" + annex_oid_ULR + "', 'annex_pic', config='height=600,width=800, location=no,status=no,toolbar=no').focus();"; //CommandField 刪除跳出詢問視窗 btn = (Button)e.Row.Cells[0].Controls[0]; if (btn.Text == "刪除")//刪除鈕才加提示訊息 { btn.OnClientClick = "if (confirm('你 確 定 要 删 除 : " + rowView["annex_name"] + " ?')) javascript:__doPostBack('GridView1','Delete$" + e.Row.RowIndex.ToString() + "'); else return false;"; } } } | ||||
沒有留言:
張貼留言