Determining What Control Caused The PostBack

Discussion in 'ASP .Net Web Controls' started by Nathan Sokalski, Sep 5, 2005.

  1. I have a DataList that I was having trouble getting the events for. After a
    bit of help, I realized that I needed to put the databinding inside an If
    Not IsPostBack() condition. Although this fixed the individual problem, it
    caused problems in other areas. This is because the DataSource that I use
    for my databinding changes and must be "rebound". Because there is only one
    situation where I need the If Not IsPostBack() condition, I think it would
    be easiest if I could somehow determine what control caused the
    IsPostBack(). Is there any way to do this? Thanks.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/
     
    Nathan Sokalski, Sep 5, 2005
    #1
    1. Advertising

  2. Although I was already doing that, I think you may have solved my problem.
    Rather than calling the method from Page_Init where I should have, I was
    calling it from Page_Load. But now that I moved it to Page_Init, everything
    seems to be good. Thanks.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "Mark Rae" <> wrote in message
    news:...
    > "Nathan Sokalski" <> wrote in message
    > news:...
    >
    >> Is there any way to do this?

    >
    > The easiest way by far is to take your databinding code out of your
    > Page_Load and put it in a separate private method which you then call
    > every time you need to bind the data.
    >
     
    Nathan Sokalski, Sep 5, 2005
    #2
    1. Advertising

  3. Nathan,

    > Although this fixed the individual problem, it caused problems in other
    > areas. This is because the DataSource that I use for my databinding
    > changes and must be "rebound".


    Can you tell us why the above is, because in my idea it is better to solve
    the problem than to create spaghetti around simple methods that you did not
    implement in your program?

    Cor
     
    Cor Ligthert [MVP], Sep 5, 2005
    #3
  4. It is probably partially due to the fact that this is the first time I am
    using the inline editing technique of DataLists/DataGrids, but I think the
    reason is also because my page has three sections on it:

    1. Creating a new record in the database (this uses a simple textbox &
    button form)
    2. Editing an existing record (this is the one that is new to me)
    3. Delete an existing record (this is a dropdownlist & button)

    If I had done inline editing before, I probably would have combined the
    three parts into one DataList that included a Delete button as well as an
    Edit button, and had a button somewhere to create a blank record that could
    be edited. I did not do this this time because I was learning inline editing
    for the first time, self-taught, and I had to have the form finished for a
    temporary job, so I wanted to make sure the minimum of being able to add and
    delete worked, so I will admit that it's not the most efficient code right
    now. But now that I know the basics, I will probably be more efficient in
    the future.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "Cor Ligthert [MVP]" <> wrote in message
    news:...
    > Nathan,
    >
    >> Although this fixed the individual problem, it caused problems in other
    >> areas. This is because the DataSource that I use for my databinding
    >> changes and must be "rebound".

    >
    > Can you tell us why the above is, because in my idea it is better to solve
    > the problem than to create spaghetti around simple methods that you did
    > not implement in your program?
    >
    > Cor
    >
     
    Nathan Sokalski, Sep 5, 2005
    #4
  5. I am now having a related problem. I have my Edit button (the button from
    the ItemTemplate) working, but three of the controls in the EditTemplate (a
    Calendar control, a Button with CommandName="update", and a Button with
    CommandName="cancel"). Whenever I click on any of these controls the
    EditItem returns to being just an Item. I am assuming this is because the
    method I made to do the binding is somehow getting called. However, I can't
    figure out where it is getting called from, since my Page_Load does not
    include it (my Page_Load simply assigns a value to a Label's Text property).
    Any ideas? Here is my databinding code and my DataList events:

    Private Sub RefreshEvents()

    Dim events As New DataSet

    Dim myconnection As New
    OracleConnection(System.Configuration.ConfigurationSettings.AppSettings("connectionString"))

    Dim cmdselect As New OracleCommand("SELECT * FROM eventlist WHERE
    eventdate<TO_DATE('" & Date.Now.ToShortDateString() & "','MM/DD/YYYY')",
    myconnection)

    Dim cmddelete As New OracleCommand("", myconnection)

    Dim eventsadapter As New OracleDataAdapter(cmdselect)

    'Delete any past events and the people who registered for them

    eventsadapter.Fill(events, "eventlist")

    If events.Tables("eventlist").Rows.Count <> 0 Then

    For Each pastevent As DataRow In events.Tables("eventlist").Rows

    cmddelete.CommandText = "DELETE FROM registered WHERE eventid=" &
    pastevent.Item("eventid")

    myconnection.Open()

    cmddelete.ExecuteNonQuery()

    cmddelete.CommandText = "DELETE FROM eventlist WHERE eventid=" &
    pastevent.Item("eventid")

    cmddelete.ExecuteNonQuery()

    myconnection.Close()

    If pastevent.Item("details") <> "" AndAlso
    System.IO.File.Exists(Server.MapPath("eventdetails/" &
    pastevent.Item("details"))) Then
    System.IO.File.Delete(Server.MapPath("eventdetails/" &
    pastevent.Item("details")))

    Next

    End If

    'Fill DataSet with all remaining events

    events.Clear()

    cmdselect.CommandText = "SELECT * FROM eventlist ORDER BY eventdate"

    eventsadapter.SelectCommand = cmdselect

    eventsadapter.Fill(events, "eventlist")

    datEditEvents.DataSource = events

    datEditEvents.DataBind()

    ddlDeleteEvents.Items.Clear()

    For Each existevent As DataRow In events.Tables("eventlist").Rows

    ddlDeleteEvents.Items.Add(New
    ListItem(CDate(existevent("eventdate")).ToShortDateString() & " " &
    existevent("eventname"), existevent("eventid")))

    Next

    End Sub


    Private Sub datEditEvents_EditCommand(ByVal source As Object, ByVal e As
    System.Web.UI.WebControls.DataListCommandEventArgs) Handles
    datEditEvents.EditCommand

    datEditEvents.EditItemIndex = e.Item.ItemIndex

    Me.RefreshEvents()

    End Sub



    Private Sub datEditEvents_UpdateCommand(ByVal source As Object, ByVal e As
    System.Web.UI.WebControls.DataListCommandEventArgs) Handles
    datEditEvents.UpdateCommand

    If CType(e.Item.FindControl("calEditDate"), Calendar).SelectedDate >
    Date.Today() Then

    CType(e.Item.FindControl("lblDateError"), Label).Visible = False

    If CType(e.Item.FindControl("txtEditName"), TextBox).Text <> "" Then

    CType(e.Item.FindControl("lblNameError"), Label).Visible = False

    CType(e.Item.FindControl("revEditDesc"),
    RegularExpressionValidator).Validate()

    If CType(e.Item.FindControl("revEditDesc"),
    RegularExpressionValidator).IsValid Then

    Dim myconnection As New
    OracleConnection(System.Configuration.ConfigurationSettings.AppSettings("connectionString"))

    Dim cmdupdate As New OracleCommand("UPDATE eventlist SET eventname='" &
    CType(e.Item.FindControl("txtEditName"), TextBox).Text.Replace("'", "''") &
    "',description='" & CType(e.Item.FindControl("txtEditDesc"),
    TextBox).Text.Replace("'", "''") & "',eventdate=TO_DATE('" &
    CType(e.Item.FindControl("calEditDate"),
    Calendar).SelectedDate.ToShortDateString() & "','MM/DD/YYYY')",
    myconnection)

    If CType(e.Item.FindControl("radNoDetails"), RadioButton).Checked Then

    'Delete existing file

    If e.CommandArgument <> "" AndAlso
    System.IO.File.Exists(Server.MapPath("eventdetails/" & e.CommandArgument))
    Then System.IO.File.Delete(Server.MapPath("eventdetails/" &
    e.CommandArgument))

    'Set details=''

    cmdupdate.CommandText &= ",details=''"

    End If

    If CType(e.Item.FindControl("radNewDetails"), RadioButton).Checked Then

    'Delete existing file

    If e.CommandArgument <> "" AndAlso
    System.IO.File.Exists(Server.MapPath("eventdetails/" & e.CommandArgument))
    Then System.IO.File.Delete(Server.MapPath("eventdetails/" &
    e.CommandArgument))

    'Upload new file

    Dim upfilename As String = ""

    If fileDetails.Value <> "" AndAlso fileDetails.PostedFile.ContentLength > 0
    Then

    Dim dir As String() =
    fileDetails.PostedFile.FileName.Split("\".ToCharArray())

    upfilename = dir(dir.GetUpperBound(0))

    fileDetails.PostedFile.SaveAs(Server.MapPath("eventdetails/" & upfilename))

    End If

    'Set details to new filename

    cmdupdate.CommandText &= ",details='" & upfilename & "'"

    End If

    cmdupdate.CommandText &= " WHERE eventid=" &
    datEditEvents.DataKeys(e.Item.ItemIndex)

    myconnection.Open()

    cmdupdate.ExecuteNonQuery()

    myconnection.Close()

    datEditEvents.EditItemIndex = -1

    Me.RefreshEvents()

    End If

    Else

    CType(e.Item.FindControl("lblNameError"), Label).Visible = True

    End If

    Else

    CType(e.Item.FindControl("lblDateError"), Label).Visible = True

    End If

    End Sub



    Private Sub datEditEvents_CancelCommand(ByVal source As Object, ByVal e As
    System.Web.UI.WebControls.DataListCommandEventArgs) Handles
    datEditEvents.CancelCommand

    datEditEvents.EditItemIndex = -1

    Me.RefreshEvents()

    End Sub



    Thank you so much.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/
    "Mark Rae" <> wrote in message
    news:...
    > "Nathan Sokalski" <> wrote in message
    > news:...
    >
    >> Is there any way to do this?

    >
    > The easiest way by far is to take your databinding code out of your
    > Page_Load and put it in a separate private method which you then call
    > every time you need to bind the data.
    >
     
    Nathan Sokalski, Sep 5, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RTM
    Replies:
    4
    Views:
    2,775
    mikeb
    Jan 20, 2004
  2. Nathan Sokalski

    Determining What Control Caused The PostBack

    Nathan Sokalski, Sep 5, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    942
    Nathan Sokalski
    Sep 5, 2005
  3. =?Utf-8?B?ZGNoaWxsbWFu?=

    Determining control that caused a postback

    =?Utf-8?B?ZGNoaWxsbWFu?=, Oct 18, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    4,360
    =?Utf-8?B?ZGNoaWxsbWFu?=
    Oct 19, 2005
  4. Nathan Sokalski

    Determining What Control Caused The PostBack

    Nathan Sokalski, Sep 5, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    4
    Views:
    144
    Nathan Sokalski
    Sep 5, 2005
  5. matthias s

    determining what control caused the click event

    matthias s, Sep 19, 2007, in forum: ASP .Net Web Controls
    Replies:
    5
    Views:
    186
    David R. Longnecker
    Sep 20, 2007
Loading...

Share This Page