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. How should I pass a variable to a "controlid" on a ControlParameter.

How should I pass a variable to a "controlid" on a ControlParameter.

Scheduled Pinned Locked Moved ASP.NET
question
2 Posts 2 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.
  • R Offline
    R Offline
    Red_Wizard_Shot_The_Food
    wrote on last edited by
    #1

    I need to pass a variable to the controlid of a ControlParameter so i can tell a Gridview which row the dropdownlist im editing belongs to. What would be the best moethod for setting the control id? Thanks.

    M 1 Reply Last reply
    0
    • R Red_Wizard_Shot_The_Food

      I need to pass a variable to the controlid of a ControlParameter so i can tell a Gridview which row the dropdownlist im editing belongs to. What would be the best moethod for setting the control id? Thanks.

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      Basically, the ControlParameter does not support the DataBinding event so you cannot use a data binding expression to set the ControlID property of the parameter, and your choice is to do this in code. Also, you need to be aware that the ControlID property needs to be updated before it is used to populate the value, if it happens after then you'll get an error as the specified control is not found. Normally, the ControlParameter uses its ControlID property to evaluate the parameter value in the overridable Evaluate method which occurs in the LoadComplete event of the Page instance. So you can put your code to update the ControlID in the events (of the data source control or the Page instance) that happens before the Page_LoadComplete and of cource after the data source control is built. For example, you can use the Load event of the data source control:

      protected void SqlDataSource1_Load(object sender, EventArgs e)
      {
      //Assuming the ControlPatameter is the first one in the UpdateParameter collection.
      ControlParameter para = SqlDataSource1.UpdateParameters[0] as ControlParameter;
      para.ControlID = "GridView1$ctl0" + (GridView1.EditIndex + 2) + "$DropDownList1";
      }

      where the EditIndex property of the GridView control will give you the index of the row being edited, this is a zero-based value, however the index used in the UniqueID of the dropdownlist is a one-based value and it is counted from the header row. Therefore, you have to add 2 to the EditIndex to get the correct number used in the UniqueID.

      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