locked
ASP .NET FormView Question RRS feed

  • Question

  • User238457982 posted

    Hello, I have a question. Please see the attached YouTube video that gives the demonstration of my problem.

    http://www.youtube.com/watch?v=3D5kkHEhjmg

    The project:

    I have attached a comment table in Microsoft's personal starter site database. In details.aspx, i can enter comment on the photo that gets displayed. However, after I click submit button, the page goes back to the previously viewed photo that I have selected with the pager setting.

    What should I do so that after I hit submit button, the page loads and stays in the same page and does not switches to next/prev photo?

    The details.aspx, photos.aspx are attached.

    Deatils.aspx
    .......................
    <%@	Page Language="C#" MasterPageFile="~/Default.master" Title="Your Name Here | Picture Details"
    	CodeFile="Details.aspx.cs" Inherits="Details_aspx" %>
    
    <asp:content id="Content1" contentplaceholderid="Main" runat="server">
    <script runat="server">
       
         void FormView_ItemCreated(Object sender, EventArgs e)  
       {  
         Label2.Text = FormView1.SelectedValue.ToString();  
      }
        
        
        
        </script>
    	<div class="shim solid"></div>
    	
    	<div class="page" id="details">
         <asp:Label ID="Label1" runat="server" ></asp:Label>
          <asp:Label ID="Label2" runat="server" ></asp:Label><br /><br />
    		<asp:formview id="FormView1" runat="server"	datasourceid="ObjectDataSource1"  
            onitemcreated="FormView_ItemCreated" 
            cssclass="view"  DataKeyNames="PhotoID"
    			borderstyle="none" borderwidth="0" CellPadding="0" cellspacing="0" 
                EnableViewState="false" AllowPaging="true" 
                >
    			<itemtemplate>
    			
    				<div class="buttonbar buttonbar-top">
    					<a href="Albums.aspx"><asp:image ID="Image1" runat="Server"	 skinid="gallery" /></a>
    					    
    					<asp:ImageButton ID="ImageButton9" Runat="server" CommandName="Page" CommandArgument="First" skinid="first"/>
    					<asp:ImageButton ID="ImageButton10"	Runat="server" CommandName="Page" CommandArgument="Prev" skinid="prev"/>
    					<asp:ImageButton ID="ImageButton11"	Runat="server" CommandName="Page" CommandArgument="Next" skinid="next"/>
    					<asp:ImageButton ID="ImageButton12"	Runat="server" CommandName="Page" CommandArgument="Last" skinid="last"/>
    				</div>
    				<p><%# Server.HtmlEncode(Eval("Caption").ToString()) %></p>
    				<table border="0" cellpadding="0" cellspacing="0" class="photo-frame">
    					<tr>
    						<td class="topx--"></td>
    						<td class="top-x-"></td>
    						<td class="top--x"></td>
    					</tr>
    					<tr>
    						<td class="midx--"></td>
    						<td><img src="Handler.ashx?PhotoID=<%# Eval("PhotoID") %>&Size=S" class="photo_198" 
                            style="border:4px solid white" alt='Photo Number <%# Eval("PhotoID") %>' /></a></td>
    						<td class="mid--x"></td>
    					</tr>
    					<tr>
    						<td class="botx--"></td>
    						<td class="bot-x-"></td>
    						<td class="bot--x"></td>
    					</tr>
    				</table>
    				<p><a href='Download.aspx?AlbumID=<%# Eval("AlbumID") %>&Page=<%# Container.DataItemIndex %>'>
    					<asp:image runat="Server" id="DownloadButton" AlternateText="download this photo" skinid="download" /></a></p>
    				<div class="buttonbar">
    					<a href="Albums.aspx"><asp:image ID="Image2" runat="Server"	 skinid="gallery" /></a>
    					    
    					<asp:ImageButton ID="ImageButton1" Runat="server" CommandName="Page" CommandArgument="First" skinid="first"/>
    					<asp:ImageButton ID="ImageButton2" Runat="server" CommandName="Page" CommandArgument="Prev" skinid="prev"/>
    					<asp:ImageButton ID="ImageButton3" Runat="server" CommandName="Page" CommandArgument="Next" skinid="next"/>
    					<asp:ImageButton ID="ImageButton4" Runat="server" CommandName="Page" CommandArgument="Last" skinid="last"/>
                        <br />
                          <br />
                    
    				</div>
           
                   <div id="Div1" style="text-align:left; padding-left:5px; padding-top:5px; padding-bottom:20px;">
                  <asp:Label ID="UserComment" runat="server" Text="Submit Your Comment" Font-Bold="true" Font-Size="Large"></asp:Label><br />
                  <table><tr><td> <asp:TextBox ID="TextBox1" runat="server" Width="350px" Height="80px"></asp:TextBox></td>
                  <td>             
                      <asp:RegularExpressionValidator ID="regexpName" runat="server" Font-Size="XX-Small" Font-Names="Verdana"
                                        ErrorMessage="<strong>ERROR</strong>Use only alphabets or numbers." 
                                        ControlToValidate="TextBox1"     
                                        ValidationExpression="^[a-zA-Z0-9'.\s]{1,40}$" /></td></tr></table>
                    
                    <asp:Button ID="Button1" runat="server" Text="Submit Your Comment" OnClick="Button1_Click" BackColor="Transparent" />
                    </div>
          <div id="commentDiv" style="text-align:left;padding-left:5px;">
                       <asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource2">
        	<itemtemplate>
            PhotoID: <%# Eval("pID") %><br />
           <strong> User Name: <%# Eval("UserName")%><br/></strong>
             <%# Eval("CommentDesc")%><br/>
             ----------------------------------------<br />
    
            </itemtemplate>
        </asp:Repeater>
        </div>
    
    			</itemtemplate>
    		</asp:formview>
            <div id="myDiv" style="; top:320px; left:300px;">
            
             </div>
             
            <hr />
            
         
    	</div>
    
    	<asp:ObjectDataSource ID="ObjectDataSource1" Runat="server" TypeName="PhotoManager" 
    		SelectMethod="GetPhotos">
    		<SelectParameters>
    			<asp:QueryStringParameter Name="AlbumID" Type="Int32" QueryStringField="AlbumID" DefaultValue="0"/>
    		</SelectParameters>
    	</asp:ObjectDataSource>
    
      
      <asp:ObjectDataSource ID="ObjectDataSource2" Runat="server" TypeName="PhotoManager"  
    		SelectMethod="GetComments">
    		<SelectParameters>
    		
                   <asp:ControlParameter Name="PhotoID" ControlID="FormView1" PropertyName="SelectedValue" Type="Int32"
                   DefaultValue="0" />
    		</SelectParameters>
             
            </asp:ObjectDataSource>
    </asp:content>
    ----------------------------------------------------------------------------
    Details.aspx.cs
    ............................
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    public partial class Details_aspx : System.Web.UI.Page
    {
    
        void Page_Load(object sender, EventArgs e)
        {
            Page.MaintainScrollPositionOnPostBack = true;
            if (!IsPostBack)
            {
                int i = Convert.ToInt32(Request.QueryString["Page"]);
                if (i >= 0) FormView1.PageIndex = i;
            }
        }
        public void Button1_Click(Object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
            {
                Response.Redirect("login.aspx", true);
            }
            else
            {
                TextBox tb1 = (TextBox)FormView1.FindControl("TextBox1");
                 string userComment = tb1.Text;
                MembershipUser myObject = Membership.GetUser();
                string UserID = myObject.ProviderUserKey.ToString();
               // = "HELLO";
                int i = Convert.ToInt32(Label2.Text);
                string queryString = @"INSERT INTO User_Photo_Comment (commentDesc, pID, uID) VALUES ('";
                queryString += userComment+"' , "+i+", '"+UserID+"')";
                using (System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(
                                       @"Data Source=SHARMIN-PC\SQLEXPRESS;Initial Catalog=aspnetdb;Integrated Security=True"))
                {
                    System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(
                        queryString, connection);
                    connection.Open();
                    try
                    {
                        command.ExecuteNonQuery();
                    }
                    finally
                    {
                        connection.Close();
                    }
                    Response.Redirect(Request.RawUrl);
                   
    
                }
    
              
            }
        }
    
       
    }
    --------------------------------------------------------------------------
    Photos.aspx
    .........................
    <%@	Page Language="C#" MasterPageFile="~/Default.master" Title="Your Name Here | Photos"
    	CodeFile="Photos.aspx.cs" Inherits="Photos_aspx" %>
    
    <asp:content id="Content1" contentplaceholderid="Main" runat="server">
    	
    	<div class="shim solid"></div> 
    	  
    	<div class="page" id="photos">
    		<div class="buttonbar buttonbar-top">
    			<a href="Albums.aspx"><asp:image ID="Image1" runat="Server" skinid="gallery" /></a>
    		</div>
    		<asp:DataList ID="DataList1" runat="Server"	cssclass="view"	dataSourceID="ObjectDataSource1" 
    			repeatColumns="4" repeatdirection="Horizontal" onitemdatabound="DataList1_ItemDataBound" EnableViewState="false">
    			<ItemTemplate>
    				<table border="0" cellpadding="0" cellspacing="0" class="photo-frame">
    					<tr>
    						<td class="topx--"></td>
    						<td class="top-x-"></td>
    						<td class="top--x"></td>
    					</tr>
    					<tr>
    						<td class="midx--"></td>
    						<td><a href='Details.aspx?AlbumID=<%# Eval("AlbumID") %>&Page=<%# Container.ItemIndex %>'>
    							<img src="Handler.ashx?PhotoID=<%# Eval("PhotoID") %>&Size=S" class="photo_198" style="border:4px solid white" alt='Thumbnail of Photo Number <%# Eval("PhotoID") %>' /></a></td>
    						<td class="mid--x"></td>
    					</tr>
    					<tr>
    						<td class="botx--"></td>
    						<td class="bot-x-"></td>
    						<td class="bot--x"></td>
    					</tr>
    				</table>
    				<p><%# Server.HtmlEncode(Eval("Caption").ToString()) %></p>
    			</ItemTemplate>
    			<FooterTemplate>
    			</FooterTemplate>
    		</asp:DataList>
    		<asp:panel id="Panel1" runat="server" visible="false" CssClass="nullpanel">There are currently no pictures in this album.</asp:panel>
    		<div class="buttonbar">
    			<a href="Albums.aspx"><asp:image id="gallery" runat="Server" skinid="gallery" /></a>
    		</div>
    	</div>
    	
    	<asp:ObjectDataSource ID="ObjectDataSource1" Runat="server" TypeName="PhotoManager" 
    		SelectMethod="GetPhotos">
    		<SelectParameters>
    			<asp:QueryStringParameter Name="AlbumID" Type="Int32" QueryStringField="AlbumID" DefaultValue="0"/>
    		</SelectParameters>
    	</asp:ObjectDataSource>
    	
    </asp:content>

    Wednesday, January 12, 2011 8:23 PM

