在ASP.NET中GridView用存储过程删除 本帖最后由 qswsqsw 于 2012-12-19 16:53:41 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你是不知道怎么调用存储过程呢?还是不知道怎么在GRIDVIEW里写Delete事件呢?还是都不知道啊? GridView中添加OnCommand="gvDele_RowCommand"事件你的存储过程中传入的是两个参数不是吗?你想好到底只要一个参数还是要两个 如果要两个你可以在Gridview中加删除按钮但是删除按钮的数据绑定是CommandArgument='<%# Eval("BID"+","+"UID")%>' 在后台Command事件处理时自己分离两个参数,commd事件:protected void gvDele_RowCommand(object sender,GridViewCommandEventArgs e) { if (e.CommandName == "btnDele") { string s=e.CommandArgument.ToString().Trim(); //把s分离成uid bid Dele(uid,bid);就可以了后面最好重新绑定你的GRIDVIEW } }存储过程调用很简单给你一个模板好了public static void Dele(v_UID,v_BID) { SqlConnection con = new SqlConnection(_connectionString); SqlCommand comm = new SqlCommand("SNS_Item_Photo_Ins", con); comm.CommandType = CommandType.StoredProcedure; comm.Parameters.Add("@UID", SqlDbType.NVarChar, 50).Value = v_UID; comm.Parameters.Add("@BID", SqlDbType.NVarChar, 50).Value = v_BID; try { con.Open(); comm.ExecuteNonQuery(); } catch (Exception ex) { ***自定义内容 最好直接抛出ex.Message内容 } finally { con.Close(); } } _connectionString你的数据库连接字符串,"SNS_Item_Photo_Ins" 是你的存储过程的名字 嗯,很感谢但是请问。。怎么在GridView中添加OnCommand="gvDele_RowCommand"事件 页面怎么添加属性就怎么添加OnCommand="gvDele_RowCommand"比如<asp:GridView runat="server" ID="gv1" OnCommand="gvDele_RowCommand" 样式还有其他的属性我就不写了>~~~列你自己定义吧~~</asp:GridView> 还是写个样板给你吧:页面<asp:GridView ID="gvphoto" runat="server" AutoGenerateColumns="False" GridLines="Horizontal" Width="100%" CellPadding="0" CellSpacing="0" OnRowCommand="gvDelete_rowCommand"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:ImageButton ID="ibtnDelete" runat="server" CommandArgument='<%# Eval("BID"+","+"UID")%>' CommandName="DataDelete" Width="24px" Height="24px" ImageUrl="~~图片地址" ToolTip="刪除" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="UID" HeaderText="UID"> <HeaderStyle Wrap="False" /> <ItemStyle Wrap="False" /> </asp:BoundField> <asp:BoundField DataField="BID" HeaderText="BID"> <HeaderStyle Wrap="False" /> <ItemStyle Wrap="False" /> </asp:BoundField> <asp:BoundField DataField="UID " HeaderText="UID"> <HeaderStyle Wrap="False" /> <ItemStyle Wrap="False" /> </asp:BoundField> </Columns></GridView>后台:protected void gvDele_RowCommand(object sender,GridViewCommandEventArgs e) { if (e.CommandName == "btnDele") { string s=e.CommandArgument.ToString().Trim(); //把s分离成uid bid Dele(uid,bid);就可以了后面最好重新绑定你的GRIDVIEW } }public static void Dele(v_UID,v_BID) { SqlConnection con = new SqlConnection(_connectionString); SqlCommand comm = new SqlCommand("SNS_Item_Photo_Ins", con); comm.CommandType = CommandType.StoredProcedure; comm.Parameters.Add("@UID", SqlDbType.NVarChar, 50).Value = v_UID; comm.Parameters.Add("@BID", SqlDbType.NVarChar, 50).Value = v_BID; try { con.Open(); comm.ExecuteNonQuery(); } catch (Exception ex) { ***自定义内容 最好直接抛出ex.Message内容 也可以直接RETURN最好知道异常出现的原因 } finally { con.Close(); } } <asp:BoundField DataField="UID " HeaderText="UID"> <HeaderStyle Wrap="False" /> <ItemStyle Wrap="False" /> </asp:BoundField>这个写多了 只要一个就行了 问一个office二次开发的问题 自己写的音乐播放器总是出现问题,急求解。 用C#实现客户端生成服务端的功能 reportview 打印颠倒 求一字符串分割方法 求大神帮帮我…… 紧急求救,C#中调用聚合接口? DataAdapter的Update()不支持控制并发更新吗 c#ADO数据库引用问题 c#ae二次开发怎么通过checkbox与图层绑定 datagrid添加button列 C#连接ACCESS库错误,未发现数据源名称并且未指定默认驱动程序。
还是不知道怎么在GRIDVIEW里写Delete事件呢?
还是都不知道啊?
你的存储过程中传入的是两个参数不是吗?你想好到底只要一个参数还是要两个 如果要两个你可以在Gridview中加删除按钮但是删除按钮的数据绑定是CommandArgument='<%# Eval("BID"+","+"UID")%>' 在后台Command事件处理时自己分离两个参数,
commd事件:
protected void gvDele_RowCommand(object sender,GridViewCommandEventArgs e)
{
if (e.CommandName == "btnDele")
{
string s=e.CommandArgument.ToString().Trim();
//把s分离成uid bid
Dele(uid,bid);就可以了后面最好重新绑定你的GRIDVIEW
}
}存储过程调用很简单给你一个模板好了
public static void Dele(v_UID,v_BID)
{
SqlConnection con = new SqlConnection(_connectionString);
SqlCommand comm = new SqlCommand("SNS_Item_Photo_Ins", con);
comm.CommandType = CommandType.StoredProcedure;
comm.Parameters.Add("@UID", SqlDbType.NVarChar, 50).Value = v_UID;
comm.Parameters.Add("@BID", SqlDbType.NVarChar, 50).Value = v_BID;
try
{
con.Open();
comm.ExecuteNonQuery();
}
catch (Exception ex)
{
***自定义内容 最好直接抛出ex.Message内容
}
finally
{
con.Close();
}
}
<asp:GridView ID="gvphoto" runat="server" AutoGenerateColumns="False" GridLines="Horizontal" Width="100%" CellPadding="0" CellSpacing="0" OnRowCommand="gvDelete_rowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ibtnDelete" runat="server" CommandArgument='<%# Eval("BID"+","+"UID")%>' CommandName="DataDelete" Width="24px" Height="24px" ImageUrl="~~图片地址" ToolTip="刪除" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UID" HeaderText="UID">
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="BID" HeaderText="BID">
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="UID " HeaderText="UID">
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
</Columns>
</GridView>
后台:
protected void gvDele_RowCommand(object sender,GridViewCommandEventArgs e)
{
if (e.CommandName == "btnDele")
{
string s=e.CommandArgument.ToString().Trim();
//把s分离成uid bid
Dele(uid,bid);就可以了后面最好重新绑定你的GRIDVIEW
}
}
public static void Dele(v_UID,v_BID)
{
SqlConnection con = new SqlConnection(_connectionString);
SqlCommand comm = new SqlCommand("SNS_Item_Photo_Ins", con);
comm.CommandType = CommandType.StoredProcedure;
comm.Parameters.Add("@UID", SqlDbType.NVarChar, 50).Value = v_UID;
comm.Parameters.Add("@BID", SqlDbType.NVarChar, 50).Value = v_BID;
try
{
con.Open();
comm.ExecuteNonQuery();
}
catch (Exception ex)
{
***自定义内容 最好直接抛出ex.Message内容
也可以直接RETURN最好知道异常出现的原因
}
finally
{
con.Close();
}
}
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
这个写多了 只要一个就行了