ASP.NET Webform从后面的代码填充bootstrap模式

时间:2016-11-28 06:50:33

标签: c# asp.net twitter-bootstrap

我的页面中有一个gridview,当单击Edit按钮时,它将显示一个名为 modalEditPurchase 的引导模式,并显示数据。我试图从代码隐藏填充模态中的字段但它不起作用,字段没有填充。

有谁能告诉我做错了什么?

提前谢谢。

HTML

<div class="form-group">
             <div class="col-md-2">
                 <a id="hypAddPurchase" data-toggle="modal" data-target="#modalAddPurchase" class="btn btn-primary" <%--style="float: left;"--%>>
                    <span class="glyphicon glyphicon-plus"></span> Add Purchase
                 </a>
             </div>
         </div>

    <div class="form-group">
             <div class="col-md-11">
                 <div class="container">
                     <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                         <ContentTemplate>
                             <asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="false" CssClass="table table-bordered table-hover"
                                 DataKeyNames="ID">
                                <Columns>
                                    <asp:BoundField DataField="ID" HeaderText="Id" />
                                    <asp:BoundField DataField="PRODUCT_CODE" HeaderText="Fund Code" />
                                    <asp:BoundField DataField="PRODUCT_NAME" HeaderText="Fund Name" />
                                    <asp:BoundField DataField="" HeaderText="Fund Currency" />
                                    <asp:BoundField DataField="INVEST_AMT" HeaderText="Amount Invest" />
                                    <asp:BoundField DataField="" HeaderText="Payment Currency" />
                                    <asp:BoundField DataField="ENTRY_FEE_RATE" HeaderText="Entry Fee %" />
                                    <asp:BoundField DataField="ENTRY_FEE_AMT" HeaderText="Entry Fee Amount" />
                                    <asp:BoundField DataField="GST_RATE" HeaderText="GST %" />
                                    <asp:BoundField DataField="GST_AMT" HeaderText="GST Amount" />
                                    <asp:BoundField DataField="TRANS_AMT" HeaderText="Transaction Amount" />
                                    <asp:BoundField DataField="FX_RATE" HeaderText="FX Rate" Visible="False" />
                                    <asp:TemplateField>
                                        <ItemTemplate>
                                            <asp:LinkButton runat="server" CommandName="Edit" data-toggle="modal" data-target="#modalEditPurchase" CssClass="btn btn-primary btn-sm" OnClick="OpenEdit_OnClick"><span class="glyphicon glyphicon-pencil"></span> Edit</asp:LinkButton>
                                            <asp:LinkButton runat="server" CommandName="Delete" CssClass="btn btn-primary btn-sm"><span class="glyphicon glyphicon-minus"></span> Delete</asp:LinkButton>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                             </asp:GridView>
                         </ContentTemplate>
                         <Triggers>
                            <asp:AsyncPostBackTrigger ControlID="btnAdd" EventName="Click" />
                         </Triggers>
                     </asp:UpdatePanel>
                 </div>
             </div>
         </div>

