简体   繁体   中英

ASPxGridView lose data when moving to the next page

I am binding an ASPxGridView to an SqlDataSource:

protected void search_ServerClick(object sender, EventArgs e)
    {
        string selectCommand = @"SELECT     dbo.TaskSchedularTb.UniqueID, dbo.UserTb.UserName, dbo.TaskSchedularTb.UserID, dbo.TaskTypeTb.TypeID, dbo.TaskTypeTb.TaskType, 
                  dbo.TaskLabelTb.LabelID, dbo.TaskLabelTb.Label, dbo.TaskStatusTb.StatusID, dbo.TaskStatusTb.Status, dbo.ProjectTb.ProjectID, dbo.ProjectTb.ProjectName, 
                  dbo.TaskSchedularTb.StartDate, dbo.TaskSchedularTb.EndDate, dbo.TaskSchedularTb.AllDay, dbo.TaskSchedularTb.Subject, dbo.TaskSchedularTb.Location, 
                  dbo.TaskSchedularTb.Description,dbo.UserTb.userImagePath
                  FROM         dbo.TaskSchedularTb INNER JOIN
                  dbo.UserTb ON dbo.TaskSchedularTb.UserID = dbo.UserTb.UserID INNER JOIN
                  dbo.TaskTypeTb ON dbo.TaskSchedularTb.TaskType = dbo.TaskTypeTb.TypeID INNER JOIN
                  dbo.TaskLabelTb ON dbo.TaskSchedularTb.Label = dbo.TaskLabelTb.LabelID INNER JOIN
                  dbo.ProjectTb ON dbo.TaskSchedularTb.ResourceID = dbo.ProjectTb.ProjectID INNER JOIN
                  dbo.TaskStatusTb ON dbo.TaskSchedularTb.Status = dbo.TaskStatusTb.StatusID 
                  where dbo.TaskSchedularTb.UserID = " + int.Parse(Session["userID"].ToString());

        if (dfrom.Value != "" && dto.Value != "")
        {
            selectCommand += " AND dbo.TaskSchedularTb.StartDate >='" + Convert.ToDateTime(dfrom.Value).Year.ToString() + "-" + Convert.ToDateTime(dfrom.Value).Month.ToString() + "-" +
                Convert.ToDateTime(dfrom.Value).Day.ToString() + "'";
            selectCommand += " AND dbo.TaskSchedularTb.EndDate <='" + Convert.ToDateTime(dto.Value).Year.ToString() + "-" + Convert.ToDateTime(dto.Value).Month.ToString() + "-" +
                Convert.ToDateTime(dto.Value).Day.ToString() + "'";

        }
        else if (dfrom.Value != "")
            selectCommand += " AND dbo.TaskSchedularTb.StartDate ='" + Convert.ToDateTime(dfrom.Value).Year.ToString() + "-" + Convert.ToDateTime(dfrom.Value).Month.ToString() + "-" +
                Convert.ToDateTime(dfrom.Value).Day.ToString() + "'";
        else if(dto.Value != "")
            selectCommand += " AND dbo.TaskSchedularTb.EndDate ='" + Convert.ToDateTime(dto.Value).Year.ToString() + "-" + Convert.ToDateTime(dto.Value).Month.ToString() + "-" +
                Convert.ToDateTime(dto.Value).Day.ToString() + "'";

        if (project.Value != "")
            selectCommand += " AND dbo.TaskSchedularTb.ResourceID=" + project.Value.ToString();

        if (selecttype.Value != "")
            selectCommand += " AND dbo.TaskSchedularTb.TaskType=" + selecttype.Value.ToString();

        if (status.Value != "")
            selectCommand += " AND dbo.TaskSchedularTb.Status=" + status.Value.ToString();

        SqlDataSource1.SelectCommandType = SqlDataSourceCommandType.Text;
        SqlDataSource1.SelectCommand = selectCommand;
        SqlDataSource1.DataBind();


        ASPxGridView1.DataSourceID = "";
        ASPxGridView1.DataSource = SqlDataSource1;
        ASPxGridView1.KeyFieldName = "UniqueID";
        ASPxGridView1.DataBind();
    }

