GridVieew中更新数据库数据的问题

AspnetDreamer 2011-11-05 03:03:27
我的GridView控件中什么控件也没放就是把数据库中的数据显示到控件中。当想更新数据库中数据时,用后代代码
string str1 = GridView1.Rows[e.RowIndex].Cells[0].Text.Trim();
string str2 = GridView1.Rows[e.RowIndex].Cells[1].Text.Trim();
string str3 = GridView1.Rows[e.RowIndex].Cells[2].Text.Trim();
string str4 = GridView1.Rows[e.RowIndex].Cells[3].Text.Trim();
DataBaseConnection connection1 = new DataBaseConnection();
connection1.DataUpdateRoomCategory(str1, str2, str3, str4);
GridView1.DataBind();

执行时为什么str1,str2,str3,str4的值都为空值??

这是前台代码:

<asp:GridView ID="GridView1" runat="server" BackColor="Gray"
BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4"
GridLines="Horizontal" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataSourceID="SqlDataSource2"
style="position:absolute; top: 12px; left: 13px; height: 260px; width: 786px;"
PageSize="6" DataKeyNames="RoomId" RowStyle-HorizontalAlign="Center"
onrowupdating="GridView1_RowUpdating">
<FooterStyle BackColor="White" ForeColor="#333333" />
<RowStyle BackColor="White" ForeColor="#333333" HorizontalAlign="Center" />
<Columns>
<asp:BoundField DataField="RoomId" HeaderText="Id" SortExpression="Id" >
<ItemStyle VerticalAlign="Middle" />
</asp:BoundField>
<asp:BoundField DataField="RoomPrice" HeaderText="房间价格" />
<asp:BoundField DataField="RoomBed" HeaderText="房间床位" />
<asp:BoundField DataField="RoomArea" HeaderText="房间面积" />
<asp:CommandField HeaderText="插入" ShowEditButton="True" />
<asp:TemplateField HeaderText="删除"><ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Delete" OnClientClick="return confirm('您真的要删除该记录吗?');">删除</asp:LinkButton></ItemTemplate></asp:TemplateField>
</Columns>
<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
</asp:GridView>

是什么原因当修改GridView中的数据时为什么str1,str2,str3,str4的值为空""值?????
...全文
69 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
AspnetDreamer 2011-11-05
  • 打赏
  • 举报
回复
谢谢4楼!不过当我修改数据进行点击更新时str1,str2,str3,str4的值依然是原来的值并不是我更新修改的值。请问这个怎么解决??
LONG332949572 2011-11-05
  • 打赏
  • 举报
回复
TextBox txtName = (TextBox)gvList.Rows[e.RowIndex].Cells[0].Controls[0];
TextBox txtM = (TextBox)gvList.Rows[e.RowIndex].Cells[1].Controls[0];
TextBox txtQ = (TextBox)gvList.Rows[e.RowIndex].Cells[2].Controls[0];
TextBox txtU = (TextBox)gvList.Rows[e.RowIndex].Cells[3].Controls[0];

string str1=txtName.text;
string str2=txtM .text;
string str3=txtQ .text;
string str4=txtU .text;

试下
  • 打赏
  • 举报
回复
一个TableCell的Text属性值跟它的Controls集合里的自控件的表现值是两回事。我不知道你如何要这样写代码。建议你找一个正规地一点的国外教程照着做一遍。
AspnetDreamer 2011-11-05
  • 打赏
  • 举报
回复
后台 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string str1 = GridView1.Rows[e.RowIndex].Cells[0].Text.ToString().Trim();
string str2 = GridView1.Rows[e.RowIndex].Cells[1].Text.ToString().Trim();
string str3 = GridView1.Rows[e.RowIndex].Cells[2].Text.ToString().Trim();
string str4 = GridView1.Rows[e.RowIndex].Cells[3].Text.ToString().Trim();
DataBaseConnection connection1 = new DataBaseConnection();
connection1.DataUpdateRoomCategory(str1, str2, str3, str4);
GridView1.DataBind();

}
更新事件中写的
lighwind 2011-11-05
  • 打赏
  • 举报
回复
string str1 = GridView1.Rows[e.RowIndex].Cells[0].Text.Trim();
string str2 = GridView1.Rows[e.RowIndex].Cells[1].Text.Trim();
这些代码在哪里写的?

62,051

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