Link to home
Start Free TrialLog in
Avatar of atljarman
atljarman

asked on

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

Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

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
Avatar of atljarman
atljarman

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of hdesouky
hdesouky

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

If you bind the grid in this event, the update operation will not work
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.