I tried to get the value of the Key in the HtmlRowPrepared event:

protected void ASPxGridView1_HtmlRowPrepared(object sender, ASPxGridViewTableRowEventArgs e)
    {
        if (e.KeyValue != null)
            uniqueID_var = e.KeyValue.ToString();
    }

And when i moved to the next page the KeyValue become null and the data in the gridview loses.

The ascx file contains the following code

<dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" KeyFieldName="UniqueID" Theme="Moderno" Width="100%" OnHtmlRowPrepared="ASPxGridView1_HtmlRowPrepared">
<SettingsAdaptivity AdaptivityMode="HideDataCells">
</SettingsAdaptivity>
<Templates>
    <DataRow>

        <ul class="timeline">
            <li id="page2">
                <div class="main">
                    <!-- begin timeline-time -->
                    <div class="timeline-time">
                        <span class="date"><%# Eval("StartDate", "{0:d}") %>
                        </span><span class="time"><%# Eval("StartDate", "{0:t}") %></span>
                    </div>
                    <!-- end timeline-time -->
                    <!-- begin timeline-icon -->
                    <div class="timeline-icon">
                        <a href="javascript:;">&nbsp;</a>
                    </div>
                    <!-- end timeline-icon -->
                    <!-- begin timeline-body -->
                    <div class="timeline-body">
                        <div class="timeline-header">
                            <span class="userimage">
                                <img alt="" src='<%# "assets/img/user/"+Eval("userImagePath")  %>' />
                            </span><span class="pull-right text-muted">
                                <asp:HiddenField ID="taskID" Value='<%# Eval("UniqueID") %>' runat="server" />
                                <a href='<%# "taskAdd.aspx?action=Edit&id="+Eval("UniqueID") %>'><i class="fas fa-edit"></i></a></span><span class="username"><a href="javascript:;"><%# Eval("UserName") %></a><br />
                                    <small>Project Name: <%# Eval("ProjectName") %></small> </span>
                        </div>
                        <div class="timeline-content">
                            <p>
                                <%# Eval("Subject")+" "+ Eval("Description") %>
                            </p>
                        </div>
                        <div class="timeline-likes">
                            <div class="stats-right">
                                <span class="stats-text"><%# "Status: "+Eval("Status") %></span> <span class="stats-text"><%# "Type: "+Eval("TaskType") %></span>
                            </div>
                            <div class="stats">
                                <span class="stats-total"><%# "Deadline: "+Eval("EndDate") %></span>
                            </div>
                        </div>
                        <div class="timeline-footer">
                            <div class="stats">
                                <span class="stats-total">Attachments: </span>
                            </div>
                            <%--<a class="m-r-15 text-inverse-lighter" href="javascript:;"><i class="fa fa-thumbs-up fa-fw fa-lg m-r-3"></i>Like</a> <a class="m-r-15 text-inverse-lighter" href="javascript:;"><i class="fa fa-comments fa-fw fa-lg m-r-3"></i>Comment</a> <a class="m-r-15 text-inverse-lighter" href="javascript:;"><i class="fa fa-share fa-fw fa-lg m-r-3"></i>Shae</a>--%><dx:ASPxDataView ID="ASPxDataView1" runat="server" AllowPaging="False" DataSourceID="SqlDataSource2" EnableTheming="True" Theme="Moderno" ItemSpacing="2px" Height="10%" Width="100%">
                                <SettingsTableLayout RowsPerPage="1" ColumnCount="6" />
                                <PagerSettings ShowNumericButtons="False" Visible="False">
                                </PagerSettings>
                                <ItemTemplate>
                                    <a href='<%# System.Web.HttpContext.Current.Server.HtmlEncode(Eval("FilePath").ToString()) %>' target="_blank"><i class='fas fa-<%# fileType(Eval("FileType").ToString())%> fa-2x'></i></a>
                                </ItemTemplate>
                                <Paddings Padding="0px" />
                                <ContentStyle Border-BorderStyle="None">
                                    <Paddings Padding="0px" />
                                </ContentStyle>
                                <ItemStyle Height="10%" Width="10%" Border-BorderStyle="None" HorizontalAlign="Center" />
                            </dx:ASPxDataView>
                            &nbsp;<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:connectionString %>" SelectCommand="SELECT * FROM [TaskAttachmentTb] WHERE ([TaskSchedularID] = @TaskSchedularID)">
                                <SelectParameters>
                                    <asp:FormParameter DefaultValue="1" FormField="taskID" Name="TaskSchedularID" Type="Int32" />
                                </SelectParameters>
                            </asp:SqlDataSource>

                        </div>
                        <div class="timeline-comment-box">
                            <div class="user">
                                <img src='<%# "assets/img/user/"+ session_user_image()%>' />
                            </div>
                            <div class="input">
                                <div class="input-group">
                                    <input id="commentStr" runat="server" class="form-control rounded-corner" placeholder="Write a comment..." type="text" />
                                    <span class="input-group-btn p-l-10">
                                        <button id="btnComment" value='<%# Eval("UniqueID") %>' runat="server" class="btn btn-primary f-s-12 rounded-corner" type="button" onserverclick="btnComment_ServerClick">
                                            Comment
                                        </button>
                                        <%--<input id="btnComment" type="button" value='Comment' runat="server"  class="btn btn-primary f-s-12 rounded-corner" onserverclick="btnComment_ServerClick" />--%>
                                    </span>
                                </div>
                            </div>
                            <div>
                                &nbsp;
                            </div>
                            <% 
                                List<Task.std_task_comments_get_Result> com_list = getComments();
                                foreach (Task.std_task_comments_get_Result lc in com_list)
                                {
                                    string cdv = lc.CommentDate.Value.ToString();
                                    string cmnt = lc.Comment.ToString();
                                    string uiid = getCommentedUserImage(lc.UserID.ToString());
                                    string cuv = getCommentedUserName(lc.UserID.ToString());
                            %>
                            <div class="user">
                                <img id="123" src='<%= "assets/img/user/"+uiid %>' />

                            </div>
                            <div class="input">
                                <div class="input-group">
                                    <label id="commentUser" style="width: 100%;">
                                        <%= cuv %></label>
                                    <div id="commentDiv">
                                        <%= cmnt %> &nbsp; &nbsp;&nbsp; <a href="javascript:;">1h</a>
                                    </div>
                                </div>
                            </div>
                            <% }; %>
                        </div>
                    </div>
                    <!-- end timeline-body -->
                </div>
            </li>
        </ul>
    </DataRow>
</Templates>
<SettingsPager PageSize="1">
</SettingsPager>
<Settings GridLines="Horizontal" ShowColumnHeaders="False" />
<Columns>
    <dx:GridViewCommandColumn VisibleIndex="0">
    </dx:GridViewCommandColumn>
</Columns>
<Styles>
    <PagerBottomPanel HorizontalAlign="Center">
    </PagerBottomPanel>
    <SearchPanel HorizontalAlign="Center">
    </SearchPanel>
</Styles>

And the sql datasource in the ascx is:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:connectionString %>" SelectCommand="std_taskScheduler_get_my" SelectCommandType="StoredProcedure">
<SelectParameters>
    <asp:SessionParameter DefaultValue="2" Name="UserID" SessionField="userID" Type="Int32" />
</SelectParameters>

Is there any problem in the code above ?

You need to bind your data on page load not just on your button click event. I suggest moving you binding logic out into its own method and calling on page_load and when you click your button.

Also look at using parameters when making calls to your database. try not to concatenate sql code as it can lead to all sorts of issues such as sql injection

if (IsPostBack){ // you may need a ispostback in you page load.
     binddatamethod()    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM