<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" DataKeyNames="ProductId" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="ProductId" DataNavigateUrlFormatString=""Product.aspx?ProductId="+Eval("ProductId")"
DataTextField="ProductName" HeaderText="名称" />
<asp:BoundField DataField="ProductId" HeaderText="编号" InsertVisible="False" ReadOnly="True"
SortExpression="ProductId" />
<asp:BoundField DataField="MarketPrice" HeaderText="市场价" SortExpression="MarketPrice" />
<asp:ImageField DataImageUrlField="pic" HeaderText="图片">
</asp:ImageField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:shopConnectionString %>"
SelectCommand="SELECT [ProductName], [ProductId], [MarketPrice], [UserPrice], [Pic] FROM [product]">
</asp:SqlDataSource> protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType ==DataControlRowType .DataRow )
{
if(e.Row .RowState ==DataControlRowState .Normal || e.Row .RowState ==DataControlRowState .Alternate )
{
HyperLink hyp = e.Row.Cells[0].Controls[0] as HyperLink;
TextBox txt = e.Row.Cells[1].Controls[0] as TextBox; //这行出错
if(hyp !=null && lab!=null)
{
hyp.NavigateUrl = "http://www.163.com";
hyp.Text = "我爱中华";
txt.Text = "我爱中华";
}
}
}
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="ProductId" DataNavigateUrlFormatString=""Product.aspx?ProductId="+Eval("ProductId")"
DataTextField="ProductName" HeaderText="名称" />
<asp:BoundField DataField="ProductId" HeaderText="编号" InsertVisible="False" ReadOnly="True"
SortExpression="ProductId" />
<asp:BoundField DataField="MarketPrice" HeaderText="市场价" SortExpression="MarketPrice" />
<asp:ImageField DataImageUrlField="pic" HeaderText="图片">
</asp:ImageField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:shopConnectionString %>"
SelectCommand="SELECT [ProductName], [ProductId], [MarketPrice], [UserPrice], [Pic] FROM [product]">
</asp:SqlDataSource> protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType ==DataControlRowType .DataRow )
{
if(e.Row .RowState ==DataControlRowState .Normal || e.Row .RowState ==DataControlRowState .Alternate )
{
HyperLink hyp = e.Row.Cells[0].Controls[0] as HyperLink;
TextBox txt = e.Row.Cells[1].Controls[0] as TextBox; //这行出错
if(hyp !=null && lab!=null)
{
hyp.NavigateUrl = "http://www.163.com";
hyp.Text = "我爱中华";
txt.Text = "我爱中华";
}
}
}
if(hyp !=null && lab!=null)
{
}是否应为Lable lab = e.Row.Cells[1].Controls[0] as Lable不确定if语句里面的lab是哪个变量
{
if(e.Row.RowType ==DataControlRowType .DataRow )
{
if(e.Row .RowState ==DataControlRowState .Normal || e.Row .RowState ==DataControlRowState .Alternate )
{
HyperLink hyp = e.Row.Cells[0].Controls[0] as HyperLink;
TextBox txt = e.Row.Cells[1].Controls[0] as TextBox; //这行出错
if(hyp !=null && txt!=null)
{
hyp.NavigateUrl = "http://www.163.com";
hyp.Text = "我爱中华";
txt.Text = "我爱中华";
}
}
}
大家帮忙看下,怎么老是找不到,郁闷好久了这个问题