ASP.NET DEMO 14: 如何在 GridView/DataGrid 模板列中使用自动回发的 CheckBox/DropDownList
生活随笔
收集整理的這篇文章主要介紹了
ASP.NET DEMO 14: 如何在 GridView/DataGrid 模板列中使用自动回发的 CheckBox/DropDownList
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
有時候希望在 GridView 模板中使用自動回發的 CheckBox (autopostback=true)?,但是 CheckBox 沒有 CommandName 屬性,因此也就無法在 GridView.RowCommand? 事件中處理,并且如何獲取當前 GridView 行信息呢?
我們可以選擇像處理頁面上普通 CheckBox 的 CheckedChanged 事件的“最原始”的方式。
要點:
1. 注意到 .net 中控件事件委托統一原型 (object sender,? EventArgs e)
第一個參數 sender,表示觸發此事件控件
2. ASP.NET? 服務器控件基類 Control 中定義了屬性 NamingContainer ,表示對服務器控件的命名容器的引用,此引用創建唯一的命名空間。在模板列中的控件就是表示包含此控件的模板列。
注:也可以使用 BindingContainer 屬性獲取,但 MSDN 不建議使用此屬性,
???????? 此屬性主要用于控件開發以及 ASP.NET 引擎內部使用?
????????
下面以 DataGrid/GridView 模板列中使用 CheckBox/DropDownList 為例:
?.aspx
<asp:TemplateField>
????????????????????<ItemTemplate>????????????????????????
????????????????????<asp:CheckBox?ID="chkItem"?runat="server"?OnCheckedChanged="chkItem_CheckedChanged"?AutoPostBack="true"?/>
????????????????????<asp:DropDownList?ID="drpItem"?runat="server"?OnSelectedIndexChanged="drpItem_SelectedIndexChanged"?AutoPostBack="true">
????????????????????????<asp:ListItem></asp:ListItem>
????????????????????????<asp:ListItem?Value="red">red</asp:ListItem>
????????????????????????<asp:ListItem?Value="green">green</asp:ListItem>
????????????????????????<asp:ListItem?Value="orange">orange</asp:ListItem>
????????????????????</asp:DropDownList>
????????????????????</ItemTemplate>
????????????????</asp:TemplateField>
?.aspx.cs
protected?void?chkItem_CheckedChanged(object?sender,?EventArgs?e)
????{
????????CheckBox?chk?=?sender?as?CheckBox;?//?觸發事件的?CheckBox????????
????????GridViewRow?row?=?chk.NamingContainer?as?GridViewRow;?//?GridView?當前行????
????????//?也可以使用?BindingContainer?屬性獲取,但?MSDN?不建議使用此屬性,
????????//?此屬性主要用于控件開發以及?ASP.NET?引擎內部使用?
????????//?GridViewRow?row?=?chk.BindingContainer?as?GridViewRow;
????????row.BackColor?=?chk.Checked???System.Drawing.Color.BlueViolet?:?System.Drawing.Color.White;????????
????????Response.Write(String.Format("選中第?{0}?行",?row.RowIndex?+?1));
????????//?more?codes
????????//?Label?lbl?=?row.FindControl("MyLabelID")?as?Label;
????????//?string?str?=?row.Cells[0].Text;
????????//?
????}
????protected?void?drpItem_SelectedIndexChanged(object?sender,?EventArgs?e)
????{
????????DropDownList??drp?=?sender?as?DropDownList;?//?觸發事件的?DropDownList
????????GridViewRow?row?=?drp.NamingContainer?as?GridViewRow;?//?GridView?當前行????????
????????row.Style.Add(HtmlTextWriterStyle.BackgroundColor,?drp.SelectedValue);
????????Response.Write(String.Format("選中第?{0}?行",?row.RowIndex?+?1));
????????//?more?codes
????????//?Label?lbl?=?row.FindControl("MyLabelID")?as?Label;
????????//?string?str?=?row.Cells[0].Text;
????????//?
????}
protected?void?chkItem2_CheckedChanged(object?sender,?EventArgs?e)
????{
????????CheckBox?chk?=?sender?as?CheckBox;?//?觸發事件的?CheckBox????????
????????DataGridItem?row?=?chk.NamingContainer?as?DataGridItem;????????
????????row.BackColor?=?chk.Checked???System.Drawing.Color.BlueViolet?:?System.Drawing.Color.White;????????
????????Response.Write(String.Format("選中第?{0}?行",?row.ItemIndex?+?1));
????????//?more?codes
????????//?Label?lbl?=?row.FindControl("MyLabelID")?as?Label;
????????//?string?str?=?row.Cells[0].Text;
????????//?
????}
????protected?void?drpItem2_SelectedIndexChanged(object?sender,?EventArgs?e)
????{
????????DropDownList?drp?=?sender?as?DropDownList;?//?觸發事件的?DropDownList
????????DataGridItem?row?=?drp.NamingContainer?as?DataGridItem;?
????????row.Style.Add(HtmlTextWriterStyle.BackgroundColor,?drp.SelectedValue);
????????Response.Write(String.Format("選中第?{0}?行",?row.ItemIndex?+?1));
????????//?more?codes
????????//?Label?lbl?=?row.FindControl("MyLabelID")?as?Label;
????????//?string?str?=?row.Cells[0].Text;
????????//?
????}
說明
1。事實上,對于 Button LinkButton ImageButton 當然也可以采取此中方式,只是我們習慣了 ItemCommand 事件中處理
2。對于 DataList/Repeater? 只要轉換外對應的模板項類型,DataListItem/RepeaterItem
下載
我們可以選擇像處理頁面上普通 CheckBox 的 CheckedChanged 事件的“最原始”的方式。
要點:
1. 注意到 .net 中控件事件委托統一原型 (object sender,? EventArgs e)
第一個參數 sender,表示觸發此事件控件
2. ASP.NET? 服務器控件基類 Control 中定義了屬性 NamingContainer ,表示對服務器控件的命名容器的引用,此引用創建唯一的命名空間。在模板列中的控件就是表示包含此控件的模板列。
注:也可以使用 BindingContainer 屬性獲取,但 MSDN 不建議使用此屬性,
???????? 此屬性主要用于控件開發以及 ASP.NET 引擎內部使用?
????????
下面以 DataGrid/GridView 模板列中使用 CheckBox/DropDownList 為例:
?.aspx
<asp:TemplateField>
????????????????????<ItemTemplate>????????????????????????
????????????????????<asp:CheckBox?ID="chkItem"?runat="server"?OnCheckedChanged="chkItem_CheckedChanged"?AutoPostBack="true"?/>
????????????????????<asp:DropDownList?ID="drpItem"?runat="server"?OnSelectedIndexChanged="drpItem_SelectedIndexChanged"?AutoPostBack="true">
????????????????????????<asp:ListItem></asp:ListItem>
????????????????????????<asp:ListItem?Value="red">red</asp:ListItem>
????????????????????????<asp:ListItem?Value="green">green</asp:ListItem>
????????????????????????<asp:ListItem?Value="orange">orange</asp:ListItem>
????????????????????</asp:DropDownList>
????????????????????</ItemTemplate>
????????????????</asp:TemplateField>
?.aspx.cs
protected?void?chkItem_CheckedChanged(object?sender,?EventArgs?e)
????{
????????CheckBox?chk?=?sender?as?CheckBox;?//?觸發事件的?CheckBox????????
????????GridViewRow?row?=?chk.NamingContainer?as?GridViewRow;?//?GridView?當前行????
????????//?也可以使用?BindingContainer?屬性獲取,但?MSDN?不建議使用此屬性,
????????//?此屬性主要用于控件開發以及?ASP.NET?引擎內部使用?
????????//?GridViewRow?row?=?chk.BindingContainer?as?GridViewRow;
????????row.BackColor?=?chk.Checked???System.Drawing.Color.BlueViolet?:?System.Drawing.Color.White;????????
????????Response.Write(String.Format("選中第?{0}?行",?row.RowIndex?+?1));
????????//?more?codes
????????//?Label?lbl?=?row.FindControl("MyLabelID")?as?Label;
????????//?string?str?=?row.Cells[0].Text;
????????//?
????}
????protected?void?drpItem_SelectedIndexChanged(object?sender,?EventArgs?e)
????{
????????DropDownList??drp?=?sender?as?DropDownList;?//?觸發事件的?DropDownList
????????GridViewRow?row?=?drp.NamingContainer?as?GridViewRow;?//?GridView?當前行????????
????????row.Style.Add(HtmlTextWriterStyle.BackgroundColor,?drp.SelectedValue);
????????Response.Write(String.Format("選中第?{0}?行",?row.RowIndex?+?1));
????????//?more?codes
????????//?Label?lbl?=?row.FindControl("MyLabelID")?as?Label;
????????//?string?str?=?row.Cells[0].Text;
????????//?
????}
protected?void?chkItem2_CheckedChanged(object?sender,?EventArgs?e)
????{
????????CheckBox?chk?=?sender?as?CheckBox;?//?觸發事件的?CheckBox????????
????????DataGridItem?row?=?chk.NamingContainer?as?DataGridItem;????????
????????row.BackColor?=?chk.Checked???System.Drawing.Color.BlueViolet?:?System.Drawing.Color.White;????????
????????Response.Write(String.Format("選中第?{0}?行",?row.ItemIndex?+?1));
????????//?more?codes
????????//?Label?lbl?=?row.FindControl("MyLabelID")?as?Label;
????????//?string?str?=?row.Cells[0].Text;
????????//?
????}
????protected?void?drpItem2_SelectedIndexChanged(object?sender,?EventArgs?e)
????{
????????DropDownList?drp?=?sender?as?DropDownList;?//?觸發事件的?DropDownList
????????DataGridItem?row?=?drp.NamingContainer?as?DataGridItem;?
????????row.Style.Add(HtmlTextWriterStyle.BackgroundColor,?drp.SelectedValue);
????????Response.Write(String.Format("選中第?{0}?行",?row.ItemIndex?+?1));
????????//?more?codes
????????//?Label?lbl?=?row.FindControl("MyLabelID")?as?Label;
????????//?string?str?=?row.Cells[0].Text;
????????//?
????}
說明
1。事實上,對于 Button LinkButton ImageButton 當然也可以采取此中方式,只是我們習慣了 ItemCommand 事件中處理
2。對于 DataList/Repeater? 只要轉換外對應的模板項類型,DataListItem/RepeaterItem
下載
轉載于:https://www.cnblogs.com/Jinglecat/archive/2007/07/29/835817.html
總結
以上是生活随笔為你收集整理的ASP.NET DEMO 14: 如何在 GridView/DataGrid 模板列中使用自动回发的 CheckBox/DropDownList的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 雪儿一片一片是哪首歌啊?
- 下一篇: Using NUnit with Vis