简体   繁体   English

如何通过按钮点击传递参数

[英]How to pass parameter through button click

I have a gridview with a databinding : all rows are generated depending on ItemSource.我有一个带有数据绑定的 gridview:所有行都是根据 ItemSource 生成的。 I have also add a column at the end containing a button.我还在最后添加了一个包含按钮的列。 How to bind a field from current itemSource, as a parameter of button click event?如何从当前 itemSource 绑定一个字段,作为按钮单击事件的参数?

Here is code sample :这是代码示例:

WEBFORM网络表格

<asp:GridView ID="GridView1" runat="server" 
    AutoGenerateColumns="false" 
    ItemType="ServiceMonitoring.MyClass" 
    SelectMethod="GetMyClassItems" 
    CellPadding="4" 
    ShowFooter="true">
    <Columns>
        <asp:BoundField DataField="MyProperty" HeaderText="ID" />
        <asp:TemplateField HeaderText="Action">
            <ItemTemplate>
                <asp:Button runat="server" 
                    CommandArgument='<%= MyClass.MyProperty %>' 
                    CommandName="ThisBtnClick" 
                    OnClick="Unnamed_Click" 
                    Text="retraiter !" />
                <%--<button onclick="UpdateMyClassItems" runat="server" value="VALEUR">retraiter !</button>--%>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Code-behind代码隐藏

public partial class WebForm1: System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e) { }

    public List<MyClass> GetMyClassItems()
    {
        var a = new MyClass() { MyProperty = 2 };
        return new List<MyClass>() { a };
    }

    protected void Unnamed_Click(object sender, EventArgs e)
    {
        var arg = (sender as Button).CommandArgument;
        string ID = arg.ToString();
    }
}

public class MyClass
{
    public int MyProperty { get; set; }
}

Command argument binding doesn't work.命令参数绑定不起作用。 Can you help me please ?你能帮我吗 ?

Change the CommandArgument like this.像这样更改CommandArgument

CommandArgument='<%# Eval("MyProperty") %>'

that will do.那样做。 You are already mentioning your ItemType="ServiceMonitoring.MyClass"您已经提到了您的ItemType="ServiceMonitoring.MyClass"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM