[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1060
  • Last Modified:

ASP .Net GridView RowUpdating on DropDownList

Hi,

I have a menu maker program that you can add various menu items to an xml file.  To update the items, I currently have text fields for all of the fields.  Two of the fields are True/False and I want to put a DropDownList in the table.

I have my code working so that the current value shows in the first of the two True/False columns (trying one as a test).  When I go to edit the column, the drop down list appears correctly.  When I go to update the value in the row, the value returns blank/null.  My guess is that I am not using the correct VB to retrieve the value from the dropdown list.  I've commented out the VB options that haven't worked and included them here as I've spent too long trying to figure it out.

Any help would greatly be appreciated.  


Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating


        Dim i As Integer = GridView1.Rows(e.RowIndex).DataItemIndex
        'Numbers are cells are relative to the columns in gridview
        Dim strn As String = CType(GridView1.Rows(e.RowIndex).Cells(0).Controls(0), TextBox).Text
        Dim strtext As String = CType(GridView1.Rows(e.RowIndex).Cells(1).Controls(0), TextBox).Text
        Dim strurl As String = CType(GridView1.Rows(e.RowIndex).Cells(2).Controls(0), TextBox).Text
        Dim strdescr As String = CType(GridView1.Rows(e.RowIndex).Cells(3).Controls(0), TextBox).Text
        Dim strparent As String = CType(GridView1.Rows(e.RowIndex).Cells(4).Controls(0), TextBox).Text
        Dim strdisplay As String = CType(GridView1.Rows(e.RowIndex).Cells(5).FindControl("listdisplay"), DropDownList).Text
	
	'Dim ddl as DropDownList = CType(row.FindControl("listdisplay", DropDownList)
        'Dim strdisplay as String = ddl.Text

	'Dim strdisplay as String = CType(GridView1.Rows(e.RowIndex).Cells(5).FindControl("listdisplay"), DropDownList)



	'Dim strdisplay As String = CType(GridView1.Rows(e.RowIndex).Cells(5).FindControl("listdisplay"), DropDownList).SelectedItem.Value
        Dim strmembers As String = CType(GridView1.Rows(e.RowIndex).Cells(6).Controls(0), TextBox).Text


        GridView1.EditIndex = -1
        BindGrid()
        Dim oDs As DataSet = GridView1.DataSource

        'Numbers are relative to the xml file.
        oDs.Tables(0).Rows(i).Item(0) = Strn
        oDs.Tables(0).Rows(i).Item(1) = Strtext
        oDs.Tables(0).Rows(i).Item(2) = Strurl
        oDs.Tables(0).Rows(i).Item(3) = Strdescr
        oDs.Tables(0).Rows(i).Item(4) = Strparent
        oDs.Tables(0).Rows(i).Item(5) = Strdisplay
        oDs.Tables(0).Rows(i).Item(6) = Strmembers

        oDs.WriteXml(Request.PhysicalApplicationPath + "cms\menusource.xml")
        BindGrid()

    End Sub




        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="10" AutoGenerateColumns="False" 
      OnDeleteCommand="articleList_DeleteFile">

            <Columns>
                 
                 <asp:BoundField HeaderText="N"  DataField="n" ></asp:BoundField>
                <asp:BoundField HeaderText="Link<br />Text" DataField="text" HtmlEncode="false"></asp:BoundField>
                <asp:BoundField HeaderText="Page URL<br /><div style='font-size:9px;'>(~\ indicates base of site)</div>" DataField="url" HtmlEncode="false"></asp:BoundField>
                <asp:BoundField HeaderText="Page<br />Description<br /><div style='font-size:9px;'>(Section 508)</div>" DataField="descr" HtmlEncode="false"></asp:BoundField>
                <asp:BoundField HeaderText="Page<br />Parent<br /><div style='font-size:9px;'>(Optional)</div>" DataField="parent" HtmlEncode="false"></asp:BoundField>


                 <asp:TemplateField  HeaderText="Display<br />Page?<br /><div style='font-size:9px;'>(Optional)</div>" >
                    <EditItemTemplate>
                        <ItemTemplate>
                            <asp:DropDownList ID="listdisplay" runat="server" SelectedValue='<%# Bind("display") %>'>
                                <asp:ListItem Text="True" Value="True"></asp:ListItem>
                                <asp:ListItem Text="False" Value="False"></asp:ListItem>
                            </asp:DropDownList>
                        </ItemTemplate>     
                    </EditItemTemplate>
                    <ItemTemplate>                

			<asp:BoundField HeaderText="Display<br />Page?<br /><div style='font-size:9px;'>(Optional)</div>" DataField="display" HtmlEncode="false"></asp:BoundField>

                   </ItemTemplate>
                </asp:TemplateField>




                <asp:BoundField HeaderText="Members<br />Only?<br /><div style='font-size:9px;'>(Optional)</div>" DataField="members" HtmlEncode="false"></asp:BoundField>




            	<asp:ButtonField CommandName="Test" Text="Add" HeaderText="Add" />
                <asp:CommandField ShowEditButton="True"  HeaderText="Edit" />
                <asp:CommandField ShowDeleteButton="True" HeaderText="Delete" />
            	<asp:ButtonField CommandName="Up" Text="Up" />
            	<asp:ButtonField CommandName="Dn" Text="Dn" />



            </Columns>
        </asp:GridView>

Open in new window

0
atljarman
Asked:
atljarman
  • 4
  • 2
2 Solutions
 
informaniacCommented:
Looks like you are binding the grid in the Page Load. And on clicking on save it is binding again.

Please add the below code in Page Load before binding the grid..

If Not IsPostBack Then
  Bind the grid
End If
0
 
atljarmanAuthor Commented:
Infomaniac, this is already in the code.

        If Page.IsPostBack = False Then
            BindGrid()
        End If

All code seems to be working except when trying to pull the value from the DropDownList.
0
 
hdesoukyCommented:
Hav you tried to read the dropdownlist SelectedValue Property instead of Text Property?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
atljarmanAuthor Commented:
I guess I didn't check the backend to see that the xml file was updating.  I've allowed paging to ten items.  I have BindGrid() at the bottom of the code which re-binds the xml file.  It doesn't show the value correctly. Should the value be displayed in a label or a textbox in the normal state (during non edit times.)
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating


        Dim i As Integer = GridView1.Rows(e.RowIndex).DataItemIndex

        Dim strn As String = CType(GridView1.Rows(e.RowIndex).Cells(0).Controls(0), TextBox).Text
        Dim strtext As String = CType(GridView1.Rows(e.RowIndex).Cells(1).Controls(0), TextBox).Text
        Dim strurl As String = CType(GridView1.Rows(e.RowIndex).Cells(2).Controls(0), TextBox).Text
        Dim strdescr As String = CType(GridView1.Rows(e.RowIndex).Cells(3).Controls(0), TextBox).Text
        Dim strparent As String = CType(GridView1.Rows(e.RowIndex).Cells(4).Controls(0), TextBox).Text
        'Dim strdisplay As String = CType(GridView1.Rows(e.RowIndex).Cells(5).FindControl("listdisplay"), DropDownList).Text
        Dim strdisplay As String = CType(GridView1.Rows(e.RowIndex).Cells(5).FindControl("listdisplay"), DropDownList).SelectedValue
	
	'Dim ddl as DropDownList = CType(row.FindControl("listdisplay", DropDownList)
        'Dim strdisplay as String = ddl.Text

	'Dim strdisplay as String = CType(GridView1.Rows(e.RowIndex).Cells(5).FindControl("listdisplay"), DropDownList)



	'Dim strdisplay As String = CType(GridView1.Rows(e.RowIndex).Cells(5).FindControl("listdisplay"), DropDownList).SelectedItem.Value
        Dim strmembers As String = CType(GridView1.Rows(e.RowIndex).Cells(6).Controls(0), TextBox).Text


        GridView1.EditIndex = -1
        BindGrid()
        Dim oDs As DataSet = GridView1.DataSource

        'Numbers are relative to the xml file.



        oDs.Tables(0).Rows(i).Item(0) = Strn
        oDs.Tables(0).Rows(i).Item(1) = Strtext
        oDs.Tables(0).Rows(i).Item(2) = Strurl
        oDs.Tables(0).Rows(i).Item(3) = Strdescr
        oDs.Tables(0).Rows(i).Item(4) = Strparent
        oDs.Tables(0).Rows(i).Item(5) = Strdisplay
        oDs.Tables(0).Rows(i).Item(6) = Strmembers


        oDs.WriteXml(Request.PhysicalApplicationPath + "cms\menusource.xml")
        BindGrid()

    End Sub




        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="10" AutoGenerateColumns="False" 
      OnDeleteCommand="articleList_DeleteFile">

            <Columns>
                 
                 <asp:BoundField HeaderText="N"  DataField="n" ></asp:BoundField>
                <asp:BoundField HeaderText="Link<br />Text" DataField="text" HtmlEncode="false"></asp:BoundField>
                <asp:BoundField HeaderText="Page URL<br /><div style='font-size:9px;'>(~\ indicates base of site)</div>" DataField="url" HtmlEncode="false"></asp:BoundField>
                <asp:BoundField HeaderText="Page<br />Description<br /><div style='font-size:9px;'>(Section 508)</div>" DataField="descr" HtmlEncode="false"></asp:BoundField>
                <asp:BoundField HeaderText="Page<br />Parent<br /><div style='font-size:9px;'>(Optional)</div>" DataField="parent" HtmlEncode="false"></asp:BoundField>


                 <asp:TemplateField  HeaderText="Display<br />Page?<br /><div style='font-size:9px;'>(Optional)</div>" >
                    <EditItemTemplate>
                        <ItemTemplate>
                            <asp:DropDownList ID="listdisplay" runat="server" SelectedValue='<%# Bind("display") %>'>
                                <asp:ListItem Text="True" Value="True"></asp:ListItem>
                                <asp:ListItem Text="False" Value="False"></asp:ListItem>
                            </asp:DropDownList>
                        </ItemTemplate>     
                    </EditItemTemplate>
                    <ItemTemplate>                

			<asp:BoundField HeaderText="Display<br />Page?<br /><div style='font-size:9px;'>(Optional)</div>" DataField="display" HtmlEncode="false"></asp:BoundField>

                   </ItemTemplate>
                </asp:TemplateField>




                <asp:BoundField HeaderText="Members<br />Only?<br /><div style='font-size:9px;'>(Optional)</div>" DataField="members" HtmlEncode="false"></asp:BoundField>




            	<asp:ButtonField CommandName="Test" Text="Add" HeaderText="Add" />
                <asp:CommandField ShowEditButton="True"  HeaderText="Edit" />
                <asp:CommandField ShowDeleteButton="True" HeaderText="Delete" />
            	<asp:ButtonField CommandName="Up" Text="Up" />
            	<asp:ButtonField CommandName="Dn" Text="Dn" />



            </Columns>
        </asp:GridView>

Open in new window

0
 
hdesoukyCommented:
If you bind the grid in this event, the update operation will not work
0
 
atljarmanAuthor Commented:
This is what I used and it works great.

Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating


        Dim i As Integer = GridView1.Rows(e.RowIndex).DataItemIndex


        'Dim strn As String = CType(GridView1.Rows(e.RowIndex).Cells(0).Controls(0), TextBox).Text
        'Dim strn As String = CType(GridView1.Rows(e.RowIndex).Cells(0).FindControl("N"), TextBox).Text

        Dim strtext As String = CType(GridView1.Rows(e.RowIndex).Cells(1).FindControl("text"), TextBox).Text
        Dim strurl As String = CType(GridView1.Rows(e.RowIndex).Cells(2).FindControl("url"), TextBox).Text
        Dim strdescr As String = CType(GridView1.Rows(e.RowIndex).Cells(3).FindControl("descr"), TextBox).Text
        Dim strparent As String = CType(GridView1.Rows(e.RowIndex).Cells(4).FindControl("parent"), TextBox).Text
        Dim strdisplay As String = CType(GridView1.Rows(e.RowIndex).Cells(5).FindControl("listdisplay"), DropDownList).SelectedValue
	Dim strmembers As String = CType(GridView1.Rows(e.RowIndex).Cells(6).FindControl("listmembers"), DropDownList).SelectedValue


        GridView1.EditIndex = -1
        BindGrid()
        Dim oDs As DataSet = GridView1.DataSource

        'Numbers are relative to the xml file.



        'oDs.Tables(0).Rows(i).Item(0) = Strn
        oDs.Tables(0).Rows(i).Item(1) = Strtext
        oDs.Tables(0).Rows(i).Item(2) = Strurl
        oDs.Tables(0).Rows(i).Item(3) = Strdescr
        oDs.Tables(0).Rows(i).Item(4) = Strparent
        oDs.Tables(0).Rows(i).Item(5) = Strdisplay
        oDs.Tables(0).Rows(i).Item(6) = Strmembers


        oDs.WriteXml(Request.PhysicalApplicationPath + "cms\menusource.xml")
        BindGrid()

    End Sub

Open in new window

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="100" AutoGenerateColumns="False" 
      OnDeleteCommand="articleList_DeleteFile">

            <Columns>
                 


                    <asp:TemplateField HeaderText="Row<br />Num">
		     <EditItemTemplate>

                            <asp:Label Runat="server" Text='<%# Bind("N") %>' ID="Label1"></asp:Label>

                     </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label Runat="server" Text='<%# Bind("N") %>' ID="Label1"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>


                    <asp:TemplateField HeaderText="Link<br />Text">
		     <EditItemTemplate>
                        <asp:TextBox ID="text" Runat="server" Text='<%# Bind("text") %>'></asp:TextBox>

                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" Runat="server"  ErrorMessage="You must provide a menu item link text.  This should be short."
                            ControlToValidate="text">*</asp:RequiredFieldValidator>

                     </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label Runat="server" Text='<%# Bind("text") %>' ID="Label2"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="Page URL<br /><div style='font-size:9px;'>(~\ indicates base of site)</div>">
		     <EditItemTemplate>
                        <asp:TextBox width="350px" ID="url" Runat="server" Text='<%# Bind("url") %>'></asp:TextBox>

                        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" Runat="server"  ErrorMessage="You must provide a menu url.  Example: ~\pagename.aspx  Note that the ~\ indicates the base of the site and the page name is the name of the file that you are referencing on the server."
                            ControlToValidate="url">*</asp:RequiredFieldValidator>

                     </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label Runat="server" Text='<%# Bind("url") %>' ID="Label3"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>



                    <asp:TemplateField HeaderText="Page<br />Description<br /><div style='font-size:9px;'>(Section 508)</div>">
		     <EditItemTemplate>

                        <asp:TextBox ID="descr" Runat="server" Text='<%# Bind("descr") %>'></asp:TextBox>

                        <asp:RequiredFieldValidator ID="RequiredFieldValidator4" Runat="server"  ErrorMessage="You must provide a menu item description."
                            ControlToValidate="descr">*</asp:RequiredFieldValidator>

                     </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label Runat="server" Text='<%# Bind("descr") %>' ID="Label4"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>


               



                    <asp:TemplateField HeaderText="Page<br />Parent<br /><div style='font-size:9px;'>(Optional)</div>">
		     <EditItemTemplate>
                        <asp:TextBox width="20px" ID="parent" Runat="server" Text='<%# Bind("parent") %>'></asp:TextBox>
                     </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label Runat="server" Text='<%# Bind("parent") %>' ID="Label8"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>




                 <asp:TemplateField  HeaderText="Display<br />Page?<br /><div style='font-size:9px;'>(Optional)</div>" >
                    <EditItemTemplate>
                        <ItemTemplate>
                            <asp:DropDownList ID="listdisplay" runat="server" SelectedValue='<%# Bind("display") %>'>
                                <asp:ListItem Text="True" Value=""></asp:ListItem>
                                <asp:ListItem Text="False" Value="False"></asp:ListItem>
                            </asp:DropDownList>
                        </ItemTemplate>     
                    </EditItemTemplate>
                    <ItemTemplate>                


  			<asp:Label Runat="server" Text='<%# Bind("display") %>'  ID="Label5"></asp:Label>

                   </ItemTemplate>
                </asp:TemplateField>


                 <asp:TemplateField  HeaderText="Members<br />Only?<br /><div style='font-size:9px;'>(Optional)</div>" >
                    <EditItemTemplate>
                        <ItemTemplate>
                            <asp:DropDownList ID="listmembers" runat="server" SelectedValue='<%# Bind("members") %>'>
                                <asp:ListItem Text="True" Value="True"></asp:ListItem>
                                <asp:ListItem Text="False" Value=""></asp:ListItem>
                            </asp:DropDownList>
                        </ItemTemplate>     
                    </EditItemTemplate>
                    <ItemTemplate>                


  			<asp:Label Runat="server" Text='<%# Bind("members") %>'  ID="Label6"></asp:Label>


                   </ItemTemplate>
                </asp:TemplateField>




                <asp:CommandField ShowEditButton="True"  HeaderText="Edit" />
            	<asp:ButtonField CommandName="Up" Text="Up" />
            	<asp:ButtonField CommandName="Dn" Text="Dn" />
                <asp:CommandField ShowDeleteButton="True" HeaderText="Delete" />
            	<asp:ButtonField CommandName="Test" Text="Add" HeaderText="Add" />



            </Columns>
        </asp:GridView>

Open in new window

0
 
atljarmanAuthor Commented:
I've accepted my own comment because hdesouky did identify the correct property to use.  However, he/she did not identify the correct coding in the gridview (which could not have been forseen).  In my last post, I included what I am currently using which is a little more complex than the initial question.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now