gpt4 book ai didi

c# - GridView SelectedIndexChanging事件,在TemplateField中查找文本

转载 作者:太空宇宙 更新时间:2023-11-03 10:57:20 25 4
gpt4 key购买 nike

我有网格,它是 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;
}

问题是,text coming null 。有数据,但它不是值而是空的....同样的代码也在另一个页面上工作。所以,请告诉我。我做错了什么?

谢谢

最佳答案

如果您使用 TemplateFields 而不是 BoundFields,则 Cell.Text 始终为空。你要么必须

  • TemplateField 中使用一个控件并使用 row.FindControl(id) 找到它(我推荐的)
  • 或者您必须将第一个控件转换为 DataBoundLiteralControl这有点奇怪,因为您看不到该控件。它会自动添加到单元格的控件集合中。

    GridViewRow row = gvClients.Rows[e.NewSelectedIndex];
    txtClientNumber.Text = ((ITextControl)row.Cells[3].Controls[0]).Text;

关于c# - GridView SelectedIndexChanging事件,在TemplateField中查找文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19002884/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com