<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="t_id"
DataSourceID="SqlDataSource1" Width="305px" OnRowUpdated="GridView1_RowUpdated">
<Columns>
<asp:BoundField DataField="t_id" HeaderText="t_id" InsertVisible="False" ReadOnly="True"
SortExpression="t_id" />
<asp:BoundField DataField="t_name" HeaderText="t_name" SortExpression="t_name" />
<%-- <asp:BoundField DataField="t_age" HeaderText="t_age" SortExpression="t_age" />--%>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblAge" runat="server" Text='<%# Eval("t_age").ToString() %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate >
<asp:TextBox id="txtAge" runat="server" Text='<%# Eval("t_age").ToString() %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
<br />
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT * FROM [student]" UpdateCommand="UPDATE student SET t_name = @t_name, t_age = @t_age WHERE (t_id = @t_id)">
<UpdateParameters>
<asp:Parameter Name="t_name" />
<asp:ControlParameter ControlID="GridView1" Name="t_age" PropertyName="Controls" />
<asp:Parameter Name="t_id" />
</UpdateParameters>
</asp:SqlDataSource>
有一个数据绑定列age 我想用数据源来进行更新。其实是很方便的。但是我不知道象数据绑定后的列如何与updateParameters 的参数相关连, 应该是ControlParameter,但这只能绑定页面中的控件,如在gridview内的控件就不知如何是好。 protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
SqlDataSource1.UpdateParameters.Add(new Parameter("@t_age", TypeCode.Int32, ((TextBox)GridView1.FindControl("txtAge")).Text));
}触发这个事件。对@t_age进行设置,但在运行中报错,说没有实例化。
请教这样应该如何做?
DataSourceID="SqlDataSource1" Width="305px" OnRowUpdated="GridView1_RowUpdated">
<Columns>
<asp:BoundField DataField="t_id" HeaderText="t_id" InsertVisible="False" ReadOnly="True"
SortExpression="t_id" />
<asp:BoundField DataField="t_name" HeaderText="t_name" SortExpression="t_name" />
<%-- <asp:BoundField DataField="t_age" HeaderText="t_age" SortExpression="t_age" />--%>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblAge" runat="server" Text='<%# Eval("t_age").ToString() %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate >
<asp:TextBox id="txtAge" runat="server" Text='<%# Eval("t_age").ToString() %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
<br />
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT * FROM [student]" UpdateCommand="UPDATE student SET t_name = @t_name, t_age = @t_age WHERE (t_id = @t_id)">
<UpdateParameters>
<asp:Parameter Name="t_name" />
<asp:ControlParameter ControlID="GridView1" Name="t_age" PropertyName="Controls" />
<asp:Parameter Name="t_id" />
</UpdateParameters>
</asp:SqlDataSource>
有一个数据绑定列age 我想用数据源来进行更新。其实是很方便的。但是我不知道象数据绑定后的列如何与updateParameters 的参数相关连, 应该是ControlParameter,但这只能绑定页面中的控件,如在gridview内的控件就不知如何是好。 protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
SqlDataSource1.UpdateParameters.Add(new Parameter("@t_age", TypeCode.Int32, ((TextBox)GridView1.FindControl("txtAge")).Text));
}触发这个事件。对@t_age进行设置,但在运行中报错,说没有实例化。
请教这样应该如何做?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货