我将ASPxGridView绑定到SqlDataSource:
using AvroStorage()
我试图在HtmlRowPrepared事件中获取Key的值:
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();
}
当我移到下一页时,KeyValue变为null,并且gridview中的数据丢失。
ascx文件包含以下代码
protected void ASPxGridView1_HtmlRowPrepared(object sender, ASPxGridViewTableRowEventArgs e)
{
if (e.KeyValue != null)
uniqueID_var = e.KeyValue.ToString();
}
而ascx中的sql数据源是:
<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:;"> </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>
<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>
</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 %> <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>
上面的代码有什么问题吗?
答案 0 :(得分:0)
您不仅需要在按钮单击事件上绑定数据,还需要在页面加载时绑定数据。 我建议将绑定逻辑移到其自己的方法中,并在单击按钮时调用page_load。
在调用数据库时还要考虑使用参数。尽量不要串联sql代码,因为它可能导致各种问题,例如sql injection
if (IsPostBack){ // you may need a ispostback in you page load.
binddatamethod()
}