<div class="modal fade" id="modalEditPurchase" tabindex="-1" role="dialog">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="modalEditPurchaseLabel">Edit Purchase</h4>
                </div>
                <div class="modal-body">
                    <ul class="row">
                        <div class="col-md-4" align="right">
                            <label class="control-label">Fund:</label>
                        </div>
                        <div class="col-md-5">
                            <asp:DropDownList class="form-control chosen-select_inModal" ID="ddlFundEdit" Width="400" runat="server"></asp:DropDownList>
                        </div>
                    </ul>
                    <ul class="row">
                        <div class="col-md-4" align="right">
                            <label class="control-label">Fund Currency:</label>
                        </div>
                        <div class="col-md-5">
                            <asp:TextBox runat="server" ID="txtFundCurrencyEdit" CssClass="form-control"></asp:TextBox>
                        </div>
                    </ul>
                    <ul class="row">
                        <div class="col-md-4" align="right">
                            <label class="control-label">Amount Invest:</label>
                        </div>
                        <div class="col-md-5">
                            <asp:TextBox runat="server" id="txtAmountInvestEdit" CssClass="form-control" placeholder="00.00"></asp:TextBox>
                        </div>
                    </ul>
                    <ul class="row">
                        <div class="col-md-4" align="right">
                            <label class="control-label">Payment Currency:</label>
                        </div>
                        <div class="col-md-5">
                            <asp:TextBox runat="server" id="txtPaymentCurrencyEdit" CssClass="form-control" placeholder="Payment Currency"></asp:TextBox>
                        </div>
                    </ul>
                    <ul class="row">
                        <div class="col-md-4" align="right">
                            <label class="control-label">FX Rate:</label>
                        </div>
                        <div class="col-md-5">
                            <asp:TextBox runat="server" id="txtFxRateEdit" CssClass="form-control" placeholder ="FX Rate"></asp:TextBox>
                        </div>
                    </ul>
                    <ul class="row">
                        <div class="col-md-4" align="right">
                            <label class="control-label">Entry Fee %:</label>
                        </div>
                        <div class="col-md-5">
                            <asp:TextBox runat="server" id="txtEntryFeeRateEdit" CssClass="form-control" placeholder="Entry Fee Rate %"></asp:TextBox>
                        </div>
                    </ul>
                    <ul class="row">
                        <div class="col-md-4" align="right">
                            <label class="control-label">Entry Fee:</label>
                        </div>
                        <div class="col-md-5">
                            <asp:TextBox runat="server" id="txtEntryFeeAmtEdit" CssClass="form-control" placeholder="00.00"></asp:TextBox>
                        </div>
                    </ul>
                    <ul class="row">
                        <div class="col-md-4" align="right">
                            <label class="control-label">GST %:</label>
                        </div>
                        <div class="col-md-5">
                            <asp:TextBox runat="server" id="txtGstRateEdit" CssClass="form-control" placeholder="GST Rate %"></asp:TextBox>
                        </div>
                    </ul>
                    <ul class="row">
                        <div class="col-md-4" align="right">
                            <label class="control-label">GST Amount:</label>
                        </div>
                        <div class="col-md-5">
                            <asp:TextBox runat="server" id="txtGstAmtEdit" CssClass="form-control" placeholder="00.00"></asp:TextBox>
                        </div>
                    </ul>
                    <ul class="row">
                        <div class="col-md-4" align="right">
                            <label class="control-label">Trans Amount:</label>
                        </div>
                        <div class="col-md-5">
                            <asp:TextBox runat="server" id="txtTransAmtEdit" CssClass="form-control" placeholder="00.00"></asp:TextBox>
                        </div>
                    </ul>
                </div>
                <div class="modal-footer">
                    <asp:Button runat="server" CssClass="btn btn-primary" ID="btnEdit" Text="Update" UseSubmitBehavior="false" OnClick="btnEdit_Click"/>
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>

<div class="modal fade" id="modalAddPurchase" tabindex="-1" role="dialog">
        <div class="modal-dialog" style="width: 768px;">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="modalAddPurchaseLabel">Add Purchase</h4>
                </div>
                <div class="modal-body">
                    <ul class="row">
                        <div class="col-md-4" align="right">
                            <label class="control-label">Fund:</label>
                        </div>
                        <div class="col-md-5">
                            <asp:DropDownList class="form-control chosen-select_inModal" ID="ddlFundAdd" Width="400" runat="server"></asp:DropDownList>
                        </div>
                    </ul>
                    <ul class="row">
                        <div class="col-md-4" align="right">
                            <label class="control-label">Fund Currency:</label>
                        </div>
                        <div class="col-md-5">
                            <asp:TextBox runat="server" ID="txtFundCurrencyAdd" CssClass="form-control"></asp:TextBox>
                        </div>
                    </ul>
                    <ul class="row">
                        <div class="col-md-4" align="right">
                            <label class="control-label">Amount Invest:</label>
                        </div>
                        <div class="col-md-5">
                            <asp:TextBox runat="server" id="txtAmountInvestAdd" CssClass="form-control" placeholder="Amount Invest"></asp:TextBox>
                        </div>
                    </ul>
                    <ul class="row">
                        <div class="col-md-4" align="right">
                            <label class="control-label">Payment Currency:</label>
                        </div>
                        <div class="col-md-5">
                            <asp:TextBox runat="server" id="txtPaymentCurrencyAdd" CssClass="form-control" placeholder="Payment Currency"></asp:TextBox>
                        </div>
                    </ul>
                    <ul class="row">
                        <div class="col-md-4" align="right">
                            <label class="control-label">FX Rate:</label>
                        </div>
                        <div class="col-md-5">
                            <asp:TextBox runat="server" id="txtFxRateAdd" CssClass="form-control" placeholder ="FX Rate"></asp:TextBox>
                        </div>
                    </ul>
                    <ul class="row">
                        <div class="col-md-4" align="right">
                            <label class="control-label">Entry Fee %:</label>
                        </div>
                        <div class="col-md-5">
                            <asp:TextBox runat="server" id="txtEntryFeeRateAdd" CssClass="form-control"></asp:TextBox>
                        </div>
                    </ul>
                    <ul class="row">
                        <div class="col-md-4" align="right">
                            <label class="control-label">Entry Fee:</label>
                        </div>
                        <div class="col-md-5">
                            <asp:TextBox runat="server" id="txtEntryFeeAmtAdd" CssClass="form-control"></asp:TextBox>
                        </div>
                    </ul>
                    <ul class="row">
                        <div class="col-md-4" align="right">
                            <label class="control-label">GST %:</label>
                        </div>
                        <div class="col-md-5">
                            <asp:TextBox runat="server" id="txtGstRateAdd" CssClass="form-control"></asp:TextBox>
                        </div>
                    </ul>
                    <ul class="row">
                        <div class="col-md-4" align="right">
                            <label class="control-label">GST Amount:</label>
                        </div>
                        <div class="col-md-5">
                            <asp:TextBox runat="server" id="txtGstAmtAdd" CssClass="form-control"></asp:TextBox>
                        </div>
                    </ul>
                    <ul class="row">
                        <div class="col-md-4" align="right">
                            <label class="control-label">Trans Amount:</label>
                        </div>
                        <div class="col-md-5">
                            <asp:TextBox runat="server" id="txtTransAmtAdd" CssClass="form-control" placeholder="Transaction Amount"></asp:TextBox>
                        </div>
                    </ul>
                </div>
                <div class="modal-footer">
                    <asp:Button runat="server" CssClass="btn btn-primary" ID="btnAdd" Text="Add" UseSubmitBehavior="false" OnClick="btnAdd_Click" data-dismiss="modal"/>
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>

