简体   繁体   中英

how to transmit value of selected Datalist variable from html to the c# method in the codebehind?

I have an html page that loads a datalist with info about candidates. Each cell has a button that I want the user to be able to click on when they want to select that particular candidate. The Id of the selected candidate should be accessible in the OnClick method in the corresponding c# code in order to use it for subsequent operations. I've searched all over for a way to do it with no success. I hope somebody can help me. Thank you.

In the HTML:

<asp:Datalist ID="DataList1" runat="server"   > 
    <ItemTemplate>                
        <table class="table-catalog-candidates">
            <tr>
                <td>
                    <%#Eval("IdCandidate")%>
                    <asp:Button ID="Button1" runat="server" 
                        Text="Invite Candidate" 
                        OnClick="BtnInviteCandidate_Click" />
                </td>
            </tr>
        </table>
    </ItemTemplate>
</asp:Datalist>

In the Codebehind:

protected void BtnInviteCandidate_Click(object sender, EventArgs e)
{
    //several processes follow here…
    //how to transmit the value of the selected “IdCandidate” 
    //from the Datalist to this method and 
    //put it in a variable?
}
<asp:Button ID="Button1" runat="server" Text="Invite Candidate" CommandArgument='<%#Eval("IdCandidate")%>'OnClick="BtnInviteCandidate_Click"  />

Then in code behind.

(sender as Button).CommandArgument

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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