Answers

  • User238457982 posted

    Hello,

    I solved the problem.

    What is the project scenario?


    I added a comment table in Microsoft's Personal Album Database. When user selects a photo from the
    album, and insert comment, the comment would appear in the same page where that currently
    selected photo is getting displayed.

    What was the problem?

    If the user selects a photo, say photo # 8, and enters comment, the scenario works fine; however, if the user selects next/prev photo and enters comment, the originally selected photo page appears ( where the currently selected photo page should appear)

    What is the solution?

    In button click event, I added

    string txt = FormView1.PageIndex.ToString(); //get the current page index, that is showing the photo
    Then, later, I added
    Response.Redirect(@"http://localhost:64570/PersonalWebSiteCSharp/Details.aspx?AlbumID=6&Page=" + txt);


    The details.aspx is attached which has the solution inside.

    Details.aspx
    ........................
    
    <%@	Page Language="C#" MasterPageFile="~/Default.master" Title="Your Name Here | Picture Details" CodeFile="Details.aspx.cs" Inherits="Details_aspx" %>
    
    <asp:content id="Content1" contentplaceholderid="Main" runat="server">
    <script runat="server">  
         void FormView_ItemCreated(Object sender, EventArgs e)  
       {
           Label1.Text = Request.RawUrl;
         Label2.Text = FormView1.SelectedValue.ToString();
             int l = Request.RawUrl.Length;
             Label3.Text = Request.RawUrl.Substring(Request.RawUrl.Length-1);
       //  Response.Redirect(@"http://localhost:64570/PersonalWebSiteCSharp/Details.aspx?AlbumID=6&Page=1");
    
    
             // Get the pager row.
             FormViewRow pagerRow = FormView1.BottomPagerRow;
    
             // Get the Label controls that display the current page information 
             // from the pager row.
             Label pageNum = (Label)pagerRow.Cells[0].FindControl("PageNumberLabel");
             Label totalNum = (Label)pagerRow.Cells[0].FindControl("TotalPagesLabel");
    
             if ((pageNum != null) && (totalNum != null))
             {
                 // Update the Label controls with the current page values.
                 int page = FormView1.PageIndex + 1;
                 int count = FormView1.PageCount;
    
                 pageNum.Text = page.ToString();
                 totalNum.Text = count.ToString();
             }
             
      }
         
    
         
        </script>
    	<div class="shim solid"></div>
    
    	<div class="page" id="details">
     
         <asp:Label ID="Label1" runat="server" ></asp:Label>  | | 
          <asp:Label ID="Label2" runat="server" ></asp:Label>    | | 
          <asp:Label ID="Label3" runat="server" ></asp:Label>  
    
          <br />
            
    
    		<asp:formview id="FormView1" runat="server"	datasourceid="ObjectDataSource1"    
             OnDataBound="FormView_ItemCreated"
            cssclass="view"  DataKeyNames="PhotoID"
    
    			borderstyle="none" borderwidth="0" CellPadding="0" cellspacing="0" 
                EnableViewState="false" AllowPaging="true" 
                >
                <PagerTemplate>
                  Page <asp:label id="PageNumberLabel" runat="server"/> 
                    of <asp:label id="TotalPagesLabel" runat="server"/>       
    
                
                </PagerTemplate>
    			<itemtemplate>
               
    			
    				<div class="buttonbar buttonbar-top">
    					<a href="Albums.aspx"><asp:image ID="Image1" runat="Server"	 skinid="gallery" /></a>
    					    
    					<asp:ImageButton ID="ImageButton9" Runat="server" CommandName="Page" CommandArgument="First" skinid="first"/>
    					<asp:ImageButton ID="ImageButton10"	Runat="server" CommandName="Page" CommandArgument="Prev" skinid="prev"/>
    					<asp:ImageButton ID="ImageButton11"	Runat="server" CommandName="Page" CommandArgument="Next" skinid="next"/>
    					<asp:ImageButton ID="ImageButton12"	Runat="server" CommandName="Page" CommandArgument="Last" skinid="last"/>
    				</div>
    				<p><%# Server.HtmlEncode(Eval("Caption").ToString()) %></p>
    				<table border="0" cellpadding="0" cellspacing="0" class="photo-frame">
    					<tr>
    						<td class="topx--"></td>
    						<td class="top-x-"></td>
    						<td class="top--x"></td>
    					</tr>
    					<tr>
    						<td class="midx--"></td>
    						<td><img src="Handler.ashx?PhotoID=<%# Eval("PhotoID") %>&Size=S" class="photo_198" 
                            style="border:4px solid white" alt='Photo Number <%# Eval("PhotoID") %>' /></a></td>
    						<td class="mid--x"></td>
    					</tr>
    					<tr>
    						<td class="botx--"></td>
    						<td class="bot-x-"></td>
    						<td class="bot--x"></td>
    					</tr>
    				</table>
    				<p><a href='Download.aspx?AlbumID=<%# Eval("AlbumID") %>&Page=<%# Container.DataItemIndex %>'>
    					<asp:image runat="Server" id="DownloadButton" AlternateText="download this photo" skinid="download" /></a></p>
    				<div class="buttonbar">
    					<a href="Albums.aspx"><asp:image ID="Image2" runat="Server"	 skinid="gallery" /></a>
    					    
    					<asp:ImageButton ID="ImageButton1" Runat="server" CommandName="Page" CommandArgument="First" skinid="first"/>
    					<asp:ImageButton ID="ImageButton2" Runat="server" CommandName="Page" CommandArgument="Prev" skinid="prev"/>
    					<asp:ImageButton ID="ImageButton3" Runat="server" CommandName="Page" CommandArgument="Next" skinid="next"/>
    					<asp:ImageButton ID="ImageButton4" Runat="server" CommandName="Page" CommandArgument="Last" skinid="last"/>
                        
    
                        <br />
                          <br />
                    
    				</div>
           
                   <div id="Div1" style="text-align:left; padding-left:5px; padding-top:5px; padding-bottom:20px;">
                  <asp:Label ID="UserComment" runat="server" Text="Submit Your Comment" Font-Bold="true" Font-Size="Large"></asp:Label><br />
                  <table><tr><td> <asp:TextBox ID="TextBox1" runat="server" Width="350px" Height="80px"></asp:TextBox></td>
                  <td>             
                      <asp:RegularExpressionValidator ID="regexpName" runat="server" Font-Size="XX-Small" Font-Names="Verdana"
                                        ErrorMessage="<strong>ERROR</strong>Use only alphabets or numbers." 
                                        ControlToValidate="TextBox1"     
                                        ValidationExpression="^[a-zA-Z0-9'.\s]{1,40}$" /></td></tr></table>
                    
                    <asp:Button ID="Button1" runat="server" Text="Submit Your Comment" OnClick="Button1_Click" BackColor="Transparent"/>
                    </div>
          <div id="commentDiv" style="height:100px; overflow:auto; text-align:left;padding-left:5px;">
                       <asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource2">
        	<itemtemplate>
            PhotoID: <%# Eval("pID") %><br />
           <strong> User Name: <%# Eval("UserName")%><br/></strong>
             <%# Eval("CommentDesc")%><br/>
             ----------------------------------------<br />
    
            </itemtemplate>
        </asp:Repeater>
    
        </div>
    
    			</itemtemplate>
    
            
    		</asp:formview>
               
            <div id="myDiv" style="; top:320px; left:300px;">
            
             </div>
             
            <hr />
            
         
        </div>
    
    	<asp:ObjectDataSource ID="ObjectDataSource1" Runat="server" TypeName="PhotoManager" 
    		SelectMethod="GetPhotos">
    		<SelectParameters>
    			<asp:QueryStringParameter Name="AlbumID" Type="Int32" QueryStringField="AlbumID" DefaultValue="0"/>
    		</SelectParameters>
    	</asp:ObjectDataSource>
    
      
      <asp:ObjectDataSource ID="ObjectDataSource2" Runat="server" TypeName="PhotoManager"  
    		SelectMethod="GetComments">
    		<SelectParameters>
    		
                   <asp:ControlParameter Name="PhotoID" ControlID="FormView1" PropertyName="SelectedValue" Type="Int32"
                   DefaultValue="0" />
    		</SelectParameters>
             
            </asp:ObjectDataSource>
    </asp:content>
    =================================================
    Deatils.aspx.cs
    ..........................
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    public partial class Details_aspx : System.Web.UI.Page
    {
    
        void Page_Load(object sender, EventArgs e)
        {
            Page.MaintainScrollPositionOnPostBack = true;
            if (!IsPostBack)
            {
                int i = Convert.ToInt32(Request.QueryString["Page"]);
                if (i >= 0) FormView1.PageIndex = i;
            
            }
        }
       
        public void Button1_Click(Object sender, EventArgs e)
        {
            
            if (!User.Identity.IsAuthenticated)
            {
                Response.Redirect("login.aspx", true);
            }
            else
            {
    
                string txt = FormView1.PageIndex.ToString();
                TextBox tb1 = (TextBox)FormView1.FindControl("TextBox1");
                 string userComment = tb1.Text;
                MembershipUser myObject = Membership.GetUser();
                string UserID = myObject.ProviderUserKey.ToString();
               // = "HELLO";
                int i = Convert.ToInt32(Label2.Text);
                string queryString = @"INSERT INTO User_Photo_Comment (commentDesc, pID, uID) VALUES ('";
                queryString += userComment+"' , "+i+", '"+UserID+"')";
                using (System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(
                                       @"Data Source=SHARMIN-PC\SQLEXPRESS;Initial Catalog=aspnetdb;Integrated Security=True"))
                {
                    System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(
                        queryString, connection);
                    connection.Open();
                    try
                    {
                        command.ExecuteNonQuery();
                       
                    }
                    finally
                    {
                        connection.Close();
                    }
                    Response.Redirect(@"http://localhost:64570/PersonalWebSiteCSharp/Details.aspx?AlbumID="+Request.QueryString["AlbumID"]+"&Page=" + txt);
    
                    
                }
    
              
            }
        }
    
       
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 13, 2011 1:17 AM