简体   繁体   English

ASP.NET VS2008中的gridview

[英]gridview in asp.net VS2008

Have a gridview control and i want to display checkboxes for each row. 有一个gridview控件,我想为每一行显示复选框。 The checkboxes should only appear if Session["DisplayBox"] == true. 仅当Session [“ DisplayBox”] == true时,才应显示复选框。

<asp:GridView ID="gridView" runat="server" AutoGenerateColumns="False" EnableSortingAndPagingCallbacks="True"
    AllowPaging="True" DataSourceID="JObjectDataSource" PageSize="5" OnRowCommand="gridView_RowCommand"
    DataKeyNames="ID" Width="100%">
    <Columns>
        <asp:TemplateField HeaderText="Review">
            <ItemTemplate>
                <asp:CheckBox  ID="chkRejectFile" AutoPostBack="true" runat="server" OnCheckedChanged="chkRejectFile_CheckedChanged" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>

I removed some of the columns and kept the one that i am asking the question about. 我删除了一些列,并保留了我要问的问题。 How would I place a conditional code in the aspx page and check for the session value? 如何在aspx页面中放置条件代码并检查会话值?

Also, if I page, do i have to explicitly handle keeping track of which row was checked and which wasn't? 另外,如果我要翻页,是否必须显式处理跟踪已检查的行和未检查的行?

Add a callback handler for the OnDataBind event of the GridView. 为GridView的OnDataBind事件添加一个回调处理程序。 Then on each row, determine whether or not to show the checkbox. 然后在每一行上,确定是否显示该复选框。

The code will of course be in your .cs file. 该代码当然会在您的.cs文件中。

Add a new event property to the grid like this one: 像这样向网格添加一个新的事件属性:

OnRowDataBound="gridView_RowDataBound"

And then, in the code-behind, add the following corresponding event handler: 然后,在后面的代码中,添加以下相应的事件处理程序:

protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if(e.Row.RowType == DataControlRowType.DataRow)
  {
    // check for null first
    object displayBoxFlag = Session["DisplayBox"];
    if(displayBoxFlag!=null && (bool) displayBoxFlag)
    {
      e.Row.FindControl("chkRejectFile").Visible = true;
    }
    else
    {
      e.Row.FindControl("chkRejectFile").Visible = false;
    }
  }
}

Plenty of room to optimize, but it should work. 有足够的空间进行优化,但是应该可以工作。

What happens is that ASP.NET will raise this event and call the method for each row of the grid right after they are bound. 发生的情况是,ASP.NET将在绑定它们后立即引发此事件并为网格的每一行调用该方法。 You now may go ahead and override or customize the way they appear. 现在,您可以继续并覆盖或自定义它们的显示方式。

You want to set the Visible property based on the value of the session variable. 您要基于会话变量的值设置Visible属性。 Add something like the following (untested) code to your control: 将类似以下(未经测试)的代码添加到您的控件中:

 Visible='<%# Convert.ToBoolean(HttpContext.Current.Session["DisplayBox"]) %>'

Note that this doesn't check if the session variable is actually defined, ie, it depends on it being set and set to either true or false . 请注意,这不会检查会话变量是否已实际定义,即取决于它是否被设置并设置为truefalse

If you do paging and care about retaining checked status, you'll need to keep track of the checked status for each item explicitly. 如果进行分页并关心保留已检查状态,则需要明确跟踪每个项目的已检查状态。 The only thing that will get posted back on submit are the controls that are actually on the page at the time of the submit. 提交回发的唯一一件事就是提交时页面上实际存在的控件。

您可以在后面的代码中覆盖“ OnRowDataBound”事件,并针对每一行执行逻辑并相应地设置visisble属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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