【问题标题】:How to call a textbox that is in GridView如何调用 GridView 中的文本框
【发布时间】:2015-12-11 15:25:54
【问题描述】:

我有一个 GridView,其中一列是一个文本框。当这个文本框被选中时,它会弹出一个片段列表。选择一块时,我需要在文本框中显示该块。

<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>  

我在这里尝试填充文本框:

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("");
        }
    }

但我收到此错误:

错误 2586 名称 'txtMemPieceType' 在当前不存在 上下文

【问题讨论】:

    标签: javascript c# jquery asp.net gridview


    【解决方案1】:

    不会

    gvPieceOutturns.FindControl("hfPieceType");
    

    工作?

    你也可以试试

    ClientIDMode="Static"
    

    在您的 TextBoxes 上使用 jQuery。

    【讨论】:

    • 如果我使用`FindControl`,当一个片段被选中时如何填充文本框?
    • 设置它,然后更新 UpdatePanel,将 tb 设置为 AsyncPostBackTrigger。此外,您可能还需要将 AutoPostBack 设置为 TextBox。
    【解决方案2】:

    由于您尝试在 JavaScript 中访问该控件但它嵌套在 Gridview 中,我建议您在解析该控件的代码隐藏中创建一个属性。我会将以下属性添加到 .cs 文件中:

    编辑:该控件也嵌套在 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;
            }
        }       
    }
    

    您需要想出一种方法来区分需要更新的行。或者,如果有问题的行可以在选择项目时触发像 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;
    }
    

    【讨论】:

    • 我收到错误 Object reference not set to an instance of an object with $("#&lt;%= txtMemPieceType.ClientID %&gt;").val(result.Code);
    • 啊,我没注意到你在哪里使用HtmlInputText 而不是Texbox。我编辑了我的答案,看看是否有效。
    • 我再次编辑了我的答案,因为控件位于 TemplateField 内,看起来它会更复杂。
    猜你喜欢
    • 2012-06-09
    • 1970-01-01
    • 2014-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多