【问题标题】:How to pass multiple values through command argument in Asp.net?如何通过 Asp.net 中的命令参数传递多个值?
【发布时间】:2012-04-07 01:07:06
【问题描述】:

我有具有多个 Eval 值的 CommandArgument 属性的 ImageButton。当我单击其中一个时,我想将值传递给 ImageButton2_Click 事件,但它不起作用,因为 Command 参数为空。

<div class="sag-re-icerik" id="icerik2" runat="server">Lorem ipsum dolor sit amet, consectetur commodo et convallis et, auctor viverra metus. Aenean pharetra, arcu nec viverra mollis, turpis neque feugiat massa, non dapibus neque nunc ac orci. </div>
    <div class="oy-verme">
        <div class="yildiz"><asp:ImageButton ID="ImageButton4" runat="server" Height="19px" ImageUrl="~/images/yildiz.png" onclick="ImageButton2_Click" Width="20px"  style="position: relative; top: 13px; left:6px; float:left;  "    commandArgument='<%#Eval("sdasdas") + ","+Eval("fafasfa") %>' /></div>
        <div class="yildiz"><asp:ImageButton ID="ImageButton5" runat="server" Height="19px" ImageUrl="~/images/yildiz.png" onclick="ImageButton5_Click" Width="20px"  style="position: relative; top: 13px; left:8px; float:left;" commandArgument='<%#Eval("row[0].ToString()") + ","+Eval("row[1].ToString()") %>'/></div>
        <div class="yildiz"><asp:ImageButton ID="ImageButton6" runat="server" Height="19px" ImageUrl="~/images/yildiz.png" onclick="ImageButton2_Click" Width="20px"  style="position: relative; top: 13px; left:10px ; float:left; " commandArgument='<%#Eval("row[0].ToString()") + ","+Eval("row[1].ToString()") %>' /></div>
        <div class="yildiz"><asp:ImageButton ID="ImageButton3" runat="server" Height="19px"  ImageUrl="~/images/yildiz.png" onclick="ImageButton2_Click" Width="20px" style="position: relative; top:13px; left:12px ; float:left;" commandArgument='<%#Eval("row[0].ToString()") + ","+Eval("row[1].ToString()") %>' /></div>
        <div class="yildiz"> <asp:ImageButton ID="ImageButton2" runat="server" Height="19px" ImageUrl="~/images/yildiz.png" onclick="ImageButton2_Click" Width="20px"  style="position: relative; top: 13px; left: 14px; float:left;" commandArgument='<%#Eval("row[0].ToString()") + ","+Eval("row[1].ToString()") %>' /></div>
        <div class="oy-sil"><img src="images/oy-sil.png" width="11" height="13" style="position: relative; top: 30px; " /></div>
    </div>
</div>

这是代码隐藏:

protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
    ImageButton objImage = (ImageButton)sender;

    string[] commandArgs = objImage.CommandArgument.ToString().Split(new char[] { ',' });
    string id = commandArgs[0];
    string text = commandArgs[1];


    //  string s= Imageid.UniqueID.ToString();
    //this.baslik2.Text = s;
}

【问题讨论】:

标签: c# asp.net .net imagebutton commandargument


【解决方案1】:
CommandArgument='<%#Eval("ScrapId").Tostring()+ Eval("UserId")%>
//added the comment function

【讨论】:

    【解决方案2】:

    你可以试试这个:

    CommandArgument='<%# "scrapid=" + Eval("ScrapId")+"&"+"UserId="+ Eval("UserId")%>'
    

    【讨论】:

      【解决方案3】:

      使用 imagebutton 的 OnCommand 事件。在里面做

      <asp:Button id="Button1" Text="Click" CommandName="Something" CommandArgument="your command arg" OnCommand="CommandBtn_Click" runat="server"/>
      

      代码隐藏:

      void CommandBtn_Click(Object sender, CommandEventArgs e) 
      {    
          switch(e.CommandName)
          {
              case "Something":
                  // Do your code
                  break;
              default:              
                  break; 
      
          }
      }
      

      【讨论】:

      • 如果您要复制代码或答案,请给 MSDN 赞一个赞!
      • 那么,@leventkalay92 你明白如何通过命令参数传递多个值了吗?这是一个很好的参考msdn.microsoft.com/en-us/library/…
      【解决方案4】:

      我检查了您的代码,似乎完全没有问题。请确保 Image commandArgument 获得价值。首先在标签中检查它是否获得价值。

      但是,这是我在项目中使用的示例

      <asp:GridView ID="GridViewUserScraps" ItemStyle-VerticalAlign="Top" AutoGenerateColumns="False" Width="100%" runat="server" OnRowCommand="GridViews_RowCommand" >
              <Columns>
                  <asp:TemplateField SortExpression="SendDate">
                      <ItemTemplate>
                      <asp:Button ID="btnPost" CssClass="submitButton" Text="Comment" runat="server" CommandName="Comment" CommandArgument='<%#Eval("ScrapId")+","+ Eval("UserId")%>' />
      
                      </ItemTemplate>
                  </asp:TemplateField>
              </Columns>
          </asp:GridView>
      

      首先绑定GridView。

      public void GetData()
      {
         //bind ur GridView
         GridViewUserScraps.DataSource = dt;
         GridViewUserScraps.DataBind();
      }
      
      protected void GridViews_RowCommand(object sender, GridViewCommandEventArgs e)
      {
          if (e.CommandName == "Comment")
          {
              string[] commandArgs = e.CommandArgument.ToString().Split(new char[] { ',' });
              string scrapid = commandArgs[0];
              string uid = commandArgs[1];
          }
      }
      

      【讨论】:

      • 我没有使用 gridview 并且 e.CommandArgument 给出了错误,即 clickeventargs 不包含命令参数的定义
      • 我刚刚向您展示了在通过 CommandArgument 获取值之前必须绑定 gridview 的示例。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-12
      • 2012-11-27
      • 2013-03-10
      • 2017-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多