Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. GridView event handler not working

GridView event handler not working

Scheduled Pinned Locked Moved ASP.NET
algorithmsdebuggingquestionworkspace
12 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    compninja25
    wrote on last edited by
    #1

    Hi all, I'm still fighting with getting multiple lines in a dynamic gridview. Searching around led me to think that if I can change the data during the rowcreated handler this just might work.

    gv2.RowCreated += new GridViewRowEventHandler(gv2_RowCreated);
    gv2.DataBind();

    protected void gv2_RowCreated(object sender, GridViewRowEventArgs e)
    {
    for (int i = 0; i < 5; i++)
    {
    e.Row.Cells[i].Text.Replace(Environment.NewLine, "
    ");
    }
    }

    Whenever I run the debugger, the new event handler line adds the handler, but never runs through the block of code. I'd imagine that during the DataBind, the rows are being created, so why isn't the system seeing this event?

    "You're damned if you do, and you're damned if you dont" - Bart Simpson

    A B C 3 Replies Last reply
    0
    • C compninja25

      Hi all, I'm still fighting with getting multiple lines in a dynamic gridview. Searching around led me to think that if I can change the data during the rowcreated handler this just might work.

      gv2.RowCreated += new GridViewRowEventHandler(gv2_RowCreated);
      gv2.DataBind();

      protected void gv2_RowCreated(object sender, GridViewRowEventArgs e)
      {
      for (int i = 0; i < 5; i++)
      {
      e.Row.Cells[i].Text.Replace(Environment.NewLine, "
      ");
      }
      }

      Whenever I run the debugger, the new event handler line adds the handler, but never runs through the block of code. I'd imagine that during the DataBind, the rows are being created, so why isn't the system seeing this event?

      "You're damned if you do, and you're damned if you dont" - Bart Simpson

      A Offline
      A Offline
      Abhijit Jana
      wrote on last edited by
      #2

      itmaster21 wrote:

      gv2.RowCreated += new GridViewRowEventHandler(gv2_RowCreated);

      From where you are executing this ? If you want to execute Event handler of a runtime created controls, you need to add this event handler before Page_Load()

      cheers, Abhijit My Recent Article : Beginner's Guide to ASP.NET Application Folder

      C 2 Replies Last reply
      0
      • C compninja25

        Hi all, I'm still fighting with getting multiple lines in a dynamic gridview. Searching around led me to think that if I can change the data during the rowcreated handler this just might work.

        gv2.RowCreated += new GridViewRowEventHandler(gv2_RowCreated);
        gv2.DataBind();

        protected void gv2_RowCreated(object sender, GridViewRowEventArgs e)
        {
        for (int i = 0; i < 5; i++)
        {
        e.Row.Cells[i].Text.Replace(Environment.NewLine, "
        ");
        }
        }

        Whenever I run the debugger, the new event handler line adds the handler, but never runs through the block of code. I'd imagine that during the DataBind, the rows are being created, so why isn't the system seeing this event?

        "You're damned if you do, and you're damned if you dont" - Bart Simpson

        B Offline
        B Offline
        Brij
        wrote on last edited by
        #3

        Why are you not using RowDataBound event?I think it'll fulfill you requirement.

        Cheers!! Brij

        C A 2 Replies Last reply
        0
        • B Brij

          Why are you not using RowDataBound event?I think it'll fulfill you requirement.

          Cheers!! Brij

          C Offline
          C Offline
          compninja25
          wrote on last edited by
          #4

          I tried that first but it gave me the same results.

          "You're damned if you do, and you're damned if you dont" - Bart Simpson

          B 1 Reply Last reply
          0
          • A Abhijit Jana

            itmaster21 wrote:

            gv2.RowCreated += new GridViewRowEventHandler(gv2_RowCreated);

            From where you are executing this ? If you want to execute Event handler of a runtime created controls, you need to add this event handler before Page_Load()

            cheers, Abhijit My Recent Article : Beginner's Guide to ASP.NET Application Folder

            C Offline
            C Offline
            compninja25
            wrote on last edited by
            #5

            Ah, maybe that's why! It's actually in the block of code that is being called from within the Page_Load().

            "You're damned if you do, and you're damned if you dont" - Bart Simpson

            C 1 Reply Last reply
            0
            • B Brij

              Why are you not using RowDataBound event?I think it'll fulfill you requirement.

              Cheers!! Brij

              A Offline
              A Offline
              Abhijit Jana
              wrote on last edited by
              #6

              Brij wrote:

              Why are you not using RowDataBound event?I think it'll fulfill you requirement.

              No Brij, I Think, his problem is something different. He Needs add event handler before page_load.

              cheers, Abhijit My Recent Article : Beginner's Guide to ASP.NET Application Folder

              B 1 Reply Last reply
              0
              • C compninja25

                Ah, maybe that's why! It's actually in the block of code that is being called from within the Page_Load().

                "You're damned if you do, and you're damned if you dont" - Bart Simpson

                C Offline
                C Offline
                compninja25
                wrote on last edited by
                #7

                Well, after moving it to the Page_PreLoad the event fires but apparently replacing the element.newline with the "
                " didn't do the trick. Dang!

                "You're damned if you do, and you're damned if you dont" - Bart Simpson

                modified on Wednesday, December 17, 2008 1:01 PM

                1 Reply Last reply
                0
                • A Abhijit Jana

                  Brij wrote:

                  Why are you not using RowDataBound event?I think it'll fulfill you requirement.

                  No Brij, I Think, his problem is something different. He Needs add event handler before page_load.

                  cheers, Abhijit My Recent Article : Beginner's Guide to ASP.NET Application Folder

                  B Offline
                  B Offline
                  Brij
                  wrote on last edited by
                  #8

                  Hey Dude Adding a eventhandler with every row means he wants to execute some statements on every row addiotion.My answer was the same can be done using RowdataBound function with ease.

                  Cheers!! Brij

                  1 Reply Last reply
                  0
                  • C compninja25

                    I tried that first but it gave me the same results.

                    "You're damned if you do, and you're damned if you dont" - Bart Simpson

                    B Offline
                    B Offline
                    Brij
                    wrote on last edited by
                    #9

                    can you share your piece of code? aspx and c# code.

                    Cheers!! Brij

                    1 Reply Last reply
                    0
                    • A Abhijit Jana

                      itmaster21 wrote:

                      gv2.RowCreated += new GridViewRowEventHandler(gv2_RowCreated);

                      From where you are executing this ? If you want to execute Event handler of a runtime created controls, you need to add this event handler before Page_Load()

                      cheers, Abhijit My Recent Article : Beginner's Guide to ASP.NET Application Folder

                      C Offline
                      C Offline
                      compninja25
                      wrote on last edited by
                      #10

                      Here's the code:

                      using System;
                      using System.Collections.Generic;
                      using System.Web;
                      using System.Web.UI;
                      using System.Web.UI.WebControls;
                      using System.Data;
                      using System.Xml;
                      using System.IO;
                      using System.Runtime.Serialization.Formatters.Binary;
                      using System.Collections;

                      public partial class Subpgs_Warehouse_ShippingCalendar : System.Web.UI.Page
                      {
                      public Dictionary content1 = new Dictionary();

                      protected void Page\_Preload(object sender, EventArgs e)
                      {
                          Generate\_Table();
                      }
                      
                      protected void Page\_Load(object sender, EventArgs e)
                      {
                          //Generate\_Table();        
                      }
                      
                      protected void Generate\_Table()
                      {
                          try
                          {            
                                  using (Stream input = File.OpenRead(Server.MapPath("~/Subpgs/Data/Shipping/Calendar.binary")))
                                  {
                                      BinaryFormatter bf = new BinaryFormatter();
                                      content1 = (Dictionary)bf.Deserialize(input);
                                  }
                                  
                           }
                          catch
                          {
                      
                          }
                          
                          int date = 0;
                      
                          switch (DateTime.Today.DayOfWeek.ToString())
                          {
                              case "Sunday":
                                  date = -6;
                                  break;
                              case "Monday":
                                  date = -7;
                                  break;
                              case "Tuesday":
                                  date = -8;
                                  break;
                              case "Wednesday":
                                  date = -9;
                                  break;
                              case "Thursday":
                                  date = -10;
                                  break;
                              case "Friday":
                                  date = -11;
                                  break;
                              case "Saturday":
                                  date = -12;
                                  break;
                          }
                      
                          int datelist = date;       
                      
                      
                          DataTable Table1 = new DataTable("last\_week");
                          DataTable Table2 = new DataTable("this\_week");
                          DataTable Table3 = new DataTable("next\_week");
                          
                          DataRow dr1 = Table1.NewRow();
                          for (int i = 0; i < 5; i++)
                          {
                              Table1.Columns.Add(DateTime.Today.AddDays(date).DayOfWeek.ToString() + " " + DateTime.Today.AddDays(date).ToShortDateString(), typeof(string));
                              try{
                                  dr1\[i\] = content1\[DateTime.Today.AddDays(date).ToShortDateString()\];
                                 }
                              catch{
                                  dr1\[i\] = "No Data supplied";
                                   }
                              date++;                     
                          }
                      
                          date += 2;
                      
                      1 Reply Last reply
                      0
                      • C compninja25

                        Hi all, I'm still fighting with getting multiple lines in a dynamic gridview. Searching around led me to think that if I can change the data during the rowcreated handler this just might work.

                        gv2.RowCreated += new GridViewRowEventHandler(gv2_RowCreated);
                        gv2.DataBind();

                        protected void gv2_RowCreated(object sender, GridViewRowEventArgs e)
                        {
                        for (int i = 0; i < 5; i++)
                        {
                        e.Row.Cells[i].Text.Replace(Environment.NewLine, "
                        ");
                        }
                        }

                        Whenever I run the debugger, the new event handler line adds the handler, but never runs through the block of code. I'd imagine that during the DataBind, the rows are being created, so why isn't the system seeing this event?

                        "You're damned if you do, and you're damned if you dont" - Bart Simpson

                        C Offline
                        C Offline
                        compninja25
                        wrote on last edited by
                        #11

                        Hey Guys, I got it working. It was actually a combination of both your responses. I did have to move everything to the Page_PreLoad() and also do a RowDataBound event handler instead of RowCreated. I also had to change the event handler to this after it dawned on me that I'm replacing but not specifying that's what I want to be in the text area!

                        e.Row.Cells[i].Text = e.Row.Cells[i].Text.Replace(Environment.NewLine, "
                        ");

                        Thank's for taking the time to help me out!

                        "You're damned if you do, and you're damned if you dont" - Bart Simpson

                        A 1 Reply Last reply
                        0
                        • C compninja25

                          Hey Guys, I got it working. It was actually a combination of both your responses. I did have to move everything to the Page_PreLoad() and also do a RowDataBound event handler instead of RowCreated. I also had to change the event handler to this after it dawned on me that I'm replacing but not specifying that's what I want to be in the text area!

                          e.Row.Cells[i].Text = e.Row.Cells[i].Text.Replace(Environment.NewLine, "
                          ");

                          Thank's for taking the time to help me out!

                          "You're damned if you do, and you're damned if you dont" - Bart Simpson

                          A Offline
                          A Offline
                          Abhijit Jana
                          wrote on last edited by
                          #12

                          Nice to know our soluation helps you!!

                          cheers, Abhijit My Recent Article : Beginner's Guide to ASP.NET Application Folder

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups