使用ModalPopupExtender&编辑GridView行值。在GridView中保存值

时间:2016-06-23 08:25:39

标签: c# asp.net gridview

我正在使用ModalPopUpExtender来显示GridView中项目的详细信息,我似乎无法使其工作,在从Popup编辑Row之后,它将返回到页面,它也将随着任何数据库访问而改变。

aspx表单

<asp:GridView runat="server" ID="gridview" CssClass="table-hover" AutoGenerateColumns="true" HeaderStyle-BackColor="CornflowerBlue" BackColor="White" BorderWidth="5" BorderColor="CornflowerBlue" OnSelectedIndexChanged="gridview_SelectedIndexChanged" CellPadding="10"
     CellSpacing="0" Width="100%">
    <HeaderStyle BackColor="CornflowerBlue"></HeaderStyle>
    <Columns>
        <asp:TemplateField ItemStyle-Width="30px" HeaderText="Details">
            <ItemTemplate>
                <asp:ImageButton ID="lnkEdit" runat="server" ImageUrl="~/Images/Icons/Modify.png" OnClick="Edit" />
                <%--<asp:LinkButton ID="lnkEdit" runat="server" Text="Edit" OnClick="Edit"></asp:LinkButton>--%>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
                 <asp:Panel ID="pnlAddEdit" runat="server" CssClass="modal-content modal-body">
                <asp:Label Font-Bold ="true" runat="server" ID ="lblDetails" Text="Details"></asp:Label>
                <br />
                <table align ="center">
                    <tr>
                        <td>
                            <asp:Label ID="label1" runat="server" Text ="Type"></asp:Label>
                        </td>
                        <td>
                            <asp:TextBox ID ="typetxt" CssClass="form-control" runat="server"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:Label ID="label2" runat="server" Text ="Model"></asp:Label>
                        </td>
                        <td>
                            <asp:TextBox ID ="modeltxt" CssClass="form-control" runat="server"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:Label ID="label3" runat="server" Text ="Quantity"></asp:Label>
                        </td>
                        <td>
                            <asp:TextBox ID ="quantxt" CssClass="form-control" runat="server"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:Label ID="label4" runat="server" Text ="Unit"></asp:Label>
                        </td>
                        <td>
                            <asp:TextBox ID ="txtunit" CssClass="form-control" runat="server"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:Label ID="label5" runat="server" Text ="Description"></asp:Label>
                        </td>
                        <td>
                            <asp:TextBox ID ="descritxt" CssClass="form-control" runat="server"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:Label ID="label6" runat="server" Text ="Remarks"></asp:Label>
                        </td>
                        <td>
                            <asp:TextBox ID ="remarkstxt" CssClass="form-control" runat="server"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:Label ID="label7" runat="server" Text ="Date"></asp:Label>
                        </td>
                        <td>
                            <asp:TextBox ID ="target" CssClass="form-control" runat="server"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:Button ID ="btnSave" runat="server" Text="Save" OnClick="Save" />
                        </td>
                        <td>
                            <asp:Button ID ="btnCancel" runat="server" Text="Cancel" OnClientClick ="return Hidepopup()" />
                        </td>
                    </tr>                        
                </table>                   
            </asp:Panel>


<asp:LinkButton ID="lnkFake" runat="server"></asp:LinkButton>
            <cc1:ModalPopupExtender ID="modal" runat="server" DropShadow="true" PopupControlID="pnlAddEdit" TargetControlID="lnkFake" BackgroundCssClass="modalBackground">
            </cc1:ModalPopupExtender>
            </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID ="gridview" />
            <asp:AsyncPostBackTrigger ControlID ="btnSave" />
        </Triggers>
        </asp:UpdatePanel>
    </div>

aspx.cs表单

 protected void Edit(object sender, EventArgs e)
    {
        using (GridViewRow row = (GridViewRow)((ImageButton)sender).Parent.Parent)
        {
            typetxt.Text = row.Cells[1].Text;
            modeltxt.Text = row.Cells[2].Text;
            quantxt.Text = row.Cells[3].Text;
            txtunit.Text = row.Cells[4].Text;
            descritxt.Text = row.Cells[5].Text;
            remarkstxt.Text = row.Cells[6].Text;
            target.Text = row.Cells[7].Text;
            modal.Show();
        }

    }

    protected void Save(object sender, EventArgs e)
    {
        //what will i put here
    }

1 个答案:

答案 0 :(得分:1)

你可以像这样保存使用实体框架(示例代码):

 protected void Save(object sender, EventArgs e)
{
    using (var context = new EntityModelNAme())
        {
            int Pid = Convert.ToInt32(Label13.Text);// Get your Primary Id value
            DBTableName Obj = (from c in context.DBTableName
                               where c.ID == Pid
                               select c).FirstOrDefault();// Match the Id with Database

            Obj.Type =  typetxt.Text;           
            Obj.Model = modeltxt.Text;
            Obj.Quant = quantxt.Text;
            // Like Above write your code
            context.SaveChanges();           
        }
        this.BindGrid();

}