Solved

ASP.NET DataList problems

Posted on 2007-11-16
6
1,748 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

624 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