locked
Form View Cancel button setting in vb.net RRS feed

  • Question

  • User-1235671040 posted

    Dear all

    Hope you all fine,

    am trying to set a customized function when press cancel button in form view so am writing the following function:

    Protected Sub FormView1_ItemCommand(sender As Object, e As FormViewCommandEventArgs) Handles FormView1.ItemCommand

    If e.CommandName = "Cancel" Then
    FormView1.DataBind()

    'Setting DiscDDLInsert  Drop Down list to certain value 
    Dim DiscDDLInsert As DropDownList = CType(FormView1.FindControl("DiscNewEditDDL"), DropDownList)
    DiscDDLInsert.SelectedValue = DiscDDLInsert.Items.FindByText(DiscDDL.SelectedItem.Text).Value
    DiscDDLInsert.Enabled = False

    'Setting  blockWeek  label to certain value
    Dim blockWeek As Label = CType(FormView1.FindControl("BlockWeeklbl"), Label)
    blockWeek.Text = BlockDDL.Text & " - Week " & WeekNolbl.Text

    End If


    End Sub

    The problem is when I press the cancel button it's not giving me the setting values but it gives me always the default values for DiscDDLInsert  Drop Down list always gives me the first value and for the blockWeek  label always gives me empty value .So any ideas to set the customized values rather than the default values.

    Regards

    Wednesday, December 4, 2019 10:40 AM

All replies

  • User-775646050 posted

    Why are you calling your databind method in the cancel event? Based on your code, I'm not sure what you're trying to do. Perhaps you could also include the .aspx page.

    Wednesday, December 4, 2019 7:27 PM
  • User-719153870 posted

    Hi engzezo,

    Tried to reproduce the problem but failed. During the process i met two errors.

    One is 'FormView1' fired event ModeChanging which wasn't handled. which solved by added an OnModeChanging event to FormView1;

    Another is Object reference not set to an instance of an object. which solved by removed the FormView1.DataBind() in your code.

    To help reproduce the problem, we will need more information such as your aspx code and if necessary more of your related behind code.

    Below is the demo:

    aspx:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:FormView ID="FormView1" runat="server" OnItemCommand="FormView1_ItemCommand" OnModeChanging="FormView1_ModeChanging">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%#Eval("FirstName") %>'></asp:Label>
                        <asp:Label ID="BlockWeeklbl" runat="server" Text="Label"></asp:Label>
                        <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Cancel">Cancel</asp:LinkButton> 
                    </ItemTemplate>
                </asp:FormView>
            </div>
        </form>
    </body>
    </html>

    cs:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                BindData()
            End If
        End Sub
    
        Protected Sub BindData()
            Dim dt As DataTable = New DataTable()
            dt.Columns.AddRange(New DataColumn(4) {New DataColumn("FirstName"), New DataColumn("LastName"), New DataColumn("ID"), New DataColumn("Phone1"), New DataColumn("username")})
            dt.Rows.Add("AA", "aa", "1", "000", "Aa")
            dt.Rows.Add("BB", "bb", "2", "000", "Bb")
            dt.Rows.Add("CC", "cc", "3", "000", "Cc")
            dt.Rows.Add("DD", "dd", "4", "000", "Dd")
            dt.Rows.Add("EE", "ee", "5", "000", "Ee")
            dt.Rows.Add("FF", "ff", "6", "000", "Ff")
            dt.Rows.Add("GG", "gg", "7", "000", "Gg")
            FormView1.DataSource = dt
            FormView1.DataBind()
        End Sub
    
        Protected Sub FormView1_ItemCommand(sender As Object, e As FormViewCommandEventArgs)
            If e.CommandName = "Cancel" Then
                'FormView1.DataBind()
    
                'Setting DiscDDLInsert  Drop Down list to certain value 
                'Dim DiscDDLInsert As DropDownList = CType(FormView1.FindControl("DiscNewEditDDL"), DropDownList)
                'DiscDDLInsert.SelectedValue = DiscDDLInsert.Items.FindByText(DiscDDL.SelectedItem.Text).Value
                'DiscDDLInsert.Enabled = False
    
                'Setting  blockWeek  label to certain value
                Dim blockWeek As Label = CType(FormView1.FindControl("BlockWeeklbl"), Label)
                blockWeek.Text = "BlockDDL"
    
            End If
        End Sub
    
        Protected Sub FormView1_ModeChanging(sender As Object, e As FormViewModeEventArgs)
    
        End Sub

    Below is the result of this demo:

    Best Regard,

    Yang Shen

    Thursday, December 5, 2019 2:18 AM