C# 是否可能将数据从Webservice拉入Gridview?
我正试图将定价从我的Web服务拉到我的gridview中。为此,我在Itemtemplate和GridView行中使用了一个带有标签的列 我得到的错误标签2在当前上下文中不存在????但显然是这样。当我将label2移到GridView之外时,就会找到它。但我得到的错误对象引用未设置为PartNumber=row.Cells[1].Text上对象的实例 这段代码会像我写的那样工作吗?以下是我用于执行此操作的代码:C# 是否可能将数据从Webservice拉入Gridview?,c#,asp.net,C#,Asp.net,我正试图将定价从我的Web服务拉到我的gridview中。为此,我在Itemtemplate和GridView行中使用了一个带有标签的列 我得到的错误标签2在当前上下文中不存在????但显然是这样。当我将label2移到GridView之外时,就会找到它。但我得到的错误对象引用未设置为PartNumber=row.Cells[1].Text上对象的实例 这段代码会像我写的那样工作吗?以下是我用于执行此操作的代码: <asp:GridView ID="GridView1" runat="se
<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>
无法在页面加载中直接访问label控件,因为它位于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>
或者,您可以更改标记以直接绑定数据,如下所示:-
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>