从网格视图捕捉多个值传递给存储过程 [英] capture multiple values from grid view to pass to a stored procedure

查看:141
本文介绍了从网格视图捕捉多个值传递给存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

asp.net和C#

asp.net and C#

在这里我有一个GridView控件中的列

here I have the columns from a gridview control

当用户点击其中一个按钮,我需要为CaseID,CaseworkerID,EVENTDATE和code中的值传递到存储过程

when the user clicks either button i need to pass the values for CaseID, CaseworkerID, EventDate and Code to a stored procedure

但我怎么多个参数传递给我的onclick方法????

but how do i pass multiple parameters to my onclick method ????

<columns>
<asp:BoundField DataField="CaseID" HeaderText="CaseID" Visible = "False" />
<asp:BoundField DataField="caseworkerID" HeaderText="CwID" Visible = "False" />
<asp:BoundField DataField="CaseWorker" HeaderText="Case Worker" />
<asp:BoundField DataField="EventDate" HeaderText="Event Date" />
<asp:BoundField DataField="Code" HeaderText="Code" />
  <asp:TemplateField HeaderText="ADD">
       <ItemTemplate> 
                    <asp:Button  ID="AddUnit" runat="server" Text=" +1 " onserverclick="AddUnit_Click"/>
        </ItemTemplate> 
   </asp:TemplateField> 

<asp:BoundField DataField="TotalUnits" HeaderText="Total Units" />

  <asp:TemplateField HeaderText="DDT">
       <ItemTemplate> 
          <asp:Button ID="DdtUnit" runat="server" Text=" -1 " onserverclick="DdtUnit_Click"/>
        </ItemTemplate> 
   </asp:TemplateField> 


<asp:BoundField DataField="EventType" HeaderText="Event Type" />
<asp:BoundField DataField="UnitCost" HeaderText="Unit Cost" />
<asp:BoundField DataField="TotalCost" HeaderText="Total Cost"/>

编辑:
所以我现在有这个

so I now have this

            <ItemTemplate> 
                <asp:Button  ID="AddUnit" runat="server" Text=" +1 " 
                CommandName="AddUnit" 
                CommandArgument='<%# Eval("CaseID")+ ";" + Eval("caseworkerID")+ ";" + Eval("EventDate")+ ";" + Eval("Code")%>'/>
            </ItemTemplate> 

这就是一个良好的开端。

thats a good start

推荐答案

pretty多,你想要做的就是创建一个特殊字符(S)deliminated一个commandargument多数民众赞成。当您处理该事件,拔出从事件的命令参数(e)和分裂它。 (由于utkarshs回答显示,使用GridView的rowcommand事件,而不是一个onclick)

Pretty much what you want to do is create a commandargument thats deliminated by a special character(s). When you handle the event, pull out the command argument from the event (e) and split it. (As utkarshs answer shows, use the gridviews rowcommand event, not an onclick)

这可能会导致问题如果commandargument数据实际上包含您用于deliminator相同的字符。只是一个建议,但(我们这样做有时当我们知道像产品标识成才将不包含管道(|))

This can cause issues if the commandargument data actually contains the same character you used for a deliminator. Just one suggestion though (we do this sometimes when we know someting like a productId won't contain a pipe (|) )

编辑:
所以,你的按钮应该是这样...

So your button should be something like...

  <asp:button CommandArgument='<%# Eval("someValue") + " | " 
              + Eval("someOtherValue") + " | " %>'  
             CommandName="whatever" runat="server" text="FOO BAR" />

然后在后面,其中网格id是你的code gridId

protected void gridId_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "btnname")
    {
        string[] differentValues = e.CommandArgument.ToString().Split("|");           
    }
}

这篇关于从网格视图捕捉多个值传递给存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