我有网格,它是selectIndexchanging事件。
我的aspx代码:
<asp:GridView ID="gvClients" runat="server" AutoGenerateColumns="False" BackColor="LightGoldenrodYellow"
BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None"
OnRowEditing="gvClients_RowEditing" Font-Size="Medium"
onrowcancelingedit="gvClients_RowCancelingEdit"
onrowdeleting="gvClients_RowDeleting" onrowupdating="gvClients_RowUpdating"
onselectedindexchanging="gvClients_SelectedIndexChanging">
<FooterStyle BackColor="Tan" />
<Columns>
<asp:CommandField ShowEditButton="True" ButtonType="Image" CancelImageUrl="~/Images/Cancel.png"
UpdateImageUrl="~/Images/save.png" EditImageUrl="~/images/Edit.png" ItemStyle-Width="25px"
UpdateText="Update" CancelText="Cancel" />
<asp:CommandField ShowDeleteButton="true" DeleteImageUrl="~/Images/delete.png" ButtonType="Image"
ItemStyle-Width="25px" />
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton runat="server" ID="imgSelect" CommandName="Select" AlternateText="Select"
ImageUrl="~/Images/allowed.png" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ClientId" ItemStyle-Width="150px" ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<%# Eval("ClientId")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtClientClientId" Text='<%#Eval("ClientId") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ClientName" ItemStyle-Width="150px" ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<%# Eval("ClientName")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtClientClientName" Text='<%#Eval("ClientName") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status" ItemStyle-Width="150px" ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<%# Eval("Active")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtClientActive" Text='<%#Eval("Active") %>' />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<HeaderStyle BackColor="Tan" Font-Bold="True" HorizontalAlign="Left" Font-Size="Smaller" />
<AlternatingRowStyle BackColor="PaleGoldenrod" />
</asp:GridView>
现在,我的SelectedIndexChanging
代码:
protected void gvClients_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
GridViewRow row = gvClients.Rows[e.NewSelectedIndex];
txtClientNumber.Text = row.Cells[3].Text;
}
问题是,文字无效。有数据,但它没有值,它会变为空....也相同的代码正在另一页上工作。所以,请让我知道。我做错了什么?
感谢
答案 0 :(得分:2)
如果您使用TemplateFields
代替BoundFields
,则Cell.Text
始终为空。你要么
TemplateField
中的控件并使用row.FindControl(id)
找到它(我推荐的内容)或者您必须将第一个控件转换为DataBoundLiteralControl
,这有点奇怪,因为您无法看到该控件。它会自动添加到单元格的控件集合中。
GridViewRow row = gvClients.Rows[e.NewSelectedIndex];
txtClientNumber.Text = ((ITextControl)row.Cells[3].Controls[0]).Text;