我有一个网格视图,我需要使用RowUpdating事件更新它,但是在更新后没有出现新值,数据库更新时使用旧值。 这是我的代码
protected void gvContactInfo_RowEditing(object sender, GridViewEditEventArgs e)
{
gvContactInfo.EditIndex = e.NewEditIndex;
bindingGVContacts(int.Parse(ddlfilterforContact.SelectedValue.ToString()));
}
protected void gvContactInfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label lbl = ((Label)gvContactInfo.Rows[e.RowIndex].FindControl("lblContactidno"));
DropDownList ddl = ((DropDownList)gvContactInfo.Rows[e.RowIndex].FindControl("ddlInfoType"));
TextBox txtinfo = ((TextBox)gvContactInfo.Rows[e.RowIndex].FindControl("txtValueE"));
TextBox txtext = ((TextBox)gvContactInfo.Rows[e.RowIndex].FindControl("txtExt"));
string queryContactInfo = "update tblContactInfo set ContactInfoType='"+ddl.SelectedItem.Text+"',ContactInfo='"+txtinfo.Text+"',Ext='"+txtext.Text+"' where ContactID=" + int.Parse(lbl.Text.Trim()) + "";
Connection = new SqlConnection(ConnString);
Connection.Open();
SqlCommand cmd = new SqlCommand(queryContactInfo, Connection);
cmd.ExecuteNonQuery();
Connection.Close();
gvContactInfo.EditIndex = -1;
bindingGVContacts(int.Parse(ddlfilterforContact.SelectedValue.ToString()));
}
protected void gvContactInfo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvContactInfo.EditIndex = -1;
bindingGVContacts(int.Parse(ddlfilterforContact.SelectedValue.ToString()));
}
我的数据绑定代码如下:
public void bindingGVContacts(int contactID)
{
int contactID1 = contactID;
string queryContactInfo = "SELECT * FROM tblContactInfo where ContactID=" + contactID1 + "";
Connection = new SqlConnection(ConnString);
Connection.Open();
ds = new DataSet();
DataTable dt = new DataTable();
ad = new SqlDataAdapter(queryContactInfo, ConnString);
ad.Fill(ds, "queryContactInfo");
ad.Fill(dt);
Connection.Close();
if (ds.Tables["queryContactInfo"].Rows.Count > 0)
{
gvContactInfo.Columns[0].Visible = true;
gvContactInfo.DataSource = ds.Tables["queryContactInfo"];
gvContactInfo.DataBind();
gvContactInfo.Columns[0].Visible = false;
foreach (GridViewRow grow in gvContactInfo.Rows)
{
Label lbl = ((Label)grow.FindControl("lblContactidno"));
DropDownList ddl = ((DropDownList)grow.FindControl("ddlInfoType"));
DataRow[] dr = dt.Select("ContactNoID=" + lbl.Text.Trim() + "");
if (dr.Length != 0)
{
ddl.SelectedItem.Selected = false;
if (ddl.Items.FindByText(dr[0]["ContactInfoType"].ToString()) != null)
ddl.Items.FindByText(dr[0]["ContactInfoType"].ToString()).Selected = true;
}
}
}
else
{
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
gvContactInfo.DataSource = dt;
gvContactInfo.DataBind();
gvContactInfo.Rows[0].Visible = false;
}
}
这是我的gridview的aspx代码:
<asp:GridView runat="server" ID="gvContactInfo" ShowHeader="true" ShowHeaderWhenEmpty="true" Enableviewstate="true"
AutoGenerateColumns="false" ShowFooter="true" OnRowEditing="gvContactInfo_RowEditing" OnRowUpdating="gvContactInfo_RowUpdating" OnRowCancelingEdit="gvContactInfo_RowCancelingEdit" OnRowCommand="gvContactInfo_RowCommand"
CssClass=" CategoriesTable table table-striped table-bordered CategoriesTable1"
onrowdatabound="gvContactInfo_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Value" ItemStyle-Width="5%">
<ItemTemplate>
<asp:Label ID="lblContactidno" runat="server" Text='<%#Eval("ContactNoID")%>' Font-Bold="true"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="INFO Type" ItemStyle-Width="5%">
<ItemTemplate>
<asp:DropDownList ID="ddlInfoType" runat="server">
<asp:ListItem Value="Address" Text="Address"></asp:ListItem>
<asp:ListItem Value="Email-Personal" Text="Email-Personal"></asp:ListItem>
<asp:ListItem Value="Email-Work" Text="Email-Work"></asp:ListItem>
<asp:ListItem Value="Phone-Home" Text="Phone-Home"></asp:ListItem>
<asp:ListItem Value="Phone-Work" Text="Phone-Work"></asp:ListItem>
<asp:ListItem Value="Phone-Mobile" Text="Phone-Mobile"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlInfoType" runat="server">
<asp:ListItem Value="Address" Text="Address"></asp:ListItem>
<asp:ListItem Value="Email-Personal" Text="Email-Personal"></asp:ListItem>
<asp:ListItem Value="Email-Work" Text="Email-Work"></asp:ListItem>
<asp:ListItem Value="Phone-Home" Text="Phone-Home"></asp:ListItem>
<asp:ListItem Value="Phone-Work" Text="Phone-Work"></asp:ListItem>
<asp:ListItem Value="Phone-Mobile" Text="Phone-Mobile"></asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Value" ItemStyle-Width="5%">
<ItemTemplate>
<asp:TextBox ID="txtValue" runat="server" Text='<%#Eval("ContactInfo")%>'></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtValue" runat="server" Text=""></asp:TextBox>
</FooterTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtValueE" runat="server" Text='<%#Eval("ContactInfo")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Extension" ItemStyle-Width="5%">
<ItemTemplate>
<asp:TextBox ID="txtExtension" runat="server" Text='<%#Eval("Ext")%>'></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtExtension1" runat="server" Text=""></asp:TextBox>
</FooterTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtExt" runat="server" Text='<%#Eval("Ext")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action" ItemStyle-Width="5%">
<ItemTemplate>
<asp:LinkButton runat="server" CommandName="Edit" CausesValidation="false">Edit</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton runat="server" CommandName="Update" CausesValidation="false">Update</asp:LinkButton>
<asp:LinkButton runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:Button runat="server" Text="ADD" CommandName="Insert"></asp:Button>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
请给我一个解决方案。
答案 0 :(得分:0)
设置调试器并检查是否将正确的(新)值传递给sql查询字符串,并使用try-catch块在更新时捕获任何异常。
另外,使用string构建sql查询不是一个好主意。您应该使用Parameter
来阻止SQL注入。
这个example显示了SQL注入会发生什么。
这个example显示了如何使用Parameter
阻止C#中的SQL注入
答案 1 :(得分:0)
检查您是否使用断点在gvContactInfo_RowUpdating更改事件中获取新值
答案 2 :(得分:0)
而不是使用FindControl选项直接读取旧值,例如
Label lbl = ((Label)gvContactInfo.Rows[e.RowIndex].FindControl("lblContactidno"));
您应该使用GridViewUpdateEventArgs.NewValues Property属性将所有新值作为键/值对。
string lblStr = e.NewValues[0].ToString(); //lblContactidno
修改
您正在使用Row_Updating事件中的FindControl方法读取控件的现有值。问题是您的新值尚未更新,并且正在执行此过程中。因此,它剔除旧的价值观。旧的和新值存储在事件 GridViewUpdateEventArgs 中作为键/值对。所以你必须从那里[ NewValues属性]获取新值。我在这里建议的代码是仅读取标签的值。检查您是否为其获取新值,因为它基于标签是网格中第一个控件的假设。
答案 3 :(得分:0)
我有另一种方法来更新,即在RowCommand方法的帮助下
protected void gvContactInfo_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Update"))
{
GridViewRow gvr = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
int RowIndex = gvr.RowIndex;
Label lbl = ((Label)gvContactInfo.Rows[RowIndex].FindControl("lblContactidno"));
DropDownList ddl = ((DropDownList)gvContactInfo.Rows[RowIndex].FindControl("ddlInfoType"));
TextBox txtinfo = ((TextBox)gvContactInfo.Rows[RowIndex].FindControl("txtValueE"));
TextBox txtext = ((TextBox)gvContactInfo.Rows[RowIndex].FindControl("txtExt"));
string queryContactInfo = "update tblContactInfo set ContactInfoType='" + ddl.SelectedItem.Text + "',ContactInfo='" + txtinfo.Text + "',Ext='" + txtext.Text + "' where ContactNoID=" + int.Parse(lbl.Text.Trim()) + "";
Connection = new SqlConnection(ConnString);
Connection.Open();
SqlCommand cmd = new SqlCommand(queryContactInfo, Connection);
cmd.ExecuteNonQuery();
Connection.Close();
gvContactInfo.EditIndex = -1;
}
}