想自己用代码动态的加载一些控件到它的编辑模式下,没成想挺麻烦?
1
<
asp:GridView
ID
="GridView1"
runat
="server"
AutoGenerateColumns
="False"
2 DataKeyNames ="ID" DataSourceID ="SqlDataSource1" OnRowCreated ="GridView1_RowCreated" >
3 < Columns >
4 < asp:CommandField ShowDeleteButton ="True" ShowEditButton ="True" />
5 < asp:BoundField DataField ="ID" HeaderText ="ID" InsertVisible ="False" ReadOnly ="True"
6 SortExpression ="ID" />
7 < asp:BoundField DataField ="Title" HeaderText ="Title" SortExpression ="Title" />
8 < asp:BoundField DataField ="Author" HeaderText ="Author" SortExpression ="Author" />
9 < asp:BoundField DataField ="PostTime" HeaderText ="PostTime" SortExpression ="PostTime" />
10 < asp:TemplateField HeaderText ="Sth.Here" >
11 <EditItemTemplate>
12 <asp:CheckBoxList ID="CheckBoxList1" runat="server">
13 </asp:CheckBoxList>
14 </EditItemTemplate>
15 < ItemTemplate >
16 < asp:Label ID ="Label1" runat ="server" Text ='<%# Eval("Title") % > '> </ asp:Label >
17 </ ItemTemplate >
18 </ asp:TemplateField >
19 </ Columns >
20 </ asp:GridView >
看到11~14行了吧,准备在Gridview进入编辑模式后在初始化CheckBoxList1,动态添加Items。
2 DataKeyNames ="ID" DataSourceID ="SqlDataSource1" OnRowCreated ="GridView1_RowCreated" >
3 < Columns >
4 < asp:CommandField ShowDeleteButton ="True" ShowEditButton ="True" />
5 < asp:BoundField DataField ="ID" HeaderText ="ID" InsertVisible ="False" ReadOnly ="True"
6 SortExpression ="ID" />
7 < asp:BoundField DataField ="Title" HeaderText ="Title" SortExpression ="Title" />
8 < asp:BoundField DataField ="Author" HeaderText ="Author" SortExpression ="Author" />
9 < asp:BoundField DataField ="PostTime" HeaderText ="PostTime" SortExpression ="PostTime" />
10 < asp:TemplateField HeaderText ="Sth.Here" >
11 <EditItemTemplate>
12 <asp:CheckBoxList ID="CheckBoxList1" runat="server">
13 </asp:CheckBoxList>
14 </EditItemTemplate>
15 < ItemTemplate >
16 < asp:Label ID ="Label1" runat ="server" Text ='<%# Eval("Title") % > '> </ asp:Label >
17 </ ItemTemplate >
18 </ asp:TemplateField >
19 </ Columns >
20 </ asp:GridView >
Gridview响应编辑模式的事件好像只能是RowEditing了,可是MSDN上确实是写着,这个事件在进入编辑模式前发生,e.Cancel可以控制是否取消编辑操作,晕哦,在GridView1_RowEditing事件里怎么FindControl也搞不到,汗……
尝试了各种方法也不行,可是Gridview也没有个什么“RowEdited”事件呀。
一下午的时间都没搞成,烦了,派个小丫头去给买了包烟,一边享受一边捉摸——MS不至于这么傻呀,肯定是自己XX了。
不过接下来,领导开会……
下班前又PK了两把。
回到家,看了个电视剧大结局就再翻MSDN了,看到了RowCreated眼前一亮!.NET是回传来响应用户动作的,那么进入edit mode也要create row的,然后try了一下,嘿嘿,OK
protected
void
GridView1_RowCreated(
object
sender, GridViewRowEventArgs e)
{
if (e.Row.RowState == DataControlRowState.Edit)
{
CheckBoxList cbl = (CheckBoxList)e.Row.FindControl("CheckBoxList1");
cbl.Items.Add("总算找到你个CBL了!");
}
}
View in browser一下,点了第一行挺满意,再随意点了几下,等等!!!有不正确的!
{
if (e.Row.RowState == DataControlRowState.Edit)
{
CheckBoxList cbl = (CheckBoxList)e.Row.FindControl("CheckBoxList1");
cbl.Items.Add("总算找到你个CBL了!");
}
}
最后发现凡是偶数行都不能加入checkbox。
怎么回事,把所有的State输出了一看,哦,偶数行都有DataControlRowState.Alternate值,哦,那就好办了,与一下就去掉了:
protected
void
GridView1_RowCreated(
object
sender, GridViewRowEventArgs e)
{
if ((e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
{
CheckBoxList cbl = (CheckBoxList)e.Row.FindControl( " CheckBoxList1 " );
cbl.Items.Add( " 总算找到你个CBL了! " );
}
}
{
if ((e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
{
CheckBoxList cbl = (CheckBoxList)e.Row.FindControl( " CheckBoxList1 " );
cbl.Items.Add( " 总算找到你个CBL了! " );
}
}
我就说今天开始要重生了吧……