如何获得绑定列值,还是我完全错了? [英] How to get boundfield value, or am I totally wrong?
问题描述
我follwing 本教程中, 他使用模板列,然后把文本框在里面,显示数据,这样,
<&ItemTemplate中GT;
< ASP:标签ID =lblitemUsr=服务器文本='<%#的eval(用户名)%>/>
< / ItemTemplate中>
< FooterTemplate>
< ASP:文本框ID =txtftrusrname=服务器/>
< ASP:的RequiredFieldValidator ID =rfvusername=服务器的ControlToValidate =txtftrusrname文本=*的ValidationGroup =validaiton/>
< / FooterTemplate>
< / ASP:的TemplateField>
然后同时更新的GridView他是这样做的,
CMD的SqlCommand =新的SqlCommand(更新Employee_Details设置城市='+ txtcity.Text +',名称='+ txtDesignation.Text +'其中userid =+用户ID,CON);
而我在做什么是,
< ASP:BoundField的数据字段=username的的HeaderText =用户ItemStyle-WIDTH =120像素/>
现在我怎样才能绑定列值,而更新的SQL语句,因为没有txtbox?
我的GridView的声明中,
< ASP:GridView控件ID =GridView1=服务器的AutoGenerateColumns =假
CELLPADDING =5OnRowDataBound =GridView1_RowDataBoundWIDTH =800像素AllowPaging =真
每页=5的网格线=水平OnPageIndexChanging =GridView1_PageIndexChanging
OnRowCancelingEdit =GridView1_RowCancelingEditOnRowDeleting =GridView1_RowDeleting
OnRowUpdating =GridView1_RowUpdatingOnRowEditing =GridView1_RowEditing>
您忘了设定的最重要的事情的DataKeyNames
在< ASP :GridView的>< / ASP:GridView的>
声明,首先添加,然后,
您可以尝试这样的:
保护无效GridView1_RowUpdating(对象发件人,GridViewUpdateEventArgs E)
{ INT用户id = Convert.ToInt32(GridView1.DataKeys [e.RowIndex] [将把userid]);
串市=((文本框)(GridView1.Rows [e.RowIndex] .Cells [2] .Controls [0]))Text.Trim();
串指定=((文本框)(GridView1.Rows [e.RowIndex] .Cells [3] .Controls [0]))Text.Trim(); CMD的SqlCommand =新的SqlCommand(更新Employee_Details设置城市='+城市+',名称='+名称+'其中userid =用户ID +,CON);
//
//其他code片段
//
GridView1.EditIndex = -1;
BindGridview(); }
您必须提供细胞[N]
您在code后面想细胞值。
I am follwing this tutorial, he is using templatefield and then putting textbox in it for displaying data, like this,
<ItemTemplate>
<asp:Label ID="lblitemUsr" runat="server" Text='<%#Eval("UserName") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtftrusrname" runat="server"/>
<asp:RequiredFieldValidator ID="rfvusername" runat="server" ControlToValidate="txtftrusrname" Text="*" ValidationGroup="validaiton"/>
</FooterTemplate>
</asp:TemplateField>
then while updating gridview he is doing this,
SqlCommand cmd = new SqlCommand("update Employee_Details set City='" + txtcity.Text + "',Designation='" + txtDesignation.Text + "' where UserId=" + userid, con);
whereas what I am doing is,
<asp:BoundField DataField="userName" HeaderText="User" ItemStyle-Width="120px" />
Now how can I get boundfield value while updating SQL statement as there is no txtbox ?
My GridView declaration,
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
CellPadding="5" OnRowDataBound="GridView1_RowDataBound" Width="800px" AllowPaging="True"
PageSize="5" GridLines="Horizontal" OnPageIndexChanging="GridView1_PageIndexChanging"
OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting"
OnRowUpdating="GridView1_RowUpdating" OnRowEditing="GridView1_RowEditing" >
You forgot to set the most important thing DataKeyNames
in your <asp:Gridview></asp:GridView>
declaration, first add that and then,
You may try like this:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int userid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex]["userid"]);
string City= ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.Trim();
string Designation = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.Trim();
SqlCommand cmd = new SqlCommand("update Employee_Details set City='" + City + "',Designation='" + Designation + "' where UserId=" + userid, con);
//
// other Code snippets
//
GridView1.EditIndex = -1;
BindGridview();
}
You will have to provide cell[n]
values of cell that you want in code behind.
这篇关于如何获得绑定列值,还是我完全错了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!