Determining What Control Caused The PostBack

  • Thread starter Nathan Sokalski
  • Start date
N

Nathan Sokalski

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.
 
M

Mark Rae

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.
 
N

Nathan Sokalski

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.
 
C

Cor Ligthert [MVP]

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
 
N

Nathan Sokalski

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.
 
N

Nathan Sokalski

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top