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. Dynamically adding value in drop down list of a Gridview

Dynamically adding value in drop down list of a Gridview

Scheduled Pinned Locked Moved ASP.NET
question
4 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.
  • B Offline
    B Offline
    Ballita
    wrote on last edited by
    #1

    Hi, I have a gridview which has dropdown list in template column and i am adding value dynamically to that drop down list.Now I want to save all the values which I have selected in the dropdown list. But while clicking on save button in that page it is not retaining the selected values of the ddl. How can I solve this?

    Thanks & Regards Mishra

    P 1 Reply Last reply
    0
    • B Ballita

      Hi, I have a gridview which has dropdown list in template column and i am adding value dynamically to that drop down list.Now I want to save all the values which I have selected in the dropdown list. But while clicking on save button in that page it is not retaining the selected values of the ddl. How can I solve this?

      Thanks & Regards Mishra

      P Offline
      P Offline
      pmarfleet
      wrote on last edited by
      #2

      How are you adding the items? Through Javascript? If so, your server-side code knows nothing about the items. When the page is reloaded after a postback the DDL will be reloaded from the server-side. Any items added using client-side code will be lost. You would either need to post back the details for the items added in client-script to your server, or delegate the loading of the DDL to your client-side code.

      Paul Marfleet

      B 1 Reply Last reply
      0
      • P pmarfleet

        How are you adding the items? Through Javascript? If so, your server-side code knows nothing about the items. When the page is reloaded after a postback the DDL will be reloaded from the server-side. Any items added using client-side code will be lost. You would either need to post back the details for the items added in client-script to your server, or delegate the loading of the DDL to your client-side code.

        Paul Marfleet

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

        Hi, I am adding values to the ddl in RowDataBound event using the following code. if (e.Row.RowType == DataControlRowType.DataRow) { DropDownList ddlval = new DropDownList(); ddlval = (DropDownList)e.Row.FindControl("ddlvalidation"); if (lblcheck.Text == "Suspect") { ddlval.Items.Add("Error"); ddlval.Items.Add("No Error"); } else if (lblcheck.Text == "Fatal") { ddlval.Items.Add("Not Fixed"); ddlval.Items.Add("Fixed"); } & In the button save I am trying to fetch the selected values using the following code, but I am getting the error "Object reference not set to an instance of an object." foreach (GridViewRow rec in this.gvsqldesc.Rows) { //Response.Write(rec.Cells[0].Text); DropDownList ddlval = new DropDownList(); ddlval = (DropDownList)rec.FindControl("ddlvalidation"); string str = null; str = ddlval.SelectedItem.Text; }

        Thanks & Regards Mishra

        P 1 Reply Last reply
        0
        • B Ballita

          Hi, I am adding values to the ddl in RowDataBound event using the following code. if (e.Row.RowType == DataControlRowType.DataRow) { DropDownList ddlval = new DropDownList(); ddlval = (DropDownList)e.Row.FindControl("ddlvalidation"); if (lblcheck.Text == "Suspect") { ddlval.Items.Add("Error"); ddlval.Items.Add("No Error"); } else if (lblcheck.Text == "Fatal") { ddlval.Items.Add("Not Fixed"); ddlval.Items.Add("Fixed"); } & In the button save I am trying to fetch the selected values using the following code, but I am getting the error "Object reference not set to an instance of an object." foreach (GridViewRow rec in this.gvsqldesc.Rows) { //Response.Write(rec.Cells[0].Text); DropDownList ddlval = new DropDownList(); ddlval = (DropDownList)rec.FindControl("ddlvalidation"); string str = null; str = ddlval.SelectedItem.Text; }

          Thanks & Regards Mishra

          P Offline
          P Offline
          pmarfleet
          wrote on last edited by
          #4

          Your button click event fires before the grid databinding has taken place. The controls you had added dynamically have not been recreated yet. You should wait until the grid databinding has taken place before attempting to reference any controls you have created dynamically.

          Paul Marfleet

          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