Solved

ASP.NET DataList problems

Posted on 2007-11-16
6
1,738 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

746 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

10 Experts available now in Live!

Get 1:1 Help Now