javascript - Gridview 行为空或不是对象

标签 javascript html asp.net gridview

我有一个 Gridview,其中我想要的是,如果金额列是 0""。我想隐藏生成发票按钮。所以为此我尝试的是,

function GridExpInfo_ClientInsert() {
        for (i = 0; i < GridExpInfo.Rows.length; i++) {
            if (GridExpInfo.Rows[i].Cells[7].Value == "" || GridExpInfo.Rows[i].Cells[7].Value == "0") {
                GridExpInfo.Rows[i].Cells[14].style.display = "none";
            }
        }
    }

但是我收到错误

Microsoft JScript runtime error: 'GridExpInfo.Rows[...].Cells.14.style' is null or not an object

这是我的gridview aspx

<cc1:Grid ID="GridExpInfo" runat="server" FolderStyle="../Styles/Grid/style_12" AutoGenerateColumns="false"
          AllowAddingRecords="true" AllowColumnResizing="true" Width="100%" ShowFooter="true"
          ShowHeader="true" OnInsertCommand="GridExpInfo_InsertCommand" OnRebind="GridExpInfo_Rebind"
          OnRowDataBound="GridExpInfo_RowDataBound" OnUpdateCommand="GridExpInfo_UpdateCommand">
    <ClientSideEvents OnClientAdd="GridExpInfo_ClientAdd" OnClientEdit="GridExpInfo_OnClientEdit"
                      OnClientDblClick="GridExpInfo_OnClientDblClick" OnClientInsert="GridExpInfo_ClientInsert" />
    <TemplateSettings RowEditTemplateId="tplRowEdit3" />
    <Columns>
        <cc1:Column ID="Column19" DataField="MKEY" ReadOnly="true" Width="0%" runat="server"
                    Visible="false">
            <TemplateSettings TemplateId="gtchkConfirm" HeaderTemplateId="HTConfirm" />
        </cc1:Column>
        <cc1:Column Wrap="true" DataField="SR_NO" ReadOnly="true" HeaderText="Sr No" Width="4%"
                    ID="Column31">
            <TemplateSettings RowEditTemplateControlId="txtsrnoexp" RowEditTemplateControlPropertyName="value" />
        </cc1:Column>
        <cc1:Column Wrap="true" DataField="TYPE" HeaderText="Agreement Type" Width="8%" ID="Column51">
            <TemplateSettings RowEditTemplateControlId="ddlTypeExpData" RowEditTemplateControlPropertyName="value" />
        </cc1:Column>
        <cc1:Column Wrap="true" DataField="STAGE" HeaderText="Stage" Width="8%" ID="Column44">
            <TemplateSettings RowEditTemplateControlId="ddlStageType" RowEditTemplateControlPropertyName="value" />
        </cc1:Column>
        <cc1:Column Wrap="true" DataField="EXP_TYPE" HeaderText="Expense Type" Width="8%"
                    ID="Column25">
            <TemplateSettings RowEditTemplateControlId="ddlexptypExp" RowEditTemplateControlPropertyName="value" />
        </cc1:Column>
        <cc1:Column Wrap="true" DataField="EXP_ID" Width="0%" Visible="false" ID="Column43">
        </cc1:Column>
        <cc1:Column Wrap="true" DataField="PARTY_NAME" HeaderText="Party Name" Width="8%"
                    ID="Column29">
            <TemplateSettings RowEditTemplateControlId="txtpartynameexp" RowEditTemplateControlPropertyName="value" />
        </cc1:Column>
        <cc1:Column Wrap="true" DataField="AMOUNT" HeaderText="Amount" Width="6%" ID="Column21">
            <TemplateSettings RowEditTemplateControlId="txtamountexp" RowEditTemplateControlPropertyName="value" />
        </cc1:Column>
        <cc1:Column Wrap="true" DataField="REMARKS" HeaderText="Remarks" Width="10%" ID="Column24">
            <TemplateSettings RowEditTemplateControlId="txtremexp" RowEditTemplateControlPropertyName="value" />
        </cc1:Column>
        <cc1:Column Wrap="true" DataField="CHQ_FAVR_NAME" HeaderText="Chq in Favor Name"
                    Width="8%" ID="Column28">
            <TemplateSettings RowEditTemplateControlId="txtchqfavExp" RowEditTemplateControlPropertyName="value" />
        </cc1:Column>
        <cc1:Column Wrap="true" DataField="VOUCHER_NO" HeaderText="Voucher No" Width="6%"
                    ID="Column26">
            <TemplateSettings RowEditTemplateControlId="txtvouchexp" RowEditTemplateControlPropertyName="value" />
        </cc1:Column>
        <cc1:Column Wrap="true" DataField="CHEQUE_NO" HeaderText="Chq No" Width="5%" ID="Column27">
            <TemplateSettings RowEditTemplateControlId="txtChqnoExp" RowEditTemplateControlPropertyName="value" />
        </cc1:Column>
        <cc1:Column Wrap="true" DataField="CHEQUE_DT" DataFormatString="{0:dd-MM-yyyy}" HeaderText="Chq Date"
                    Width="6%" ID="Column30">
            <TemplateSettings RowEditTemplateControlId="txtchqDtExp" RowEditTemplateControlPropertyName="value" />
        </cc1:Column>
        <cc1:Column ID="Column32" DataField="ATTACHMENT" HeaderText="Attachment" runat="server"
                    Width="8%" Wrap="true">
        </cc1:Column>
        <cc1:Column ID="Column20" HeaderText="Generate Invoice" runat="server" Width="8%"
                    Wrap="true">
            <TemplateSettings TemplateId="tplButtonInvoice" />
        </cc1:Column>
    </Columns>
    <Templates>
        <cc1:GridTemplate runat="server" ID="tplRowEdit3">
            <Template>
                <table class="rowEditTable">
                    <tr>
                        <td valign="top">
                            <fieldset style="width: 750px; height: 310px;">
                                <legend>Expense Information</legend>
                                <table cellpadding="2" cellspacing="2" border="0">
                                    <tr>
                                        <td style="display: none;">
                                            SR NO :
                                        </td>
                                        <td>
                                            <input type="text" runat="server" id="txtsrnoexp" style="display: none;" class="ob_gEC" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Type :
                                        </td>
                                        <td>
                                            <asp:DropDownList ID="ddlTypeExpData" runat="server" Width="250px" DataSourceID="ds_exp_type"
                                                              DataTextField="TYPE_DESC" DataValueField="TYPE_DESC">
                                                <asp:ListItem Value="0">--Select--</asp:ListItem>
                                                <%--<asp:ListItem Value="Agreement Type">Agreement Type</asp:ListItem>
                                                <asp:ListItem Value="Expense Type">Expense Type</asp:ListItem>--%>
                                            </asp:DropDownList>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Stage :
                                        </td>
                                        <td>
                                            <asp:DropDownList ID="ddlStageType" runat="server" name="ddlStageTypename" DataSourceID="sd_Type_Stage"
                                                              DataTextField="STAGE" DataValueField="STAGE" Width="250px">
                                            </asp:DropDownList>
                                        </td>
                                    </tr>
                                    <tr style="display: none;">
                                        <td>
                                            Ref No :
                                        </td>
                                        <td>
                                            <%--<input type="text" id="txtExpRefNo" name="txtrefNovalue" runat="server" style="width: 150px;
                                                       height: 18px; font-size: 11px;" class="ob_gEC" />--%>
                                            <asp:TextBox ID="txtExpRefNo" Height="18px" name="txtrefNovalue" runat="server" CssClass="ob_gEC"
                                                         Width="150px"></asp:TextBox>
                                        </td>
                                        <td>
                                            Ref Date :
                                        </td>
                                        <td>
                                            <%-- <input type="text" id="txtExpRefDt" name="txtrefdatevalue" runat="server" style="width: 150px;
                                                        height: 18px; font-size: 11px;" class="ob_gEC" />--%>
                                            <asp:TextBox ID="txtExpRefDt" Height="18px" name="txtExpRefDt" runat="server" CssClass="ob_gEC"
                                                         Width="150px"></asp:TextBox>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Expense Type:
                                        </td>
                                        <td>
                                            <asp:DropDownList ID="ddlexptypExp" runat="server" DataSourceID="sd_Type_EXP" DataTextField="DESCRIPTION"
                                                              DataValueField="DESCRIPTION" Width="250px">
                                                <asp:ListItem Value="--Select--">--Select--</asp:ListItem>
                                            </asp:DropDownList>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Amount:
                                        </td>
                                        <td>
                                            <input type="text" id="txtamountexp" style="width: 150px; height: 18px; font-size: 11px;"
                                                   class="ob_gEC" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Party Name:
                                        </td>
                                        <td>
                                            <input type="text" id="txtpartynameexp" style="width: 250px; height: 18px; font-size: 11px;"
                                                   class="ob_gEC" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Expense Details:
                                        </td>
                                        <td>
                                            <asp:TextBox ID="txtremexp" CssClass="ob_gEC" runat="server" Width="250px" Height="50"
                                                         TextMode="MultiLine" Style="font-size: 11px;"></asp:TextBox>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Chq in favor name:
                                        </td>
                                        <td>
                                            <input type="text" id="txtchqfavExp" style="width: 250px; height: 18px; font-size: 11px;"
                                                   class="ob_gEC" />
                                        </td>
                                        <td>
                                            Voucher No:
                                        </td>
                                        <td>
                                            <input type="text" id="txtvouchexp" style="width: 150px; height: 18px; font-size: 11px;"
                                                   class="ob_gEC" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Chq No:
                                        </td>
                                        <td>
                                            <input type="text" id="txtChqnoExp" style="width: 150px; height: 18px; font-size: 11px;"
                                                   class="ob_gEC" />
                                        </td>
                                        <td>
                                            Chq date:
                                        </td>
                                        <td>
                                            <input type="text" id="txtchqDtExp" style="width: 150px; height: 18px; font-size: 11px;"
                                                   class="ob_gEC" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Attachment:
                                        </td>
                                        <td>
                                            <input type="file" id="FlAttacExp" style="width: 300px; height: 18px; font-size: 11px;"
                                                   class="ob_gEC" />
                                        </td>
                                    </tr>
                                </table>
                            </fieldset>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center">
                            <input type="button" value="Save" onclick="GridExpInfo.save()" class="button" />
                            <input type="button" value="Cancel" onclick="GridExpInfo.cancel()" class="button" />
                        </td>
                    </tr>
                </table>
            </Template>
        </cc1:GridTemplate>
    </Templates>
    <%-- <Templates>
        <cc1:GridTemplate runat="server" ID="tplNumbering3">
            <Template>
                <b>
                    <%# (Container.RecordIndex + 1) %></b>
            </Template>
        </cc1:GridTemplate>
    </Templates>--%>
    <Templates>
        <cc1:GridTemplate runat="server" ID="tplButtonInvoice">
            <Template>
                <input id="Button1" type="button" runat="server" value="Invoice" style="width: 80px;" />
            </Template>
        </cc1:GridTemplate>
    </Templates>
