Pass an ID to OnClick event with Repeater control?

可紊 提交于 2021-01-27 13:41:26

问题


Here is my screen:

enter image description here

Here is my code for the ItemTemplate in the Repeater:

<ItemTemplate>
    <div style="float: left; overflow: hidden; display: inline-block; border-style: solid; margin: 5px; background-color: Silver">
        <div style="text-align:center">
            <asp:Label ID="lblImage" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "image") %>'></asp:Label>
        </div>
        <asp:Image runat="server" ID="image1" Width="250px" Height="250px" ImageUrl='<%# DataBinder.Eval(Container.DataItem, "url") %>' />
        <div style="text-align: center;">
            <asp:Button runat="server" ID="btnNew" Text="New" />
            <asp:Button runat="server" ID="btnOriginal" Text="Original" />
        </div>
    </div>

The Repeater uses a dataset in my program to populate the ItemTemplate's label and image controls. There's another field in my dataset called graphicID. I'd like to, somehow, pass the value in that field to the 'Original' button, so that if a user presses that button, that particular graphicID is passed into the click event. Does this make sense?

For instance, the second image is Captain Harlock. The graphicID for this image is 93. If the user presses the Original button under Captain Harlock, I want to pass 93 to the onClick event. I'm not sure how to do this, though. If someone could point me in the right direction, I'd greatly appreciate it!


回答1:


<div style="text-align: center;">
        <asp:Button runat="server" ID="btnNew" Text="New" />
        <asp:Button runat="server" CommandName="cmd_original" CommandArgument="name of field which you want to access" ID="btnOriginal" Text="Original" />
    </div>

EDIT

In repeater control

you have to add an event as below:

 onitemcommand="Repeater1_ItemCommand"

In code behind

 protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    if (e.CommandName == "cmd_original")
    {
        Int32 id = Convert.ToInt32(e.CommandArgument);
    }
}


来源:https://stackoverflow.com/questions/15973991/pass-an-id-to-onclick-event-with-repeater-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!