Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aspx Web Forms Command argument

Tags:

c#

button

asp.net

I have a list of records. Each record has unique id and text. When I blow it on page, each record has a button, who refer to method DeleteRecord(object sender, EventArgs e). But this button's doesn't know record's id. I tried push id on CommandArgument, but it's doesn't works.

Is there is another way to solve this trouble, or I simply make mistake, when I fill CommandArgument?

Profile.aspx:

<%
foreach (DAL.Record record in records) 
{
    <b><% Response.Write(record.Text);%></b>
    <asp:Button CommandArgument="<%#Eval("record.Id")%>" runat="server" OnClick="DeleteRecord" />
}
%>

When I pause my program on Profile.cs in method:

protected void DeleteRecord(object sender, EventArgs e)
    (Button)sender.CommandArgument 

    Always == "";

The problem in CommandArgument="<%#Eval("record.Id")%>", or my DNA. Forgive me for my poor english.

like image 202
user3041948 Avatar asked May 09 '26 05:05

user3041948


1 Answers

You can't use inline scripts in server controls, that is:

<asp:Button CommandName="Delete" CommandArgument="<%#Eval("record.Id")%>" />

Is wrong, because you're trying to use server code in an attribute of a server control.

Instead of this, you either have to build the button manually (as an HTML control, not a server control), or you have to use a Repeater instead of foreach, and assign the CommandArgument in the repeater's ItemDataBound event handler.

like image 108
Luaan Avatar answered May 11 '26 19:05

Luaan