Solved

ASP.NET DataList problems

Posted on 2007-11-16
6
1,739 Views
Last Modified: 2013-11-26
I'm using a DataList to display a summary of data and Select, Edit, Delete buttons to action the items.

The datalist is bound to a SQLDataSource and when the 'View' button it fires OnSelectedIndexChanged...It stores the ID and action in session variables and redirects to the page that shows the details.

It works flawlessly on my workstation...I cannot cause errors.  But, on the server there are times where it seems to loose track of the session variables.  I can refresh the Details page and it refreshes to the correct values...But, if I refresh again it reverts back.  And, again...it could refresh to the correct value or revert back...it seems almost randon.  Back, Forth, Back, Back, Forth, Back, Forth, Forth, Forth, Back.

One method for trying to correct the situation has created the condition for me to be able to test it.  If I abandon the session and refresh <F5> the Summary page, any further attempts will be mucked up.

I'm imagining that there is something wrong in the page life cycle with how I've done it.  But, I cannot see what it is.  

Here is the code
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" Title="Untitled Page" %>

<%@ Import Namespace="System.IO" %>

<%@ Import Namespace = "System.Data" %>

<%@ Import Namespace = "System.Data.SqlClient" %>

<%@ Import Namespace="PharmPro" %>

  

  <script runat="server">

  
 

    

  Const SELECT_CONTINUOUS_IMPROVEMENTS As String = "SELECT * FROM MyTable"
 

    

  Protected Sub Page_Load(ByVal s As Object, ByVal e As EventArgs) Handles MyBase.Load

    

  End Sub

  

    Protected Sub DataList1_Select(ByVal sender As Object, ByVal e As System.EventArgs)

      Session("ContinuousImprovementID") = DataList1.DataKeys(DataList1.SelectedItem.ItemIndex)

      Session("ContinuousImprovementAction") = "view"

      RedirectPage()

    End Sub

    

    Protected Sub DataList1_Delete(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs)

      Session("ContinuousImprovementID") = DataList1.DataKeys(e.Item.ItemIndex).ToString

      Session("ContinuousImprovementAction") = "remove"

      RedirectPage()

    End Sub

    

    Protected Sub DataList1_Edit(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs)

      Session("ContinuousImprovementID") = DataList1.DataKeys(e.Item.ItemIndex).ToString

      Session("ContinuousImprovementAction") = "edit"

      RedirectPage()

    End Sub

     

    Protected Sub btnAbandon_OnClick(ByVal sender As Object, ByVal e As System.EventArgs)

      Session.Abandon()

      Response.Cookies.Add(New HttpCookie("ASP.NET_SessionId", ""))

    End Sub

     

    Protected Sub btnRedirect_Click(ByVal sender As Object, ByVal e As System.EventArgs)

      RedirectPage()

    End Sub
 

    Protected Sub RedirectPage()

      Response.Redirect("TestDataListDetails.aspx")

    End Sub

  

</script>
 

<asp:Content ID="Content1" ContentPlaceHolderID="rightheader" Runat="Server">

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="links" Runat="Server">

</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="main" Runat="Server">
 

<asp:Button ID="btnRedirect" runat="server" Text="Redirect" OnClick="btnRedirect_Click" />

<asp:Button ID="btnAbandon" runat="server" Text="Abandon Session" OnClick="btnAbandon_OnClick" />

<asp:Label ID="lblID" runat="server" Text=""></asp:Label>

  <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DATA_01ConnectionString %>"

    SelectCommand="SELECT * FROM MyTable">

  </asp:SqlDataSource>

