Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ASP.NET DataList problems

Posted on 2007-11-16
6
Medium Priority
?
1,752 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Loops Section Overview
Suggested Courses

972 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