从gridview rowcommand在更新面板中设置文本框文本

时间:2014-06-13 14:07:19

标签: c# asp.net gridview textbox updatepanel

我正在尝试从gridview的rowcommand更新更新面板中的文本框的值,也在更新面板中。 rowcommand正在触发更新面板并运行OnRowCommand代码,但是文本框的值根本没有更新。

我没有收到任何错误,我已经检查过我不是通过跟踪我提供值的类的值来输入空字符串。

这是我的代码:

ASPX

<asp:Panel ID="pnlInspectorExpenses" CssClass="expenses" runat="server" Visible="false">
    <asp:HiddenField ID="hdnInsExpID" runat="server" />
    <asp:UpdatePanel ID="uPnlInspectorExpenses" runat="server">
        <ContentTemplate>
            <table>
                <tr>
                    <td class="tableHead tableItem">IMO Number:</td>
                    <td class="tableField"><asp:Label ID="lblExpensesIMO" runat="server" /></td>
                    <td></td>
                </tr>
                <tr>
                    <td class="tableHead tableItem">Port of Vessel:</td>
                    <td class="tableField"><asp:Label ID="lblExensesPort" runat="server" /></td>
                    <td></td>
                </tr>
                <%--<tr>
                    <td class="tableHead tableItem">Report No.:</td>
                    <td class="tableField"><asp:Label ID="lblExpensesReportNo" runat="server" /></td>
                    <td></td>
                </tr>--%>
                <tr>
                    <td class="tableHead tableItem">Client Reference:</td>
                    <td class="tableField"><asp:Label ID="lblExpensesClientRef" runat="server" /></td>
                    <td></td>
                </tr>
                <tr>
                    <td class="tableHead tableItem">Vessel Type:</td>
                    <td class="tableField"><asp:Label ID="lblExpensesVesselType" runat="server" /></td>
                    <td></td>
                </tr>
            </table>
            <table class="tableClass">
                <tr>
                    <th align="left">Date</th>
                    <th align="left">Expense Description</th>
                    <th align="left">Type</th>
                    <th align="left">Currency</th>
                    <th align="right">Foreign Cost</th>
                    <th align="right">Exch. Rate</th>
                    <th align="right">GBP</th>
                    <th align="left">Receipt No</th>
                    <th colspan="2"></th>
                </tr>
                <tr>
                    <td colspan="10" class="zeroPadding">
                        <asp:GridView ID="grdInspectorExpenses" runat="server" CssClass="tableClass zeroMargin zeroBorder" DataKeyNames="ExpID" AutoGenerateColumns="false" 
                            OnRowCommand="grdInspectorExpenses_RowCommand">
                            <Columns>
                                <asp:BoundField DataField="ExpDate" HeaderStyle-CssClass="invisible" ItemStyle-HorizontalAlign="Left" DataFormatString="{0:d}" ItemStyle-Width="90px"  />
                                <asp:BoundField DataField="ExpDescription" HeaderStyle-CssClass="invisible" ItemStyle-HorizontalAlign="Left" ItemStyle-Width="220px" />
                                <asp:BoundField DataField="ExpType" HeaderStyle-CssClass="invisible" ItemStyle-HorizontalAlign="Left" ItemStyle-Width="100px" />
                                <asp:BoundField DataField="ExpCurrency" HeaderStyle-CssClass="invisible" ItemStyle-HorizontalAlign="Left" ItemStyle-Width="75px" />
                                <asp:BoundField DataField="ExpForeignCost" HeaderStyle-CssClass="invisible" ItemStyle-HorizontalAlign="Right" ItemStyle-Width="75px" />
                                <asp:BoundField DataField="ExpExchangeRate" HeaderStyle-CssClass="invisible" ItemStyle-HorizontalAlign="Right" ItemStyle-Width="80px" />
                                <asp:BoundField DataField="ExpLocalCost" HeaderStyle-CssClass="invisible" ItemStyle-HorizontalAlign="Right" ItemStyle-Width="80px" />
                                <asp:BoundField DataField="ExpReceiptNo" HeaderStyle-CssClass="invisible" ItemStyle-HorizontalAlign="Left" ItemStyle-Width="80px" />
                                <asp:TemplateField HeaderStyle-CssClass="invisible" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="30px">
                                    <ItemTemplate>
                                        <asp:LinkButton ID="btnEditExpense" runat="server" Text="Edit" CommandName="Edit" CommandArgument="<%# Container.DataItemIndex %>"  />
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderStyle-CssClass="invisible" ItemStyle-HorizontalAlign="Center">
                                    <ItemTemplate>
                                        <asp:LinkButton ID="btnDeleteExpense" runat="server" Text="Delete" OnClientClick="javascript: if (!confirm('Are you sure you want delete?')) return false;" 
                                            CommandName="Delete" CommandArgument="<%# Container.DataItemIndex %>" />
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                            <EmptyDataTemplate>
                                No expenses have yet been added
                            </EmptyDataTemplate>
                        </asp:GridView>
                    </td>
                </tr>
                <tr>
                    <td><asp:TextBox ID="txtInsExpDate" CssClass="datepicker" runat="server" Width="88px" style="padding: 5px 0px;" /></td>
                    <td><asp:TextBox ID="txtInsExpDesc" runat="server" Width="218px" style="padding: 5px 0;" /></td>
                    <td>
                        <asp:DropDownList ID="ddlExpenseType" runat="server" style="width:100px; padding: 4px 0;">
                            <asp:ListItem Text="General" Value="General" />
                            <asp:ListItem Text="Accommodation" Value="Accommodation" />
                            <asp:ListItem Text="Flight" Value="Flight" />
                        </asp:DropDownList>
                    </td>
                    <td><asp:TextBox ID="txtInsExpCurrency" runat="server" Width="68px" style="padding: 5px 0;" /></td>
                    <td><asp:TextBox ID="txtInsExpForVal" runat="server" Width="78px" style="text-align: right; padding: 5px 0;" /></td>
                    <td><asp:TextBox ID="txtInsExpExcRate" runat="server" Width="78px" style="text-align: right; padding: 5px 0;" /></td>
                    <td><asp:TextBox ID="txtInsExpLocVal" runat="server" Width="78px" style="text-align: right; padding: 5px 0;" /></td>
                    <td><asp:TextBox ID="txtInsExpRecNo" runat="server" Width="78px" style="padding: 5px 0;" /></td>
                    <td colspan="2" style="width:100px"></td>
                </tr>
            </table>
        </ContentTemplate>
    </asp:UpdatePanel>
    <asp:UpdateProgress ID="UpdateProgress5" runat="server" DynamicLayout="true">
        <ProgressTemplate>
            <div class="overlay" >
                <img src="/Portals/_default/Skins/PMIT/Images/radarLoading.gif" />
            </div>
        </ProgressTemplate>
    </asp:UpdateProgress>