C#

protected void OpenEdit_OnClick(object sender, EventArgs e)
        {
            if (gvDetails.Rows.Count > 0)
            {
                GridViewRow row = gvDetails.SelectedRow;
                ddlFundEdit.SelectedValue = row.Cells[1].Text;
                txtFundCurrencyEdit.Text = row.Cells[3].Text;
                txtAmountInvestEdit.Text = row.Cells[4].Text;
                txtPaymentCurrencyEdit.Text = row.Cells[5].Text;
                txtEntryFeeRateEdit.Text = row.Cells[6].Text;
                txtEntryFeeAmtEdit.Text = row.Cells[7].Text;
                txtGstRateEdit.Text = row.Cells[8].Text;
                txtGstAmtEdit.Text = row.Cells[9].Text;
                txtTransAmtEdit.Text = row.Cells[10].Text;
                txtFxRateEdit.Text = row.Cells[11].Text;
            }
        }

protected void btnAdd_Click(object sender, EventArgs e)
        {
            int rowIndex = 0;

            DataTable dtCurrentTable = new DataTable();
            DataRow drCurrentRow = null;

            if (ViewState["CurrentTable"] != null)
            {
                dtCurrentTable = (DataTable) ViewState["CurrentTable"];

                if (dtCurrentTable.Rows.Count > 0)
                {
                    for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                    {
                        // Extract current values in table
                        string id = gvDetails.Rows[rowIndex].Cells[0].Text;
                        string fund = gvDetails.Rows[rowIndex].Cells[1].Text;
                        string fundName = gvDetails.Rows[rowIndex].Cells[2].Text;
                        string fundCurrency = gvDetails.Rows[rowIndex].Cells[3].Text;
                        string amountInvest = gvDetails.Rows[rowIndex].Cells[4].Text;
                        string paymentCurrency = gvDetails.Rows[rowIndex].Cells[5].Text;
                        string entryFeeRate = gvDetails.Rows[rowIndex].Cells[6].Text;
                        string entryFeeAmount = gvDetails.Rows[rowIndex].Cells[7].Text;
                        string gstRate = gvDetails.Rows[rowIndex].Cells[8].Text;
                        string gstAmount = gvDetails.Rows[rowIndex].Cells[9].Text;
                        string transactionAmount = gvDetails.Rows[rowIndex].Cells[10].Text;
                        string fxRate = gvDetails.Rows[rowIndex].Cells[11].Text;

                        dtCurrentTable.Rows[i - 1]["ID"] = id;
                        dtCurrentTable.Rows[i - 1]["PRODUCT_CODE"] = fund;
                        dtCurrentTable.Rows[i - 1]["PRODUCT_NAME"] = fundName;
                        dtCurrentTable.Rows[i - 1]["FUND_CURRENCY"] = fundCurrency;
                        dtCurrentTable.Rows[i - 1]["INVEST_AMT"] = amountInvest;
                        dtCurrentTable.Rows[i - 1]["PAYMENT_CURRENCY"] = paymentCurrency;
                        dtCurrentTable.Rows[i - 1]["ENTRY_FEE_RATE"] = entryFeeRate;
                        dtCurrentTable.Rows[i - 1]["ENTRY_FEE_AMT"] = entryFeeAmount;
                        dtCurrentTable.Rows[i - 1]["GST_RATE"] = gstRate;
                        dtCurrentTable.Rows[i - 1]["GST_AMT"] = gstAmount;
                        dtCurrentTable.Rows[i - 1]["TRANS_AMT"] = transactionAmount;
                        dtCurrentTable.Rows[i - 1]["FX_RATE"] = fxRate;

                        rowIndex++;
                    }
                }
            }
            else
            {
                dtCurrentTable.Columns.Add("ID", typeof(string));
                dtCurrentTable.Columns.Add("PRODUCT_CODE", typeof(string));
                dtCurrentTable.Columns.Add("PRODUCT_NAME", typeof(string));
                dtCurrentTable.Columns.Add("FUND_CURRENCY", typeof(string));
                dtCurrentTable.Columns.Add("INVEST_AMT", typeof(string));
                dtCurrentTable.Columns.Add("PAYMENT_CURRENCY", typeof(string));
                dtCurrentTable.Columns.Add("ENTRY_FEE_RATE", typeof(string));
                dtCurrentTable.Columns.Add("ENTRY_FEE_AMT", typeof(string));
                dtCurrentTable.Columns.Add("GST_RATE", typeof(string));
                dtCurrentTable.Columns.Add("GST_AMT", typeof(string));
                dtCurrentTable.Columns.Add("TRANS_AMT", typeof(string));
                dtCurrentTable.Columns.Add("FX_RATE", typeof(string));
            }

            drCurrentRow = dtCurrentTable.NewRow();
            drCurrentRow["ID"] = rowIndex + 1;
            drCurrentRow["PRODUCT_CODE"] = ddlFundAdd.SelectedValue;
            drCurrentRow["PRODUCT_NAME"] = ddlFundAdd.SelectedItem.Text;
            drCurrentRow["FUND_CURRENCY"] = txtFundCurrencyAdd.Text;
            drCurrentRow["INVEST_AMT"] = txtAmountInvestAdd.Text;
            drCurrentRow["PAYMENT_CURRENCY"] = txtPaymentCurrencyAdd.Text;
            drCurrentRow["ENTRY_FEE_RATE"] = txtEntryFeeRateAdd.Text;
            drCurrentRow["ENTRY_FEE_AMT"] = txtEntryFeeAmtAdd.Text;
            drCurrentRow["GST_RATE"] = txtGstRateAdd.Text;
            drCurrentRow["GST_AMT"] = txtGstAmtAdd.Text;
            drCurrentRow["TRANS_AMT"] = txtTransAmtAdd.Text;
            drCurrentRow["FX_RATE"] = txtFxRateAdd.Text;

            dtCurrentTable.Rows.Add(drCurrentRow);
            ViewState["CurrentTable"] = dtCurrentTable;

            BindGrid();

        }

protected void BindGrid()
        {
            if (ViewState["CurrentTable"] != null)
            {
                gvDetails.DataSource = ViewState["CurrentTable"] as DataTable;
            }
            else
            {
                gvDetails.DataSource = null;
            }
            gvDetails.DataBind();
        }

3 个答案:

答案 0 :(得分:1)

所有内容都应该在asp:UpdatePanel标记内,甚至是模式<div class="modal fade" ...>。这应该有用。

这就是UpdatePanel的工作方式:当服务器调用触发时,只更新其ContentTemplate区域(即使您尝试更新页面的其他部分)。

修改

我认为UpdatePanel的触发器存在问题:它应该定向到网格的点击事件,但目前似乎并非如此。

“编辑”按钮已配置命令:CommandName="Edit",因此您需要捕获网格的OnRowCommand事件,如下所示:

<asp:GridView ID="gvDetails" ... OnRowCommand="grid_RowCommand">

也会更改UpdatePanel的触发器,如下所示:

<asp:AsyncPostBackTrigger ControlID="gvDetails" EventName="OnRowCommand" />

并在代码背后:

protected void grid_RowCommand(object sender, GridViewCommandEventArgs e)
{
   if (e.CommandName == "Edit")
   {
      // your current OpenEdit_OnClick code
   }
}

答案 1 :(得分:0)

GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow;

现在从行中获取数据。

答案 2 :(得分:0)

只是更新并告知您我之前已设法解决此问题。

1)我将gridview中的LinkBut​​ton更改为Button

2)在OnEdit_Click中添加了这个:

Button btn = sender as Button;
GridViewRow row = btn.NamingContainer as GridViewRow;

string id = gvDetails.DataKeys[row.RowIndex].Values[0].ToString();

3)将modalEditPurchase放在UpdatePanel

4)使用id。

填充modalEditPurchase

谢谢大家的帮助。