不删除gridview中的任何记录 [英] Not delete any record in gridview
本文介绍了不删除gridview中的任何记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
mycode
gridview
mycode
gridview
<asp:GridView ID="SentBoxGridView" CssClass="footable" Width="100%" runat="server" AutoGenerateColumns="False" onrowdatabound="SentBoxGridView_RowDataBound"
onpageindexchanging="SentBoxGridView_PageIndexChanging" PageSize="1" OnRowDeleting="SentBoxGridView_RowDeleting">
<Columns>
<asp:TemplateField>
<ItemTemplate >
<asp:LinkButton ID="lbtnreply" runat="server" onclick="lbtnreply_Click"
ValidationGroup='<%# Eval("MessageId") %>' OnClientClick="document.getElementById('form1').target ='_self';">Reply</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible=false>
<ItemTemplate>
<asp:Label id="lblmessageid" runat ="server" text='<%# Eval("MessageId")%>'></asp:Label>
<asp:Label id="lblAddressId" runat ="server" text='<%# Eval("MessageStatusId")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="FromName" HeaderText="From"
SortExpression="FromName"/>
<asp:BoundField DataField="MessageBody" HeaderText="Message"
SortExpression="MessageBody" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
代码隐藏文件
codebehind file
protected void SentBoxGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = SentBoxGridView.Rows[e.RowIndex];
TextBox MessageId = (TextBox)row.FindControl("MessageId");
SqlConnection con;
con = new SqlConnection(ConfigurationManager.ConnectionStrings["SAVARIConnectionString5"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("delete from Messages where MessageId='" + MessageId + "'", con);
cmd.ExecuteNonQuery();
con.Close();
}
我的尝试:
没有任何错误发生messageId null传递
What I have tried:
not any error occur messageId null passed
推荐答案
我认为这应该是因为你有一个ID为<$ c $的Label c> lblmessageid ...
I think that should be as you have a Label with IDlblmessageid
...
TextBox MessageId = (TextBox)row.FindControl("lblmessageid");
还使用参数化查询而不是纯文本查询来避免SQL注入。
Also use parameterized query instead of plain text query to avoid SQL Injection.
这篇关于不删除gridview中的任何记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文