</asp:Panel>

ASPX.CS

protected void grdInspectorExpenses_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        Trace.Warn("0");
        if (e.CommandName == "Edit")
        {
            Trace.Warn("1");
            int rowIndex = int.Parse(e.CommandArgument.ToString());
            int ExpID = int.Parse(grdInspectorExpenses.DataKeys[rowIndex].Values[0].ToString());
            hdnInsExpID.Value = ExpID.ToString();
            List<ExpenseInfo> Expenses = IC.ExpenseSelect(ExpID);
            ExpenseInfo Expense = Expenses[0];
            txtInsExpDate.Text = Expense.ExpDate.ToString();
            txtInsExpDesc.Text = Expense.ExpDescription;
            ddlExpenseType.Items.FindByValue(Expense.ExpType).Selected = true;
            txtInsExpCurrency.Text = Expense.ExpCurrency;
            txtInsExpForVal.Text = Expense.ExpForeignCost.ToString();
            txtInsExpExcRate.Text = Expense.ExpExchangeRate.ToString();
            txtInsExpLocVal.Text = Expense.ExpLocalCost.ToString();
            txtInsExpRecNo.Text = Expense.ExpReceiptNo;
            Trace.Warn(Expense.ExpReceiptNo);
        }
        else if (e.CommandName == "Delete")
        {

        }
    }

2 个答案:

答案 0 :(得分:1)

我最终想到了这一点。似乎如果将CommandName命名为“Edit”,它会调用'OnRowEditing'函数,该函数未定义。因此,我只是将CommandName重命名为“EditExpenses”。我也为删除命令做了同样的事。

答案 1 :(得分:0)

请将UpdatePanel的UpdateMode设置为Always以查看这是否有所不同。有关UpdatePanel内容更新时的规则和条件,请参阅备注:http://msdn.microsoft.com/en-us/library/system.web.ui.updatepanel.updatemode(v=vs.110).aspx

下的此处