</cc1:Grid>

最佳答案

JavaScript 和 C# 代码不能混合。 GridView1.Rows[i].Cells[14].style.display = "none"; 行不会给出类似 TableCell 不包含“style”定义的错误吗? .. 在 Visual Studio 中?

你应该像这样使用它:

GridExpInfo.Rows[i].Cells[14].Visible = false;

但是,这将从网格中删除单元格,并且该行中的列将移至左侧。因此,对于用户来说,单元格 14 包含单元格 15、15 和 16 等的内容。您可以通过对单元格 13 应用 colspan 来解决此问题,但这也意味着单元格 13 的内容可能会溢出。

更好的是根据行中的特定值隐藏按钮。

<asp:Button Visible='<%# string.IsNullOrEmpty(Eval("value").ToString()) || Eval("value").ToString() != "0" %>' runat="server" Text="Generate invoice" ID="Button1" />

更新

看起来OP正在使用Obout中的网格。这可能意味着“正常”网格功能的工作方式有所不同。

<input type="button" value="Cancel" onclick="GridExpInfo.cancel()" class="button" style="display:<%# (Container.Value != "0" ? "block" : "none") %>" />

关于javascript - Gridview 行为空或不是对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40281295/

相关文章:

c# - 将参数传递给 Postman 中的 asmx 网络服务

javascript - Ajax 发布 Json 数组未由 Controller 检索

javascript - 将数组转换为对象+设置属性名称

javascript - 实时复制另一个文本框值Jquery

javascript - 在选择的 jQuery UI 选项卡上显示/隐藏某些 div

Javascript 函数innerHTML 样式不正确

html - 在 iphone 容器内制作屏幕截图滚动

c# - 几次成功请求后,Web API 突然返回 403 forbidden

Javascript - 无法获得要警报的数组的平均值

javascript - 我无法使用 CefSharp 从 WinForms 的 c# 获取一些数组