为什么ID是空的,从Gridview中挑选出来?

时间:2015-03-18 12:15:42

标签: c# asp.net gridview

为什么deviceID为空,我从SP填充它。在后端它提供了完美的结果,甚至完美地填充了Gridview,但在.cs级别它会产生问题

Select DeviceID, TerminalSNO, SoftwareVersion from Transport.Devices

它提供了完美的结果,但在asp.net中,它从gridview中选择""

GV:

<asp:GridView ID="GridviewDevicesBulkUpdate" runat="server" Width="100%" AutoGenerateColumns="False"
    DataKeyNames="DeviceID" CssClass="table table-hover table-striped table-bordered">
    <Columns>
        <asp:BoundField DataField="DeviceID" HeaderText="DeviceID" Visible="false" ItemStyle-CssClass="visible-desktop" />
        <asp:BoundField DataField="TerminalSNO" HeaderText="Serial No." ItemStyle-CssClass="visible-desktop"
            HeaderStyle-CssClass="visible-desktop" />
        <asp:BoundField DataField="SoftwareVersion" HeaderText="Software Version" ItemStyle-CssClass="visible-desktop"
            HeaderStyle-CssClass="visible-desktop" />
        <asp:TemplateField HeaderText="Includes ?">
            <ItemTemplate>
                <asp:CheckBox runat="server" ID="CheckBoxIncludes" Text="." CssClass="checkbox checkbox-danger" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

的.cs

foreach (GridViewRow Row in GridviewDevicesBulkUpdate.Rows)
{

    CheckBox chkUpdate = (CheckBox)Row.Cells[3].FindControl("CheckBoxIncludes");
    if (chkUpdate.Checked == true)
    {

        string DeviceID = Row.Cells[0].Text.Trim();

        if (ManageTransport.ManageDevices.UpdateAllDevices(IsCardPwdUpdated, SoftwareVersion, IsActive, int.Parse(DeviceID)))
        {
            Response = true;
        }
        else
        {
            Response= false;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

添加RowType GridView例如if (Row.RowType == DataControlRowType.DataRow)

CheckBox chkUpdate = (CheckBox)Row.FindControl("CheckBoxIncludes");

您的代码:

foreach (GridViewRow Row in GridviewDevicesBulkUpdate.Rows)
 {
    if (Row.RowType == DataControlRowType.DataRow)
    {
    CheckBox chkUpdate = (CheckBox)Row.FindControl("CheckBoxIncludes");


     if (chkUpdate.Checked == true)
        {
         string DeviceID = Row.Cells[0].Text.Trim();
         if (ManageTransport.ManageDevices.UpdateAllDevices(IsCardPwdUpdated, SoftwareVersion, IsActive, int.Parse(DeviceID)))
         {
           Response = true;
         }
         else
         {
         Response= false;
         }
      }
    }
    }

答案 1 :(得分:0)

我猜原因是你使用了一个不可见的BoundField。你可以在这里阅读:

Retrieve data from visible false BoundField of Gridview

因此,您可以按照那里的建议或使用带有标签的TemlateField

<Columns>
     <ItemTemplate>
        <Label runat="server" ID="LblDeviceID" Text='<%# Eval("DeviceID") %> ' Visible="false"  />
    </ItemTemplate>
    <asp:BoundField DataField="TerminalSNO" HeaderText="Serial No." ItemStyle-CssClass="visible-desktop"
        HeaderStyle-CssClass="visible-desktop" />
    <asp:BoundField DataField="SoftwareVersion" HeaderText="Software Version" ItemStyle-CssClass="visible-desktop"
        HeaderStyle-CssClass="visible-desktop" />
    <asp:TemplateField HeaderText="Includes ?">
        <ItemTemplate>
            <asp:CheckBox runat="server" ID="CheckBoxIncludes" Text="." CssClass="checkbox checkbox-danger" />
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

然后您可以通过以下方式检索代码隐藏中的值:

Label lblDeviceID = (Label)Row.FindControl("LblDeviceID");
string DeviceID = lblDeviceID.Text;

与此同时,我总是使用TemplateFields,因为您可以在那里使用任何HTML,而不会遇到此类问题。它们更灵活。