如何得到边界场值,或者我完全错了
本文关键字:或者 错了 何得 边界 | 更新日期: 2023-09-27 18:11:35
我遵循本教程,他使用templatefield然后在其中放置文本框来显示数据,像这样,
<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>
然后在更新gridview时,他正在做这个,
SqlCommand cmd = new SqlCommand("update Employee_Details set City='" + txtcity.Text + "',Designation='" + txtDesignation.Text + "' where UserId=" + userid, con);
而我现在做的是,
<asp:BoundField DataField="userName" HeaderText="User" ItemStyle-Width="120px" />
现在我怎么能得到boundfield值,而更新SQL语句,因为没有txtbox ?
我的GridView声明
<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" >
如果您想使用BoundFields而不是TemplateFields,您可以通过您的GridView1_RowUpdating
事件处理程序找到用户输入的值。它有一个类型为GridViewUpdateEventArgs
的参数。您所要做的就是循环遍历"NewValues"集合以获取需要更新的列的值。
另外,永远不要将用户输入直接连接到SQL字符串中。这是一个众所周知的被称为"SQL注入"的恶意攻击的漏洞。而不是用户参数(我在下面的例子中包含了它们)。
下面是一个例子:
protected void GridView1_RowUpdating(Object sender, GridViewUpdateEventArgs e)
{
string city = "";
string designation = "";
foreach (DictionaryEntry entry in e.NewValues)
{
if(entry.Key == "City")
{
city = entry.Value.ToString();
}
if(entry.Key == "Designation")
{
designation = entry.Value.ToString();
}
}
SqlCommand cmd = new SqlCommand("update Employee_Details set City=@City, Designation=@Designation where UserId=@UserId", con);
cmd.Parameters.Add("@City", SqlDbType.NVarChar);
cmd.Parameters.Add("@Designation", SqlDbType.NVarChar);
cmd.Parameters.Add("@UserId", SqlDbType.NVarChar);
cmd.Paramters["@City"].Value = city;
cmd.Paramters["@Designation"].Value = designation;
cmd.Paramters["@UserId"].Value = userid;
}
如果要使用SqlDataSource和GridView,那么这些代码都是不必要的。数据源控件将处理所有更新逻辑。您只需要包含一个带有参数的更新命令:
<asp:SqlDataSource ID="myDataSource" runat="server"
SelectCommand="Your select statement goes here"
UpdateCommand="update Employee_Details set City=@City, Designation=@Designation where UserId=@UserId" />
请注意,您需要以与BoundFields的"DataField"属性相同的方式命名参数。所以上面你有<asp:BoundField DataField="userName"...
,然后你想在你的UpdateCommand字段使用"@userName"
您忘记在<asp:Gridview></asp:GridView>
声明中设置最重要的DataKeyNames
,首先添加它,然后,
你可以这样尝试:
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();
}
您必须在后面的代码中提供单元格的cell[n]
值。