除非浏览器刷新,否则设置为visible = true时隐藏按钮不会显示

时间:2013-11-08 19:16:21

标签: c# asp.net

如果符合某些条件,我会隐藏网格视图中的某个按钮:

protected void storyGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
      int count = Int32.Parse(SqlInteraction.sqlQueryReturnString(conn => countNumberOfHoursBilled(conn, DataBinder.Eval(e.Row.DataItem, "PK_NonScrumStory").ToString())));
      DateTime createdDate = DateTime.Parse(DataBinder.Eval(e.Row.DataItem, "CreatedDate").ToString());
      if (count > 0 || createdDate < DateTime.Today)
      {
         Button btn = (Button)e.Row.FindControl("deleteButton");
         btn.Visible = false;
      }
      else
      {
         Button btn = (Button)e.Row.FindControl("deleteButton");
         btn.Visible = true;
      }
   }
}

这很好用,但我注意到在使用页面时满足其中一个条件时,除非我刷新页面,否则删除按钮不会正确地来回改变。

我试图对gridview进行数据绑定,但它不起作用;

protected void viewHoursGridView_OnRowDeleting(object sender, GridViewDeleteEventArgs e)
{
   GridView gv = sender as GridView;
   SqlDataSource6.DeleteParameters["setDailyPKDeleteParam"].DefaultValue = gv.DataKeys[e.RowIndex].Value.ToString();
   storyGridView.DataBind();
}

因此,如果我删除一行并且计数变为零,则该按钮应该是可见的。但是在刷新页面之前它不会变得可见。

我该如何解决这个问题?

编辑:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
    SelectCommand="
                SELECT NSS.PK_NonScrumStory
                    ,SCY.Catagory AS Catagory
                    ,APN.AppName AS [Application]
                    ,NSS.IncidentNumber AS IncidentNumber
                    ,CASE WHEN (NSS.[Status] = 1) THEN 'Open' ELSE 'Closed' END AS [Status]
                    ,NSS.EstimatedHours AS EstimatedHours
                    ,NSS.[Description] AS [Description]
                    ,NSS.CreateDate AS CreatedDate
                FROM NonScrumStory NSS
                LEFT JOIN SupportCatagory SCY ON NSS.CatagoryId = SCY.PK_SupportCatagory
                LEFT JOIN [Application] APN ON NSS.ApplicationId = APN.PK_Application
                WHERE NSS.Deleted = 0
                    AND NSS.UserId = @nonScrumStoryUser
                ORDER BY CreatedDate DESC
    ">
    <SelectParameters>
        <asp:QueryStringParameter Name="nonScrumStoryUser" Type="Int16" />
    </SelectParameters>
</asp:SqlDataSource>

1 个答案:

答案 0 :(得分:1)

您需要在RowDeleting事件中再次获取数据,然后将该数据绑定到GridView。您基本上需要再次设置GridView的DataSource并绑定数据,以便在删除行后可以看到更新的数据。

<强>更新

在查看SqlDataSource之后,单击“删除”按钮时,您还没有编写删除行的命令。因此,更改不会被推送到数据库,这就是您的GridView没有看到更改的原因。您需要向SqlDataSource添加DeleteCommand。

DeleteCommand="DELETE FROM [TableName] WHERE [ColumnName]=@[ParameterName];">
            <DeleteParameters>
                <asp:ControlParameter Name="[ParameterName]" ControlId="storyGridView" PropertyName="SelectedValue" />
            </DeleteParameters>

代码可能在语法上并不完全正确,但这是为了给你一个想法。 请查看来自MSDN的this样本。