<asp:Table ID="tblAll" runat="server">

  <asp:TableRow>

    <asp:TableCell>

          

      <asp:DataList ID="DataList1" runat="server" OnDeleteCommand="DataList1_Delete" OnEditCommand="DataList1_Edit" OnSelectedIndexChanged="DataList1_Select"  DataKeyField="ContinuousImprovementID" DataSourceID="SqlDataSource1">

        <SeparatorTemplate><div style="height:4px"></div>

        </SeparatorTemplate>

        <HeaderTemplate>

        </HeaderTemplate>

        <ItemTemplate >

          <div style="border-style:ridge;border-width:thin;padding:4px">

            <span style="font-size:medium">

              <asp:Label id="lblTitle" runat="server" Text='<%# Bind("Title") %>'/>

            </span>

            <asp:Panel ID="pnlContinuousImprovement" runat="server" Visible="false">

                <asp:Table ID="tblContinuousImprovement" runat="server"  Height="10">

                  <asp:TableRow >

                    <asp:TableCell >

                      <asp:Label ID="lblContinuousImprovement" runat="server" Height="100" Text='<%# Bind("Problem") %>'></asp:Label>

                    </asp:TableCell>

                  </asp:TableRow>

                </asp:Table>

            </asp:Panel>

            &nbsp;<asp:LinkButton  ID="lbDotDotDot" runat="server" Font-Size="X-Large"  Font-Underline="false"  Visible="false" PostBackUrl="~/CI/ContinuousImprovement.aspx" Text="..."/><br />

            <div>

              <asp:Panel ID="pnlSolution" runat="server" >   </asp:Panel>       

              <div style="height:8px;width:245px;border-bottom-style:solid; border-bottom-width:1px">

              </div>

              Department:

              <asp:Label ID="lblDepartment" runat="server" Text='<%# Bind("Department") %>'/>

              <asp:Label ID="lblManagerID" runat="server" Visible="false" Text='<%# Bind("ManagerID") %>'/>

              <br /> 

              Priority:

              <asp:Label ID="lblPriority" runat="server"  Text='<%# Bind("Priority") %>'/>

              <br />    

              Status:

              <asp:Label ID="lblStatus" runat="server"  Text='<%# Bind("Status") %>'/>

              <br />    

              Assigned To:

              <asp:Label ID="lblAssignedToName" runat="server" Text='<%# Bind("AssignedToName") %>'/>

              <asp:Label ID="lblAssignedTo" runat="server" Visible="false" Text='<%# Bind("AssignedTo") %>'/>

              <br />            

              Created:

              <asp:Label ID="lblCreateDate" runat="server" Text='<%# Bind("CreateDate") %>'></asp:Label>&nbsp;&nbsp;By:&nbsp;<asp:Label ID="lblCreateUserName" runat="server" Text='<%# Bind("CreateUserName") %>'/>

              <asp:Label ID="lblCreateUser" runat="server" Visible="false" Text='<%# Bind("CreateUser") %>'/>

              <br />

              <asp:Panel ID="pnlButtons" runat="server">

              <asp:Table ID="tblButtons" runat="server">

              <asp:TableRow>

              <asp:TableCell>

                <asp:Panel ID="pnlViewButton" runat="server" BorderColor="#82B4E8" BorderStyle="Solid" BorderWidth="1px" BackImageUrl="~/Images/bg-menu-blue.png"  Height="20px" width="50px">          

                  <asp:LinkButton ID="btnView" runat="server" Font-Bold="true"  CssClass="roundedLinkButton" CommandName="Select" CommandArgument="Select" ForeColor="white" Font-Underline="false" Text="View" width="100%" Height="100%" />

                </asp:Panel> 

              </asp:TableCell>

              <asp:TableCell>

                <asp:Panel ID="pnlEditButton" runat="server" Visible="false" BorderColor="#82B4E8" BorderStyle="Solid" BorderWidth="1px" BackImageUrl="~/Images/bg-menu-blue.png"  Height="20px" width="40px">          

                  <asp:LinkButton ID="btnEdit" runat="server" Font-Bold="true"  CssClass="roundedLinkButton" CommandName="Edit" CommandArgument="Edit" ForeColor="white" Font-Underline="false" Text="Edit" width="100%" Height="100%" />

                </asp:Panel>

              </asp:TableCell>

              <asp:TableCell>

                <asp:Panel ID="pnlRemoveButton" runat="server" Visible="false" BorderColor="#82B4E8" BorderStyle="Solid" BorderWidth="1px" BackImageUrl="~/Images/bg-menu-blue.png"  Height="20px" width="70px">          

                  <asp:LinkButton ID="btnRemove" runat="server" Font-Bold="true"  CssClass="roundedLinkButton" CommandName="Delete" CommandArgument="Delete" OnClientClick="return confirm('Are you sure?');" ForeColor="white" Font-Underline="false" Text="Remove" width="100%" Height="100%" />

                </asp:Panel>                 

              </asp:TableCell>

              </asp:TableRow>

              </asp:Table>

             </asp:Panel>

            </div>

          </div>

        </ItemTemplate>

      </asp:DataList>

      &nbsp;

      <br />

    </asp:TableCell>

  </asp:TableRow>

</asp:Table>

</asp:Content>

<asp:Content ID="Content4" ContentPlaceHolderID="leftfooter" Runat="Server">

</asp:Content>

<asp:Content ID="Content5" ContentPlaceHolderID="rightfooter" Runat="Server">

</asp:Content>

Open in new window

0
Comment
Question by:ast2550
  • 3
  • 3
6 Comments
 
LVL 33

Expert Comment

by:raterus
ID: 20301898
Looks to me like you are storing the DataKey in the session (an object reference), not the value of the DataKey.  I could see how over a postback this would go null since it's an object reference.

Is your datakey field an integer?  Try casing it before you assign it in the session

Try this in your code,
Session("ContinuousImprovementID") = cint(DataList1.DataKeys(DataList1.SelectedItem.ItemIndex))
0
 
LVL 7

Author Comment

by:ast2550
ID: 20302039
No better.  It works 90% of the time.  But, once it starts failing its a goner until I close the browser and re-open.
0
 
LVL 33

Accepted Solution

by:
raterus earned 500 total points
ID: 20304090
I don't think you really need the session here, have you tried to completely eliminate it here, in favor of querystring values?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 7

Author Comment

by:ast2550
ID: 20312251
No, I guess I can go down that road.  But, why can't I use session variables?  Is there a problem with the IIS Server?
0
 
LVL 33

Expert Comment

by:raterus
ID: 20312284
It's really just a personal preference of mine.  Sessions expire, or get overwritten, or something else unexpected.  I tend to favor more concrete solutions that don't leave my application in a half-functional state.  I rarely rarely use sessions these days.
0
 
LVL 7

Author Closing Comment

by:ast2550
ID: 31409658
Sorry for not closing it out.  Thanks.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now