可以将Web服务中的数据提取到Gridview吗?

时间:2014-11-22 00:51:56

标签: c# asp.net

我正试图将我的网络服务定价拉到我的gridview中。为此,我在Itemtemplate和GridView行中使用带有标签的列。

我收到错误Label2 Does not exist in the current context????但显然这样做了。当我移动label2之外的GridView时,会找到它。但我收到错误Object reference not set to an instance of an object on PartNumber = row.Cells[1].Text;

这段代码也会像我写的那样工作吗? (这是我用来执行此操作的代码:)

<asp:GridView ID="GridView1" runat="server" BackColor="White" 
     BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" 
     CellPadding="4" ForeColor="Black" GridLines="Vertical" 
     AutoGenerateColumns="False" DataKeyNames="ID" 
     DataSourceID="SqlDataSource2" Width="652px" CssClass="mGrid" 
     PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt" >
    <AlternatingRowStyle BackColor="White" CssClass="alt" />
    <FooterStyle BackColor="#CCCC99" />
    <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#F7F7DE" ForeColor="Black" 
             HorizontalAlign="Right" CssClass="pgr" />
    <RowStyle BackColor="#F7F7DE" />
    <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#FBFBF2" />
    <SortedAscendingHeaderStyle BackColor="#848384" />
    <SortedDescendingCellStyle BackColor="#EAEAD3" />
    <SortedDescendingHeaderStyle BackColor="#575357" />
    <Columns>
       <asp:BoundField DataField="ID" HeaderText="ID" 
            InsertVisible="False" ReadOnly="True" SortExpression="ID" 
            Visible="False" />
       <asp:BoundField DataField="PartNumber" 
            HeaderText="PartNumber" SortExpression="PartNumber" />
       <asp:BoundField DataField="PartDescription" 
            HeaderText="PartDescription" SortExpression="PartDescription" />
       <asp:BoundField DataField="Qty" HeaderText="Qty" SortExpression="Qty" />
       <asp:TemplateField HeaderText="Price">
       <ItemTemplate>
          <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
       </ItemTemplate>
       </asp:TemplateField>
    </Columns>        
</asp:GridView>

这是C#代码:

protected void Page_Load(object sender, EventArgs e)
{
   WebReference.WebServiceTyped ws = new WebReference.WebServiceTyped();
    WebReference.CheckPartStatus PQ = new WebReference.CheckPartStatus();
    string Parts = "";
    string PartNumber = Parts;
    string PriceSum = null;
    long QtySum = 0;
    String CustomerID = "";

    GridViewRow row = GridView1.SelectedRow;

    PartNumber = row.Cells[1].Text;

    if (PartNumber == row.Cells[1].Text)
    {

        PQ = ws.CheckPartNumberStatus(PartNumber, CustomerID, "1,6,8,9,112",
                                                                  "", "", "");

        if (PQ.Parts.Length > 0)
        {
            PriceSum = String.Format(PQ.Parts[0].Cost.ToString(), "####.00");
            Label2.Text = PriceSum;
        }
        else
        {
            Label2.Text = "No Price";
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您无法直接在页面加载中访问标签控件,因为它存在于GridView控件中。你可以在GridView的RowDataBound事件中设置它的值,方法是找到这样的控件: -

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
           Label Label2 = (Label)e.Row.FindControl("Label2");
           //And then set your value
           Label2.Text = //Your value
      }
}

或者,您可以更改标记以直接绑定数据,如下所示: -

<asp:TemplateField HeaderText="Price">
       <ItemTemplate>
             <%# Eval("Your Property")) %>
       </ItemTemplate>
</asp:TemplateField>