Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Determining What Control Caused The PostBack

Reply
Thread Tools

Determining What Control Caused The PostBack

 
 
Nathan Sokalski
Guest
Posts: n/a
 
      09-05-2005
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.velocityreviews.com/forums/(E-Mail Removed)
http://www.nathansokalski.com/


 
Reply With Quote
 
 
 
 
Mark Rae
Guest
Posts: n/a
 
      09-05-2005
"Nathan Sokalski" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

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


 
Reply With Quote
 
 
 
 
Nathan Sokalski
Guest
Posts: n/a
 
      09-05-2005
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
(E-Mail Removed)
http://www.nathansokalski.com/

"Mark Rae" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Nathan Sokalski" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>> 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.
>



 
Reply With Quote
 
Cor Ligthert [MVP]
Guest
Posts: n/a
 
      09-05-2005
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


 
Reply With Quote
 
Nathan Sokalski
Guest
Posts: n/a
 
      09-05-2005
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
(E-Mail Removed)
http://www.nathansokalski.com/

"Cor Ligthert [MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
Nathan Sokalski
Guest
Posts: n/a
 
      09-05-2005
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.Configuratio nSettings.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")).ToShortDat eString() & " " &
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.Configuratio nSettings.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("\".ToCharAr ray())

upfilename = dir(dir.GetUpperBound(0))

fileDetails.PostedFile.SaveAs(Server.MapPath("even tdetails/" & 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
(E-Mail Removed)
http://www.nathansokalski.com/
"Mark Rae" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Nathan Sokalski" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>> 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.
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
determining what control caused the click event matthias s ASP .Net Web Controls 5 09-20-2007 12:11 PM
Determining control that caused a postback =?Utf-8?B?ZGNoaWxsbWFu?= ASP .Net 4 10-19-2005 12:28 PM
Determining What Control Caused The PostBack Nathan Sokalski ASP .Net Web Controls 4 09-05-2005 10:42 PM
Determining What Control Caused The PostBack Nathan Sokalski ASP .Net Datagrid Control 4 09-05-2005 10:42 PM
Determining if a textbox control caused a postback RTM ASP .Net 4 01-20-2004 05:49 PM



Advertisments