简体   繁体   English

如何调用GridView中的文本框

[英]How to call a textbox that is in GridView

I have a GridView and in one of those columns is a textbox. 我有一个GridView,在其中一列中是一个文本框。 When this textbox is selected it pops up a list of pieces. 选择此文本框后,它将弹出一个片段列表。 When a piece is selected I need to show the piece in the textbox. 选择一块后,我需要在文本框中显示该块。

<asp:UpdatePanel ID="udpPieceDetails" UpdateMode="Conditional" runat="server">
                <ContentTemplate>
                    <asp:GridView  style="width:75%;float:left"  
                        ID="gvPieceOutturns" 
                        ShowHeaderWhenEmpty="false"
                        CssClass="tblResults" 
                        runat="server" 
                        OnRowDataBound="gvPieceOutturns_ItemDataBound"                             
                        DataKeyField="ID" 
                        AutoGenerateColumns="false"
                        allowpaging="false"
                        AlternatingRowStyle-BackColor="#EEEEEE">
                        <HeaderStyle CssClass="tblResultsHeader" />
                        <Columns>  
                           <asp:TemplateField HeaderText="Outturn Pce" SortExpression="OutturnPce">
                                <ItemTemplate>
                                    <a style="float:none;width:16px;height:16px;margin-right:0px;left:0px;top:26px" title="Pick Type from list..." class="iconSearch" id="btnMemShowPieceType"></a>  
                                    <input type="text" id="txtMemPieceType" class="lookuppopup" onblur="CheckMemPiece(this.value)"   style="text-transform:uppercase;width:40px" runat="server"/>
                                </ItemTemplate>
                            </asp:TemplateField>     
                        </Columns>
                    </asp:GridView>
                </ContentTemplate>
         </asp:UpdatePanel>  

Here I am trying to populate the textbox: 在这里,我试图填充文本框:

function PopulateMemPiece(result) {
        if (result.ID > 0) {
            $("#<%= hfPieceType.ClientID %>").val(result.ID);
            $("#<%= txtMemPieceType.ClientID %>").val(result.Code);

        } else {
            $("#<%= hfPieceType.ClientID %>").val(0);
            $("#<%= txtMemPieceType.ClientID %>").val("");
        }
    }

But I get this error: 但是我得到这个错误:

Error 2586 The name 'txtMemPieceType' does not exist in the current context 错误2586名称'txtMemPieceType'在当前上下文中不存在

Wouldn't 不会

gvPieceOutturns.FindControl("hfPieceType");

work ? 工作?

You can also try 您也可以尝试

ClientIDMode="Static"

on your TextBoxes, to use jQuery on them. 在您的TextBoxes上,以在其上使用jQuery。

Since your trying to access that control in JavaScript but it is nested inside an Gridview I would suggest that you create a Property in the code-behind that resolves that control. 由于您尝试使用JavaScript访问该控件,但该控件嵌套在Gridview中,因此建议您在隐藏该控件的代码后创建一个Property。 I would add the following property to the .cs file: 我将以下属性添加到.cs文件:

Edit: The control is also nested inside a TemplateField, so we will need to iterate through the rows of the Gridview to find the desired row which needs to be updated. 编辑:控件也嵌套在TemplateField内,因此我们将需要遍历Gridview的行以找到需要更新的所需行。

protected HtmlInputText txtMemPieceType
{
    get { return findMemPieceType(); }
}

private HtmlInputText findMemPieceType()
{
    foreach (GridViewRow row in gvPieceOutturns.Rows)
    {
        if (/* Determine which row has the info you need */)
        {
            return row.FindControl("txtMemPieceType") as HtmlInputText;
        }
    }       
}

You will need to come up with a way to distinguish which row needs to be updated. 您将需要想出一种方法来区分需要更新的行。 Alternativly if the row in question can trigger a Gridview Event like RowCommand when the item is selected, then you can do something similar to this: 或者,如果选中该项目时,所讨论的行可以触发诸如RowCommand之类的Gridview事件,那么您可以执行以下操作:

protected HtmlInputText txtMemPieceType { get; set; }

void gvPieceOutturns_RowCommand(Object sender, GridViewCommandEventArgs e)
{
    int index = Convert.ToInt32(e.CommandArgument);
    GridViewRow row = ContactsGridView.Rows[index];
    txtMemPieceType = row.FindControl("txtMemPieceType") as HtmlInputText;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM