在gridview行编辑C#中使用模态

时间:2017-03-09 08:42:08

标签: c# asp.net twitter-bootstrap gridview

这是我的自助模式

<div class="modal fade" id="myModal">
            <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">
                            </h4>
                    </div>
                    <div class="modal-body">
                        <asp:Label ID="lblMessage" runat="server" />
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">
                            Close</button>
                        <button type="button" class="btn btn-primary">
                            Save changes</button>
                    </div>
                </div>
                <!-- /.modal-content -->
            </div>
            <!-- /.modal-dialog -->
        </div>

我想打开按钮点击模式,

该按钮位于我的gridview项目模板

我的按钮看起来像这样

<ItemTemplate>
          <asp:LinkButton ID="btnedit" runat="server" data-toggle="modal" data-target="#myModal" CommandName="Edit" Text="Edit" CssClass="btn btn-warning" Width="100%"></asp:LinkButton>
</ItemTemplate>

现在我在行编辑时创建了一个受保护的void方法,

看起来像这样

protected void dgvEdit_RowEditing(object sender, GridViewEditEventArgs e)
        {
            dgvEdit.EditIndex = e.NewEditIndex;
            int index = e.NewEditIndex;
            LoadGridTask("EDIT", Session["CurrentUser"].ToString(), Session["TaskID"].ToString());

            Label taskinmodal = dgvEdit.Rows[index].FindControl("lblTaskName")as Label;
            Response.Write(taskinmodal.Text);

            ClientScript.RegisterStartupScript(this.GetType(), "alert", "ShowPopup();", true);
            this.lblMessage.Text = taskinmodal.Text;
        }

问题是当我添加模态的代码并将目标ID设置为我的模态时,

dgvEdit_RowEditing中的代码未执行,

我想在我的模态中添加gridview行的值,

我认为这会阻止我的模态(data-toggle="modal" data-target="#myModal"执行CommandName="Edit"

我需要做什么?

2 个答案:

答案 0 :(得分:0)

尝试使用rowCommand而不是使用&#39; dgvEdit_RowEditing&#39;      在GridView中添加

 OnRowCommand="dgvEdit_RowCommand"  

并改变你的方法(&#39; dgvEdit_RowEditing&#39;),像这样,

protected void dgvEdit_RowCommand(object sender, GridViewCommandEventArgs e)
{
   if(e.CommandName == "Edit")
   {
    dgvEdit.EditIndex = e.NewEditIndex;
        int index = e.NewEditIndex;
        LoadGridTask("EDIT", Session["CurrentUser"].ToString(), Session["TaskID"].ToString());

        Label taskinmodal = dgvEdit.Rows[index].FindControl("lblTaskName")as Label;
        Response.Write(taskinmodal.Text);

        ClientScript.RegisterStartupScript(this.GetType(), "alert", "ShowPopup();", true);
        this.lblMessage.Text = taskinmodal.Text;
    }
  }

希望它有效!!!

答案 1 :(得分:0)

正如您所指出的,删除此 data-toggle =“modal”data-target =“#myModal”,然后执行 dgvEdit_RowCommand 。在你的dgvEdit_RowCommand方法

protected void dgvEdit_RowCommand(object sender, GridViewCommandEventArgs e)
{
   if(e.CommandName == "Edit")
   {
    dgvEdit.EditIndex = e.NewEditIndex;
        int index = e.NewEditIndex;
        LoadGridTask("EDIT", Session["CurrentUser"].ToString(), Session["TaskID"].ToString());
    Label taskinmodal = dgvEdit.Rows[index].FindControl("lblTaskName")as Label;
    Response.Write(taskinmodal.Text);

    ClientScript.RegisterStartupScript(this.GetType(), "alert", "ShowPopup();", true);
    this.lblMessage.Text = taskinmodal.Text;
}

}

在showpopup()函数中,请致电

function ShowPopup(){
$("#myModal").modal();
}