GridView在Edit状态的行是Template中的EditItemtemplat中的控件,
某些时候,需要控制这些行,避免对他们进行操作
情况一:如不加!=EditIndex的控制,则RowDataBound将寻找空间HyperLink,而在Edit状态,控件无效
Code
<asp:TemplateField HeaderText="功能组名称">
<ItemTemplate>
<asp:HyperLink ID="Group_Name_Lab" runat="server"
Text='<%#Eval("Function_GroupName") %>'></asp:HyperLink>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<EditItemTemplate>
<asp:TextBox ID="Group_Name_Box" runat="server"
Text='<%#Eval("Function_GroupName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
<asp:TemplateField HeaderText="功能组名称">
<ItemTemplate>
<asp:HyperLink ID="Group_Name_Lab" runat="server"
Text='<%#Eval("Function_GroupName") %>'></asp:HyperLink>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<EditItemTemplate>
<asp:TextBox ID="Group_Name_Box" runat="server"
Text='<%#Eval("Function_GroupName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
处理
Code
1protected void FunctionGroupGridView_RowDataBound(
2object sender, GridViewRowEventArgs e)
3{
4if (e.Row.RowIndex != -1 &&
5 e.Row.RowType == DataControlRowType.DataRow
6&&e.Row.RowIndex!=this.FunctionGroupGridView.EditIndex)
7{
8((HyperLink)(e.Row.FindControl("Group_Name_Lab"))).NavigateUrl
9 =
10"~/AdminManage/w_Function_QueryGroup.aspx?GID="
11+ ((Label)(e.Row.FindControl("Group_ID_Lab"))).Text;
12}
13}
1protected void FunctionGroupGridView_RowDataBound(
2object sender, GridViewRowEventArgs e)
3{
4if (e.Row.RowIndex != -1 &&
5 e.Row.RowType == DataControlRowType.DataRow
6&&e.Row.RowIndex!=this.FunctionGroupGridView.EditIndex)
7{
8((HyperLink)(e.Row.FindControl("Group_Name_Lab"))).NavigateUrl
9 =
10"~/AdminManage/w_Function_QueryGroup.aspx?GID="
11+ ((Label)(e.Row.FindControl("Group_ID_Lab"))).Text;
12}
13}
情况二:如不在EditItemtemplate中写一个相同的HyperLink,则RowDataBound失败,因为无法找到控件
Code
1<asp:TemplateField HeaderText="操作">
2<EditItemTemplate>
3<asp:HyperLink ID="Img_Link" runat="server" Target="_blank"
4Text="察看图片">
5</asp:HyperLink>
6|
7<asp:LinkButton ID="LinkButton1" runat="server"
8CausesValidation="True" CommandName="Update"
9Text="更新">
10</asp:LinkButton>
11<asp:LinkButton ID="LinkButton2" runat="server"
12CausesValidation="False" CommandName="Cancel"
13Text="取消">
14</asp:LinkButton>
15</EditItemTemplate>
16<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
17<ItemTemplate>
18<asp:HyperLink ID="Img_Link" runat="server" Target="_blank"
19Text="察看图片">
20</asp:HyperLink>
21|
22<asp:LinkButton ID="LinkButton1" runat="server"
23CausesValidation="False" CommandName="Edit"
24Text="编辑"></asp:LinkButton>
25|
26<asp:LinkButton ID="LinkButton2" runat="server"
27CausesValidation="False" CommandName="Delete"
28Text="删除" OnClientClick="ConfirmDel()">
29</asp:LinkButton>
30</ItemTemplate>
31<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
32</asp:TemplateField>
1<asp:TemplateField HeaderText="操作">
2<EditItemTemplate>
3<asp:HyperLink ID="Img_Link" runat="server" Target="_blank"
4Text="察看图片">
5</asp:HyperLink>
6|
7<asp:LinkButton ID="LinkButton1" runat="server"
8CausesValidation="True" CommandName="Update"
9Text="更新">
10</asp:LinkButton>
11<asp:LinkButton ID="LinkButton2" runat="server"
12CausesValidation="False" CommandName="Cancel"
13Text="取消">
14</asp:LinkButton>
15</EditItemTemplate>
16<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
17<ItemTemplate>
18<asp:HyperLink ID="Img_Link" runat="server" Target="_blank"
19Text="察看图片">
20</asp:HyperLink>
21|
22<asp:LinkButton ID="LinkButton1" runat="server"
23CausesValidation="False" CommandName="Edit"
24Text="编辑"></asp:LinkButton>
25|
26<asp:LinkButton ID="LinkButton2" runat="server"
27CausesValidation="False" CommandName="Delete"
28Text="删除" OnClientClick="ConfirmDel()">
29</asp:LinkButton>
30</ItemTemplate>
31<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
32</asp:TemplateField>
处理:
Code
protected void ImgGridView_RowDataBound(
object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex != -1 && e.Row.RowType ==
DataControlRowType.DataRow)
{
string A = ((Label)(e.Row.FindControl("Time_Lab"))).Text;
string B = ((Label)(e.Row.FindControl("Name_Lab"))).Text;
HyperLink HL = (HyperLink)(e.Row.FindControl("Img_Link"));
HL.NavigateUrl =
Config.getOtherPhotoFolder(Convert.ToDateTime(A)) + B;
}
}
protected void ImgGridView_RowDataBound(
object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex != -1 && e.Row.RowType ==
DataControlRowType.DataRow)
{
string A = ((Label)(e.Row.FindControl("Time_Lab"))).Text;
string B = ((Label)(e.Row.FindControl("Name_Lab"))).Text;
HyperLink HL = (HyperLink)(e.Row.FindControl("Img_Link"));
HL.NavigateUrl =
Config.getOtherPhotoFolder(Convert.ToDateTime(A)) + B;
}
}