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. User control issues

User control issues

Scheduled Pinned Locked Moved ASP.NET
csshelp
13 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.
  • S Swiftain

    I have a user control with one gridview control on it and the code behind for the user control is shown below

    protected void Page_Load(object sender, EventArgs e)
    {
    gvResults.DataSource = theSource;
    gvResults.DataBind();
    }

    private List theSource;
    
    public List<string> TheSource
    {
        get { return theSource; }
        set { theSource = value; }
    }
    

    When I pass value to the control on the page load I works fine with the data being displayed on the grid as shown by the code below

    protected void Page_Load(object sender, EventArgs e)
    {
    string[] myData = {"10", "20", "30"};
    DataPager1.TheSource = myData.ToList();
    }

    But when I try to do the same thing by clicking a button, the page shows no data

    protected void SearchButton_Click(object sender, EventArgs e)
    {
    string[] myData = {"10", "20", "30"};
    DataPager1.TheSource = myData.ToList();
    }

    DataPager1 is the usercontrol ID. Any help will be appreciated.

    H Offline
    H Offline
    Hiren solanki
    wrote on last edited by
    #4

    I tried it myself, but not success at all with that. I can provide you alternate way. Just change your SearchButton_Click event with following snippets.

    protected void SearchButton_Click(object sender, EventArgs e)
    {
    string[] myData = {"10", "20", "30"};
    //DataPager1.TheSource = myData.ToList();
    GridView gview = (GridView)DataPager1.FindControl("gvResults");
    gview.DataSource = myData;
    gview.DataBind();
    }

    Regards, Hiren. Microsoft Dynamics CRM

    My Recent Article: - Way to know which control have raised PostBack[^]

    S 1 Reply Last reply
    0
    • H Hiren solanki

      I tried it myself, but not success at all with that. I can provide you alternate way. Just change your SearchButton_Click event with following snippets.

      protected void SearchButton_Click(object sender, EventArgs e)
      {
      string[] myData = {"10", "20", "30"};
      //DataPager1.TheSource = myData.ToList();
      GridView gview = (GridView)DataPager1.FindControl("gvResults");
      gview.DataSource = myData;
      gview.DataBind();
      }

      Regards, Hiren. Microsoft Dynamics CRM

      My Recent Article: - Way to know which control have raised PostBack[^]

      S Offline
      S Offline
      Swiftain
      wrote on last edited by
      #5

      Thanks Hiren for your response, what you posted will work but the only problem is that I need to manipulate the data on the user control, not on the web page itself. So I just wanted a way to pass data from the web page unto the user control and then manipulate it from there. Funny thing is that it works on the Page_Load event but doesn't on the button_click event!

      H 2 Replies Last reply
      0
      • S Swiftain

        Thanks Hiren for your response, what you posted will work but the only problem is that I need to manipulate the data on the user control, not on the web page itself. So I just wanted a way to pass data from the web page unto the user control and then manipulate it from there. Funny thing is that it works on the Page_Load event but doesn't on the button_click event!

        H Offline
        H Offline
        Hiren solanki
        wrote on last edited by
        #6

        Swiftain wrote:

        Funny thing is that it works on the Page_Load event but doesn't on the button_click event!

        I've experimented a thing already with that, But didn't find any solution for the same, Let me try other way I will let you know once I got the clue. If you're finding the solution then please put here so that it could be useful further for a members and also me.

        Regards, Hiren. Microsoft Dynamics CRM

        My Recent Article: - Way to know which control have raised PostBack[^]

        1 Reply Last reply
        0
        • S Swiftain

          But I don't have access to the gridview in SearchButton_Click I already tried DataPager1.DataBind(); and it didn't work

          T Offline
          T Offline
          thatraja
          wrote on last edited by
          #7

          Try

              gvResults.DataSource = theSource;
              gvResults.DataBind();
          

          SearchButton_Click

          thatraja |Chennai|India|


          Brainbench certifications
          Univotes are like kid's kisses don't reject it :-)
          Do what you want quickly because the Doomsday on 2012 :-)
          My childhood story

          S 1 Reply Last reply
          0
          • T thatraja

            Try

                gvResults.DataSource = theSource;
                gvResults.DataBind();
            

            SearchButton_Click

            thatraja |Chennai|India|


            Brainbench certifications
            Univotes are like kid's kisses don't reject it :-)
            Do what you want quickly because the Doomsday on 2012 :-)
            My childhood story

            S Offline
            S Offline
            Swiftain
            wrote on last edited by
            #8

            I tried it, still no luck

            T 1 Reply Last reply
            0
            • S Swiftain

              I tried it, still no luck

              T Offline
              T Offline
              thatraja
              wrote on last edited by
              #9

              Swiftain wrote:

              still no luck

              I never believe that. what's the error message? mention that too.

              thatraja |Chennai|India|


              Brainbench certifications
              Univotes are like kid's kisses don't reject it :-)
              Do what you want quickly because the Doomsday on 2012 :-)
              My childhood story

              S 1 Reply Last reply
              0
              • T thatraja

                Swiftain wrote:

                still no luck

                I never believe that. what's the error message? mention that too.

                thatraja |Chennai|India|


                Brainbench certifications
                Univotes are like kid's kisses don't reject it :-)
                Do what you want quickly because the Doomsday on 2012 :-)
                My childhood story

                S Offline
                S Offline
                Swiftain
                wrote on last edited by
                #10

                There's no error message, it just displays blank. But like I said when the same code snippet is placed in the Page_Load event, it displays the data.

                1 Reply Last reply
                0
                • S Swiftain

                  Thanks Hiren for your response, what you posted will work but the only problem is that I need to manipulate the data on the user control, not on the web page itself. So I just wanted a way to pass data from the web page unto the user control and then manipulate it from there. Funny thing is that it works on the Page_Load event but doesn't on the button_click event!

                  H Offline
                  H Offline
                  Hiren solanki
                  wrote on last edited by
                  #11

                  See THIS[^], It might be useful.

                  Regards, Hiren. Microsoft Dynamics CRM

                  My Recent Article: - Way to know which control have raised PostBack[^]

                  S 1 Reply Last reply
                  0
                  • H Hiren solanki

                    See THIS[^], It might be useful.

                    Regards, Hiren. Microsoft Dynamics CRM

                    My Recent Article: - Way to know which control have raised PostBack[^]

                    S Offline
                    S Offline
                    Swiftain
                    wrote on last edited by
                    #12

                    Instead binding the data on Page_Load event of the user control, I created a method for doing the same thing and it worked. I think the reason is because the Page_Load even of the user control fires and then that of the page fires too which happens automatically, so in order to force a bind, I had to create a simple method like this

                    public void BindData()
                    {
                        gvResults.DataSource = TheSource;
                        gvResults.DataBind();
                    }
                    

                    and then on web page, I just did this

                    protected void SearchButton\_Click(object sender, EventArgs e)
                    {
                        DataPager1.TheSource = myData.ToList();
                        DataPager1.BindData();
                    }
                    

                    Now that works, but now it looks as though the gridview events from the user control like "OnRowCreated" ain't firing. Oh well, at least I've got one bit working :)

                    H 1 Reply Last reply
                    0
                    • S Swiftain

                      Instead binding the data on Page_Load event of the user control, I created a method for doing the same thing and it worked. I think the reason is because the Page_Load even of the user control fires and then that of the page fires too which happens automatically, so in order to force a bind, I had to create a simple method like this

                      public void BindData()
                      {
                          gvResults.DataSource = TheSource;
                          gvResults.DataBind();
                      }
                      

                      and then on web page, I just did this

                      protected void SearchButton\_Click(object sender, EventArgs e)
                      {
                          DataPager1.TheSource = myData.ToList();
                          DataPager1.BindData();
                      }
                      

                      Now that works, but now it looks as though the gridview events from the user control like "OnRowCreated" ain't firing. Oh well, at least I've got one bit working :)

                      H Offline
                      H Offline
                      Hiren solanki
                      wrote on last edited by
                      #13

                      Yes, That's the way. Thanks for sharing solution.

                      Swiftain wrote:

                      "OnRowCreated" ain't firing

                      Will try to solve it as getting enough time. :)

                      Regards, Hiren.

                      My Recent Article: - Way to know which control have raised PostBack
                      My Recent Tip/Trick: - Remove HTML Tag, get plain Text

                      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