9 Replies - 3609 Views - Last Post: 15 September 2010 - 01:09 PM Rate Topic: -----

#1 eTech1  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 135
  • Joined: 27-August 10

Does OnSelectedIndexChanged Require a Postback?

Posted 15 September 2010 - 09:12 AM

I have a dropdownlist with values active, completed and canceled. on page_load I change the value based on data in my database. onselectedIndexChanged I would like to UPDATE the database with the new value.

To do this I set onselectedIndexChanged="editStatus" and my editStatus function is just the running of the update query. When I select something else in the dropdownlist my function isn't running. I tested this by both printing something to the screen and changing the value of a textbox.

If I add autoPostBack = "true" then the function runs but my status is reloaded from the database because of the on page_load and so nothing is updated.

I could figure out a way to do this all in the page_load by testing for autoPostBack but shouldn't I be able to call this other function without an autoPostBack?

Is This A Good Question/Topic? 0
  • +

Replies To: Does OnSelectedIndexChanged Require a Postback?

#2 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 636
  • Joined: 03-February 10

Re: Does OnSelectedIndexChanged Require a Postback?

Posted 15 September 2010 - 10:44 AM

From what I can tell, since you didn't post code, is that in your Page_Load event you must not be checking for the postback. Normally the Page_Load event is done something like this.


protected void Page_Load(object sender, EventArgs e)
{
     if(!IsPostBack)
     {
         //here is where you would put code that you want to load the first time 
         // because when the page postsback most server controls will have stored their
         // values in ViewState.
         // If you do not check for the postback and just fill values on your form from the 
         // Page_Load, then realize that every time the page posts back it will reset the page 
         // as though it was being loaded the first time.

     }//end check for PostBack


}


Was This Post Helpful? 1
  • +
  • -

#3 eTech1  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 135
  • Joined: 27-August 10

Re: Does OnSelectedIndexChanged Require a Postback?

Posted 15 September 2010 - 11:17 AM

Okay I've added that to my Page_Load and put autoPostBack="true" in my dropDownList again. Now the sub is running but my SQL UPDATE is not working. I'm using VB and an Access database by the way.

code behind
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
            If Not IsPostBack Then
                Dim Con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data source=...")
                Con.Open()
                Dim Sql As String = "SELECT TOP 1 * FROM userNotes WHERE noteID = @noteID;"
                Dim Cmd As New OleDbCommand(Sql, Con)
                Cmd.Parameters.AddWithValue("@noteID", Request.QueryString("noteID"))
                Dim Reader As OleDbDataReader
                Reader = Cmd.ExecuteReader()
                If Reader.Read() Then
                    Dim cBDate = Reader.Item("callBack").ToString
                    cBDate = cBDate.Substring(0, cBDate.IndexOf(" "))
                    message.Text = Reader.Item("message")
                    callBack.Text = cBDate
                    status.SelectedValue = Reader.Item("callBackStatus")
                End If
                Con.Close()
            End If
        End Sub
        Sub editStatus(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim Con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data source=...")
            Con.Open()
            Dim Sql As String = "UPDATE userNotes SET callBackStatus = @callBackStatus WHERE noteID = @noteID;"
            Dim Cmd As New OleDbCommand(Sql, Con)
            Cmd.Parameters.AddWithValue("@noteID", Request.QueryString("noteID"))
            Cmd.Parameters.AddWithValue("@callBackStatus", status.SelectedValue)
            Cmd.ExecuteNonQuery()
            Con.Close()
        End Sub


<table align="center" style="width: 400px; color:#084B8A; border-spacing: 0px; border: 1px solid #084B8A;">
                <tr style="background-color: #EAEAFF;">
                    <td style="padding: 15px; border-right: 1px solid #084B8A;">
                        Callback
                    </td>
                    <td style="padding: 15px;">
                        <asp:Label runat="server" ID="callBack" Text=""></asp:Label>
                    </td>
                </tr>
                <tr style="background-color: #FFFFFF;">
                    <td style="padding: 15px; border-right: 1px solid #084B8A;">
                        Message
                    </td>
                    <td style="padding: 15px;">
                        <asp:Label runat="server" ID="message" Text=""></asp:Label>
                    </td>
                </tr>
                <tr style="background-color: #EAEAFF;">
                    <td style="padding: 15px; border-right: 1px solid #084B8A;">
                        Status
                    </td>
                    <td style="padding: 15px;">
                        <asp:DropDownList runat="server" ID="status" onselectedIndexChanged="editStatus" AutoPostBack="true">
                            <asp:ListItem Text="Canceled" Value="Canceled"></asp:ListItem>
                            <asp:ListItem Text="Active" Value="Active"></asp:ListItem>
                            <asp:ListItem Text="Completed" Value="Completed"></asp:ListItem>
                        </asp:DropDownList>
                    </td>
                </tr>
            </table>

Was This Post Helpful? 0
  • +
  • -

#4 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 636
  • Joined: 03-February 10

Re: Does OnSelectedIndexChanged Require a Postback?

Posted 15 September 2010 - 11:24 AM

When you say it is not working, how is it not working? error? not the correct result?

Also if you could put a breakpoint on the first Cmd.Parameters of the editStatus method, can you verify that the parameter values are getting set correctly.

Otherwise everything looks good.
Was This Post Helpful? 0
  • +
  • -

#5 eTech1  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 135
  • Joined: 27-August 10

Re: Does OnSelectedIndexChanged Require a Postback?

Posted 15 September 2010 - 11:42 AM

I know the SQL is not running(or at least not properly) because I changed
Dim Sql As String = "UPDATE userNotes SET callBackStatus = @callBackStatus WHERE noteID = @noteID;"
To
Dim Sql As String = "UPDATE userNotes SET message = 'hi', callBackStatus = @callBackStatus WHERE noteID = @noteID;"

And the message also did not change in the database.

I haven't used the debugger before but I've set my breakpoint and tried Debug -> Start Debugging and it takes a few seconds and then gives me a build error saying my footer and header controls don't exist so it can't debug. That doesn't make sense though because they load properly in every page.

This post has been edited by eTech1: 15 September 2010 - 11:43 AM

Was This Post Helpful? 0
  • +
  • -

#6 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 636
  • Joined: 03-February 10

Re: Does OnSelectedIndexChanged Require a Postback?

Posted 15 September 2010 - 11:55 AM

Try this then, comment out everything in editStatus and replace it with

Response.Write(Request.QueryString["noteID"])
Response.Write(status.SelectedValue)



Not my preferred way, but is quick to switch to when you can't get the debugger going right away :)

I have run into header / footer problems before also. In my case it usually had to do with either a DataList or Repeater.
Was This Post Helpful? 0
  • +
  • -

#7 eTech1  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 135
  • Joined: 27-August 10

Re: Does OnSelectedIndexChanged Require a Postback?

Posted 15 September 2010 - 12:06 PM

Both are displaying correctly.. hmmmm
Was This Post Helpful? 0
  • +
  • -

#8 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 636
  • Joined: 03-February 10

Re: Does OnSelectedIndexChanged Require a Postback?

Posted 15 September 2010 - 12:18 PM

According to this article on Parameterized Queries in Access the order of the parameters in very important. They need to be added to the command object in the same order they appear in the SQL statement.
Was This Post Helpful? 1
  • +
  • -

#9 eTech1  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 135
  • Joined: 27-August 10

Re: Does OnSelectedIndexChanged Require a Postback?

Posted 15 September 2010 - 12:23 PM

Baah!!! I absolutely knew that lol. That just goes to show what staring at a code for too long will get you. Thank you very much!
Was This Post Helpful? 0
  • +
  • -

#10 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 636
  • Joined: 03-February 10

Re: Does OnSelectedIndexChanged Require a Postback?

Posted 15 September 2010 - 01:09 PM

No problem... have done it myself many times.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1