如何通过Asp.net中的命令参数传递多个值? [英] How to pass multiple values through command argument in Asp.net?

查看:123
本文介绍了如何通过Asp.net中的命令参数传递多个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have ImageButton with CommandArgument attribute which is having multiple Eval value. When I click one of them I want to pass values to ImageButton2_Click event but it does not work because Command arguments is null.

<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;
}

推荐答案

我检查了您的代码,似乎一点问题都没有.请确保Image commandArgument获得价值.首先检查它是否与标签绑定,是否有价值.

I checked your code and seems to be no problem at all. please make sure Image commandArgument getting value. check it first binding in label whether you are getting value.

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

However, here is sample which I'm using in my project

<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.

first bind the 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];
    }
}

这篇关于如何通过Asp.net中的命令参数传递多个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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