无法编辑以在gridview上工作

时间:2013-09-11 18:41:33

标签: c# asp.net gridview dynamic ajaxcontroltoolkit

我有一个modalpopupextender,会弹出一个预先加载了编辑和删除按钮的gridview。我创建了一个事件,当单击编辑按钮时,该事件应该能够编辑网格中的行。但是当它点击时,唯一发生的事情就是它只是一个空白页面。这是我班级的代码:

的Class1.cs

public class Class1
{
    public Class1()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public void SetInitialRow(PlaceHolder ph)
    {
        for (int i = 1; i < 5; i++)
        {
            var temp = i;
            DataTable table = GetTable();
            HttpContext.Current.Session[i.ToString()] = table;
            GridView gv = new GridView();
            gv.ID = "GridView-" + i.ToString();
            gv.DataSource = HttpContext.Current.Session[i.ToString()];
            gv.DataBind();

            Button btn = new Button();
            btn.ID = "button-" + i.ToString();
            btn.Text = "Edit Grid";


            Button cancelbtn = new Button();
            cancelbtn.ID = "Cancel-" + i.ToString();
            cancelbtn.Text = "Cancel";


            GridView gv1 = new GridView();
            gv1.ID = "GridViewNew-" + (i + 5).ToString();
            gv1.AutoGenerateColumns = true;
            gv1.AutoGenerateDeleteButton = true;
            gv1.AutoGenerateEditButton = true;
            gv1.RowEditing += (sender, e) => gv1_RowEdit(sender, e, gv1);
            gv1.DataSource = HttpContext.Current.Session[(i).ToString()];
            gv1.DataBind();

            Panel pn = new Panel();
            pn.ID = "Panel-" + (i + 5).ToString();
            pn.CssClass = "modalPopup";
            pn.Controls.Add(gv1);
            pn.Controls.Add(cancelbtn);


            AjaxControlToolkit.ModalPopupExtender modalPop = new AjaxControlToolkit.ModalPopupExtender();
            modalPop.ID = "ModalPopup" + i;
            modalPop.PopupControlID = pn.ID;
            modalPop.TargetControlID = btn.ID;
            modalPop.CancelControlID = cancelbtn.ID;
            modalPop.BackgroundCssClass = "modalBackground";

            // Adding modalpop to panel
            pn.Controls.Add(modalPop);
            pn.Controls.Add(gv1);

            // Adding Panel to placeholder
            ph.Controls.Add(pn);


            ph.Controls.Add(gv);
            ph.Controls.Add(btn);


        }
    }

    private void gv1_RowEdit(object sender, GridViewEditEventArgs e, GridView gv)
    {
        gv.EditIndex = e.NewEditIndex;
    }


    static DataTable GetTable()
    {
        //
        // Here we create a DataTable with four columns.
        //
        DataTable table = new DataTable();
        table.Columns.Add("Dosage", typeof(int));
        table.Columns.Add("Drug", typeof(string));
        table.Columns.Add("Patient", typeof(string));
        table.Columns.Add("Date", typeof(DateTime));

        //
        // Here we add five DataRows.
        //
        table.Rows.Add(25, "Indocin", "David", DateTime.Now);
        table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
        table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
        table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
        table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
        return table;
    }


}

Default3.aspx.cs

public partial class Default3 : System.Web.UI.Page
{
    // Global Object initializer
    Class1 inv = new Class1();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            inv.SetInitialRow(PlaceHolder1);
        }

    }


}

1 个答案:

答案 0 :(得分:1)

你可以更简单的方式做到这一点。这不是实际的代码。只是为了帮助您实现您想要的目标。

ASPX HTML:

<asp:Gridview>
  <columns>
   <asp:TemplateField>
    <ItemTemplate>

    <asp:LinkButton id="abc" runat="server" onclientclick="javascript:myClick();"></asp:linkbutton>
</ItemTemplate>
</asp:TemplateField>
</columns>
</asp:GridView>
<asp:Button id="xyx" runat="server" style="display:none" onclick="btn_Click"/>

<ajx:modalpopupextender id="ccc" runat="server"></ajx:modalpopupextender>
<script>
function myClick()
{
  document.getElementById('<%=xyx.ClientID%>').click();
}

CS

protected void btn_Click()
{
  this.ccc.show()
}