1

I have a modalpopupextender that pops up a gridview that is preloaded with the edit and delete buttons. I created an event that should be able to edit a row in the grid when the edit button is clicked. But when its clicked the only thing that happens is that it just comes to a blank page. Here is the code for my class:

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);
        }

    }


}
Brad Hazelnut
  • 1,603
  • 5
  • 21
  • 33

1 Answers1

1

You can do it more simpler way. This is not the actual code. Just to help you to achieve what you want to do.

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()
}
  • nice, i like it, but i need to create it dynamically in codebehind – Brad Hazelnut Sep 11 '13 at 18:57
  • I appreciate your efforts and approach towards building a generic approach. If you dont want to go for above one they try enbedding it into user control. That will be helpful too. You need just a drag and drop and wind it with some code and you are done. IMO.. you are doing is an overkill. Always try to KISS ;-). –  Sep 11 '13 at 19:01
  • good point, i never thought of using a user control and i never really used that before, do you have a simple example that i could use – Brad Hazelnut Sep 11 '13 at 19:09
  • Luckily I found the article by the guy who is behind the CodeProject. http://www.codeproject.com/Articles/1739/User-controls-in-ASP-NET –  Sep 11 '13 at 19:15
  • so just so i understand, i am going to create a usercontrol and have the edittable gridview in there, is there a way to add this usercontrol dynamically through codebehind – Brad Hazelnut Sep 11 '13 at 19:19
  • http://stackoverflow.com/questions/2275625/asp-net-custom-user-control-to-add-dynamically –  Sep 11 '13 at 19:24
  • You can instantiate a new instance of the UesrControl object and add it to the page (into a panel or other placeholder) from the codebehind. – withoutIf Sep 11 '13 at 19:24
  • going to try it now, and see how it works, thanks for help, will update to let you know how it works – Brad Hazelnut Sep 11 '13 at 19:33
  • wow, i am super confused with the usercontrol, i created a usercontrol and added the gridview in there and added the edit templates in there, but i can get how to load the datasource, meaning i need to pass it a parameter for datasource so it can load the correct one. – Brad Hazelnut Sep 11 '13 at 19:50
  • try googling your keywords... everything is there in tonne –  Sep 11 '13 at 20:00
  • i did, but it seems a lot more complicated than trying to get it to work with what i have, it seems like im so close but i just can't figure out how to get the edit row to work – Brad Hazelnut Sep 11 '13 at 20:02