[英]Get the element from itemTemplate in gridview asp.net
I want to ask a question, I'm actually looking for a way to modify the status of a textbox in my gridview, I'm quite lost since I just started this language a few weeks ago and i can't find a satisfying answer (maybe I apply them the wrong way maybe you will be able to help me there. Here is my code :我想问一个问题,我实际上正在寻找一种方法来修改我的 gridview 中文本框的状态,我很迷茫,因为我几周前才开始使用这种语言,我找不到满意的答案(也许我以错误的方式应用它们,也许您可以在那里帮助我。这是我的代码:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div class="centrer">
<asp:Image ID="IMG_attente" runat="server" ImageUrl="~/Images/1px.gif" Height="32px" />
</div>
<asp:Panel ID="panListes" runat="server" Visible="false" CssClass="formulaire">
<asp:Label runat="server" ID="LabelErreur" Visible="false"></asp:Label>
<asp:GridView ID="ListePieceFlash" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSourceListePieceFlash"
OnRowEditing="ListePieceFlash_RowEditing" OnRowCancelingEdit="ListePieceFlash_RowCancelingEdit"
EnableModelValidation="True" SkinID="Source_DarkBlue" AllowPaging="True" DataKeyNames="NumOF"
OnRowCommand="ListePieceFlash_RowCommand" Width="100%" AllowSorting="True">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/Images/annuler.gif" CommandName="cancel"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" ToolTip="Annuler" />
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Images/okvert.gif" CommandName="save"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" ToolTip="Sauvegarder" />
</EditItemTemplate>
<ItemTemplate>
<asp:ImageButton ID="btnEdit" runat="server" ImageUrl="~/Images/edit.png" CommandName="edit"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" ToolTip="Sélectionner" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="NumSerieLabel" runat="server" Text="NumSerie"></asp:Label></HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="NumSerie" runat="server" OnTextChanged="OnNumSerieChanged" /></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="SymboleLabel" runat="server" Text="Symbole"></asp:Label></HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="Symbole" runat="server" Enabled="False" OnTextChanged="OnSymboleChanged" /></ItemTemplate>
</asp:TemplateField>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSourceListePieceFlash" runat="server" ConnectionString="<%$ ConnectionStrings:PRMConnectionString %>"
SelectCommand="exec [dbo].[getListePieceFlash]"></asp:SqlDataSource>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
As you can see my "Symbole" TextBox is disabled at start but I want it to be enabled when i fill the first one and there is more than one element, here is the code behind (the SQL part is a stored procedure):正如您所看到的,我的“符号”文本框在开始时被禁用,但我希望在我填写第一个并且有多个元素时启用它,这是后面的代码(SQL 部分是一个存储过程):
protected void OnNumSerieChanged(object sender, EventArgs e)
{
numSerieValue = (sender as TextBox).Text;
var con = Sql.Instance.ObtenirConnexionSql();
SqlCommand cmd = ObtenirCommande(con);
cmd.CommandText = "dbo.getListPiece";
Int32 count = (Int32)cmd.ExecuteScalar();
if (count > 1)
(ListePieceFlash.FindControl("Symbole") as TextBox).Enabled = true;
}
The problem is on the last line and I don't really know how to get over it actually, can someone help me?问题在最后一行,我真的不知道如何克服它,有人可以帮助我吗? Thank you very much :)非常感谢 :)
ListePieceFlash.FindControl("Symbole")
returns null
because you cannot find the TextBox
via GridView.FindControl
. ListePieceFlash.FindControl("Symbole")
返回null
因为您无法通过GridView.FindControl
找到TextBox
。 The NamingContainer
of the TextBox
is the GridViewRow
. TextBox
的NamingContainer
是GridViewRow
。
You get it by casting the Sender
in OnNumSerieChanged
to TextBox
and it's NamingContainer
property to GridViewRow
.您可以通过将OnNumSerieChanged
的Sender
OnNumSerieChanged
为TextBox
并将其NamingContainer
属性转换为GridViewRow
来获得它。 Then use row.FindControl("Symbole")
to find the target TextBox
:然后使用row.FindControl("Symbole")
查找目标TextBox
:
protected void OnNumSerieChanged(object sender, EventArgs e)
{
TextBox NumSerie = (TextBox) sender;
GridViewRow row = (GridViewRow) NumSerie.NamingContainer;
TextBox Symbole = (TextBox) row.FindControl("Symbole");
// ...
}
Note that i strongly advise against such helper classes in ASP.NET which hold and return database objects like your Sql.Instance.ObtenirConnexionSql()
.请注意,我强烈建议不要在 ASP.NET 中使用此类帮助程序类,它们保存和返回数据库对象,例如Sql.Instance.ObtenirConnexionSql()
。 It can be a source of nasty errors . 它可能是令人讨厌的错误的来源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.