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 update statement

Gridview update statement

Scheduled Pinned Locked Moved ASP.NET
tutorialquestionannouncement
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.
  • T Offline
    T Offline
    Taen_Karth
    wrote on last edited by
    #1

    I ahve a gridview control on my form. In the gridview i have an Item Template like: Yes No I cannot understand how to capture the DropDownList value in my update statement. Any ideas? Here is current Update parameters: Thanks, Taen Karth

    M 1 Reply Last reply
    0
    • T Taen_Karth

      I ahve a gridview control on my form. In the gridview i have an Item Template like: Yes No I cannot understand how to capture the DropDownList value in my update statement. Any ideas? Here is current Update parameters: Thanks, Taen Karth

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

      Hi there, You can simply use the Bind method to do the updating with the dropdownlist control as the Bind method supports the two-way databinding. The sample will go like this:

      <asp:DropDownList ID="ddlComplete" runat="server" SelectedValue='<%# Bind("NComplete") %>'>
      asp:ListItemYes</asp:ListItem>
      <asp:ListItem Selected="True">No</asp:ListItem>
      </asp:DropDownList>

      Another way comes to mind is to use the ControlParameter[^] declared in the UpdateParameters field. Basically, the ControlParameter allows you to set the parameter to the selected property of a control like the dropdownlist. So what you need to do is to specify the control id in the controlid property and the selected property in the propertyname:

      <asp:controlparameter name="Description"
      controlid="GridView1$ctl02$ddlComplete" propertyname="SelectedValue"/>

      However, you should pay a bit of your attention to the control id of the dropdownlist specified in the controlid property. The value of the property includes the id of the container since the dropdownlist is placed inside the GridView control. Also, the index of the selected row is dynamic, not static like 2 in the example, so you'll have to update the controlid property based on the currently selected row, for example you can do that in the RowUpdating event.

      T 1 Reply Last reply
      0
      • M minhpc_bk

        Hi there, You can simply use the Bind method to do the updating with the dropdownlist control as the Bind method supports the two-way databinding. The sample will go like this:

        <asp:DropDownList ID="ddlComplete" runat="server" SelectedValue='<%# Bind("NComplete") %>'>
        asp:ListItemYes</asp:ListItem>
        <asp:ListItem Selected="True">No</asp:ListItem>
        </asp:DropDownList>

        Another way comes to mind is to use the ControlParameter[^] declared in the UpdateParameters field. Basically, the ControlParameter allows you to set the parameter to the selected property of a control like the dropdownlist. So what you need to do is to specify the control id in the controlid property and the selected property in the propertyname:

        <asp:controlparameter name="Description"
        controlid="GridView1$ctl02$ddlComplete" propertyname="SelectedValue"/>

        However, you should pay a bit of your attention to the control id of the dropdownlist specified in the controlid property. The value of the property includes the id of the container since the dropdownlist is placed inside the GridView control. Also, the index of the selected row is dynamic, not static like 2 in the example, so you'll have to update the controlid property based on the currently selected row, for example you can do that in the RowUpdating event.

        T Offline
        T Offline
        Taen_Karth
        wrote on last edited by
        #3

        Ok excellent... I was trying to do your second option yet it could not find the "controlid" of the dropdownlist as you have mentioned. However, I don't quite understand how I can update the "controlid" durning the RowUpdating event. Would it be something like: Protected Sub Gridview1_RowUpdating(ByVal.......) Dim ddlID as String = ddlNComplete Gridview1.SelectedRow.FindControl("DropDownList").ID = ddlID End Sub Let me know and thanks for the info ;) Thanks, Taen Karth

        M 1 Reply Last reply
        0
        • T Taen_Karth

          Ok excellent... I was trying to do your second option yet it could not find the "controlid" of the dropdownlist as you have mentioned. However, I don't quite understand how I can update the "controlid" durning the RowUpdating event. Would it be something like: Protected Sub Gridview1_RowUpdating(ByVal.......) Dim ddlID as String = ddlNComplete Gridview1.SelectedRow.FindControl("DropDownList").ID = ddlID End Sub Let me know and thanks for the info ;) Thanks, Taen Karth

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

          The reason I use the RowUpdating event is that this event occurs just before the parameters are populated and the GridView control updates the row. For example, the GridView control uses the SQL datsource defined as below:

          <asp:SqlDataSource
          ID="SqlDataSource1"
          Runat="Server"
          UpdateCommand="..."
          ...

          <UpdateParameters>
          <asp:controlparameter name="NComplete" controlid="GridView1$ctl02$ddlNComplete"
          propertyname="SelectedValue"/>
          ...
          </UpdateParameters>
          </asp:SqlDataSource>

          The sample code for the RowUpdating event handler looks something like:

          protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
          {
          ControlParameter parameter = (ControlParameter)SqlDataSource1.UpdateParameters[0];
          int index = e.RowIndex + 2;
          parameter.ControlID = "GridView1$ctl0" + index + "$ddlNComplete";
          }

          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