0
我想從我的web服務拉到我的gridview定價。爲此,我在Itemtemplate和GridView
行中使用帶有標籤的列。將Web服務中的數據拉入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";
}
}
}